Skip to content

Add port validation checks for HTTP streaming #315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 21, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
typing fixes + removed logs
  • Loading branch information
Victoria Hall committed Nov 14, 2024
commit 10a6ce33f45c2908c6b51f2f3aced6cde93e5c8f
45 changes: 25 additions & 20 deletions src/http/httpProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,22 +115,24 @@ export async function setupHttpProxy(): Promise<string> {

server.listen(() => {
const address = server.address();
if (address !== null && address.port === 0) {
// Auto-assigned port is 0, find and bind to an open port
workerSystemLog('debug', `VICTORIA: Port 0 assigned. Finding open port.`);
findOpenPort(51929, (openPort) => {
workerSystemLog('debug', `VICTORIA: found open port: ${openPort}`);
// Close the server and re-listen on the found open port
server.close();
server.listen(openPort, () => {
workerSystemLog('debug', `VICTORIA: server is now listening on found open port: ${openPort}`);
// Valid address has been created
if (address !== null && typeof address === 'object') {
if (address.port === 0) {
// Auto-assigned port is 0, find and bind to an open port
workerSystemLog('debug', `Port 0 assigned. Finding open port.`);
findOpenPort(51929, (openPort: number) => {
// Close the server and re-listen on the found open port
server.close();
server.listen(openPort, () => {
workerSystemLog('debug', `Server is now listening on found open port: ${openPort}`);
});
resolve(`http://localhost:${openPort}/`);
});
resolve(`http://localhost:${openPort}/`);
});
} else if (address !== null && typeof address === 'object') {
// Auto-assigned port is not 0
workerSystemLog('debug', `VICTORIA: auto-assigned port is valid. Port: ${address.port}`);
resolve(`http://localhost:${address.port}/`);
} else {
// Auto-assigned port is not 0
workerSystemLog('debug', `Auto-assigned port is valid. Port: ${address.port}`);
resolve(`http://localhost:${address.port}/`);
}
} else {
reject(new AzFuncSystemError('Unexpected server address during http proxy setup'));
}
Expand All @@ -143,20 +145,23 @@ export async function setupHttpProxy(): Promise<string> {
}

// Function to find an open port starting from a specified port
function findOpenPort(startingPort, callback) {
function findOpenPort(startingPort: number, callback: (port: number) => void): void {
const server = net.createServer();

function tryPort(port) {
function tryPort(port: number) {
server.once('error', () => {
// If the port is unavailable, increment and try the next one
tryPort(port + 1);
});

// If the port is available, return it
server.once('listening', () => {
const port = server.address().port;
server.close();
callback(port);
const address = server.address();
if (address !== null && typeof address === 'object') {
port = address.port;
server.close();
callback(port);
}
});

// Try binding to the given port
Expand Down
Loading