Skip to content
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

Require dev port selected is 80, 443, or between 1024-65535 #895

Merged
merged 2 commits into from
Apr 18, 2023
Merged
Changes from all commits
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
30 changes: 24 additions & 6 deletions src/commands/dev/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default class Dev extends BaseCommand {
static examples = [
'architect dev ./mycomponent/architect.yml',
'architect dev ./mycomponent/architect.yml -a myaccount --secrets-env=myenvironment',
'architect dev --port=81 --browser=false --debug=true --secret-file=./mycomponent/mysecrets.yml ./mycomponent/architect.yml',
'architect dev --port=1234 --browser=false --debug=true --secret-file=./mycomponent/mysecrets.yml ./mycomponent/architect.yml',
];

static flags = {
Expand Down Expand Up @@ -123,8 +123,9 @@ export default class Dev extends BaseCommand {
sensitive: false,
}),
port: Flags.integer({
description: '[default: 443] Port for the gateway',
description: 'Port for the gateway. Defaults to 443, or 80 if --ssl=false. Allowed port numbers are 80, 443, or any port between 1024 and 66535.',
sensitive: false,
max: 65535,
}),
// Used for proxy from deploy to dev. These will be removed once --local is deprecated
local: booleanString({
Expand Down Expand Up @@ -432,15 +433,16 @@ export default class Dev extends BaseCommand {
name: 'port',
message: `Trying to listen on port ${port}, but something is already using it. What port would you like us to run the API gateway on (you can use the '--port' flag to skip this message in the future)?`,
validate: (value) => {
if (new RegExp('^[1-9]+\\d*$').test(value)) {
return true;
const port = Number.parseInt(value);
if (!this.isValidPort(port)) {
return 'Port must be 80, 443, or any port between 1024 and 66535.';
}
return `Port can only be positive number.`;
return true;
},
},
]);

port = answers.port;
port = Number.parseInt(answers.port);
}
return port;
}
Expand Down Expand Up @@ -530,13 +532,29 @@ $ architect dev -e new_env_name_here .`));
return env_secrets;
}

/**
* Only allowed ports for architect dev are 1024-65535, or 80/443 (default http/https ports)
* This is to prevent users from choosing a well-known port that browers won't allow connections
* to (e.g. port 95 or 101).
*/
private isValidPort(port: number): boolean {
if (Number.isNaN(port)) {
return false;
}

return (port >= 1024 && port <= 65535) || port === 443 || port === 80;
}

private async runLocal() {
const { args, flags } = await this.parse(Dev);

const environment = flags.environment || DockerComposeUtils.DEFAULT_PROJECT;
await this.failIfEnvironmentExists(environment);

flags.port = await this.getAvailablePort(flags.port || (flags.ssl ? 443 : 80));
if (!this.isValidPort(flags.port)) {
throw new Error('Invalid port number. Port must be 80, 443, or any port between 1024 and 66535.');
}

if (flags.ssl) {
await this.downloadSSLCerts();
Expand Down