Skip to content

Commit

Permalink
fix: forbidden non-ascii binary subpath (#405)
Browse files Browse the repository at this point in the history
closes #395
  • Loading branch information
fengmk2 committed Feb 13, 2023
1 parent f7344eb commit 7b52f6f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
10 changes: 8 additions & 2 deletions app/port/controller/BinarySyncController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import { AbstractController } from './AbstractController';
import { BinarySyncerService } from '../../core/service/BinarySyncerService';
import { Binary } from '../../core/entity/Binary';
import binaries, { BinaryName } from '../../../config/binaries';
import { BinaryNameRule } from '../typebox';
import { BinaryNameRule, BinarySubpathRule } from '../typebox';

@HTTPController()
export class BinarySyncController extends AbstractController {
@Inject()
Expand Down Expand Up @@ -54,14 +55,19 @@ export class BinarySyncController extends AbstractController {
// check binaryName valid
try {
ctx.tValidate(BinaryNameRule, binaryName);
} catch (e) {
} catch {
throw new NotFoundError(`Binary "${binaryName}" not found`);
}
subpath = subpath || '/';
if (subpath === '/') {
const items = await this.binarySyncerService.listRootBinaries(binaryName);
return this.formatItems(items);
}
try {
ctx.tValidate(BinarySubpathRule, subpath);
} catch {
throw new NotFoundError(`Binary "${binaryName}/${subpath}" not found`);
}
subpath = `/${subpath}`;
const parsed = path.parse(subpath);
const parent = parsed.dir === '/' ? '/' : `${parsed.dir}/`;
Expand Down
4 changes: 4 additions & 0 deletions app/port/typebox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export const BinaryNameRule = Type.String({
maxLength: 220,
});

// `[ -~]` matches all printable ASCII characters
// https://catonmat.net/my-favorite-regex
export const BinarySubpathRule = Type.RegEx(/^[ -~]{1,1024}$/);

export const Tag = Type.String({
format: 'semver-tag',
transform: [ 'trim' ],
Expand Down
10 changes: 10 additions & 0 deletions test/port/controller/BinarySyncController/showBinary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ describe('test/port/controller/BinarySyncController/showBinary.test.ts', () => {
]);
});

it('should forbidden invalid paths', async () => {
const res = await app.httpRequest()
.get('/-/binary/chromium-browser-snapshots/Linux_x64/970485/%E4%B8%8B%E8%BD%BD%E7%9A%84');
assert.equal(res.status, 404);
assert.equal(res.headers['content-type'], 'application/json; charset=utf-8');
assert.deepEqual(res.body, {
error: '[NOT_FOUND] Binary "chromium-browser-snapshots/Linux_x64/970485/下载的" not found',
});
});

it('should show node binaries', async () => {
app.mockHttpclient('https://nodejs.org/dist/index.json', 'GET', {
data: await TestUtil.readFixturesFile('nodejs.org/site/index.json'),
Expand Down

0 comments on commit 7b52f6f

Please sign in to comment.