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

fix: reject dangerous url characters (--open) #2258

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions src/abstract-server.cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ export class AbstractServerCommand extends AbstractCommand {
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
throw Error(`refuse to open non http(s) url (--open): ${url}`);
}
if (!url.href.match(/^[a-zA-Z0-9._:/?%=&-]+$/)) {
throw Error(`refuse to open unsafe url (--open): ${url}`);
}
this.log.info(`opening default browser: ${url.href}`);
await opn(url.href);
}
Expand Down
18 changes: 16 additions & 2 deletions test/abstract-server-cmd.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ describe('AbstractServerCommand test', () => {
await assert.rejects(cmd.open('file://etc/passwd'), Error('refuse to open non http(s) url (--open): file://etc/passwd'));
});

it('open rejects invalid hostname argument', async () => {
const cmd = new AbstractServerCommand();
// eslint-disable-next-line no-underscore-dangle
cmd._project = { server: { hostname: 'localhost', scheme: 'http', port: 3000 } };
await assert.rejects(cmd.open('https://$(calc.exe)'), Error('refuse to open unsafe url (--open): https://$(calc.exe)/'));
});

it('open rejects invalid url argument', async () => {
const cmd = new AbstractServerCommand();
// eslint-disable-next-line no-underscore-dangle
cmd._project = { server: { hostname: 'localhost', scheme: 'http', port: 3000 } };
await assert.rejects(cmd.open('/"; Start calc.exe; echo "foo'), Error('refuse to open unsafe url (--open): http://localhost:3000/%22;%20Start%20calc.exe;%20echo%20%22foo'));
});

it('constructs valid url from path', async () => {
let opened;
const { AbstractServerCommand: MockedCommand } = await esmock('../src/abstract-server.cmd.js', {
Expand All @@ -40,7 +54,7 @@ describe('AbstractServerCommand test', () => {
const cmd = new MockedCommand();
// eslint-disable-next-line no-underscore-dangle
cmd._project = { server: { hostname: 'localhost', scheme: 'http', port: 3000 } };
await cmd.open('/"; Start calc.exe; echo "foo');
assert.strictEqual(opened, 'http://localhost:3000/%22;%20Start%20calc.exe;%20echo%20%22foo');
await cmd.open('/test page');
assert.strictEqual(opened, 'http://localhost:3000/test%20page');
});
});