Skip to content
Open
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
8 changes: 8 additions & 0 deletions api/src/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ describe('validateFilePath', () => {
expect(() => validateFilePath('.', submissionDir)).toThrow(ValidationError);
});

it('rejects NUL bytes in file names', () => {
expect(() => validateFilePath('file\0.txt', submissionDir)).toThrow(/NUL/);
});

it('rejects path traversal with .. segments', () => {
expect(() => validateFilePath('../etc/passwd', submissionDir)).toThrow(ValidationError);
expect(() => validateFilePath('a/../../escape', submissionDir)).toThrow(ValidationError);
Expand Down Expand Up @@ -188,4 +192,8 @@ describe('isValidFilePath', () => {
expect(isValidFilePath('', submissionDir)).toBe(false);
expect(isValidFilePath('a'.repeat(MAX_LEN + 10), submissionDir)).toBe(false);
});

it('returns false for paths containing NUL bytes', () => {
expect(isValidFilePath('file\0.txt', submissionDir)).toBe(false);
});
});
3 changes: 3 additions & 0 deletions api/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ export function validateFilePath(name: string, submissionDir: string): void {
if (!name || name === '.') {
throw new ValidationError('File path must not be empty');
}
if (name.includes('\0')) {
throw new ValidationError('File path must not contain NUL bytes');
}
/* Reject absolute paths up front. `path.resolve(submissionDir, name)`
* ignores `submissionDir` when `name` is absolute, so an absolute path
* that happens to point inside `submissionDir` (e.g. the exact on-disk
Expand Down