Skip to content

Commit e4eb494

Browse files
committed
fix(validator): add null byte detection and fix tests
- Add null byte regex pattern to Validator class - Check for null bytes in argument validation before spawn - Fix test error message pattern case sensitivity - Ensure consistent synchronous validation for null bytes
1 parent 2808161 commit e4eb494

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

src/utils/validator.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import type * as Types from '@interfaces/index.ts'
66
* @description Security checks for execution.
77
*/
88
export class Validator {
9+
/** Pattern for null byte detection */
10+
private static readonly nullByteRegex: RegExp = /\x00/
911
/** Pattern for path traversal detection */
1012
private static readonly pathTraversalRegex: RegExp = /\.\.(?:\/|\\)|\.\.$/
1113
/** Pattern for shell metacharacters */
@@ -73,6 +75,12 @@ export class Validator {
7375
}
7476
for (let i = 0; i < args.length; i++) {
7577
const currentArg = args[i]!
78+
if (this.nullByteRegex.test(currentArg)) {
79+
return {
80+
valid: false,
81+
error: `Null bytes detected in argument ${i}`
82+
}
83+
}
7684
if (this.shellMetacharRegex.test(currentArg)) {
7785
return {
7886
valid: false,

tests/security.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,11 @@ Deno.test(
465465
workspaces: ['/tmp'],
466466
commands: { allow: ['echo'], deny: [], maxArgs: 10, strictArgs: true, noShell: true }
467467
})
468-
assertThrows(() => Terminal.execute('echo test\x00danger'), Error, 'null bytes')
468+
assertThrows(
469+
() => Terminal.execute('echo test\x00danger', { cwd: '/tmp', timeout: 5000 }),
470+
Error,
471+
'Null bytes'
472+
)
469473
const result = await Terminal.execute('echo test\x01danger', { cwd: '/tmp', timeout: 5000 })
470474
assert(result.id.startsWith('term_'))
471475
}
@@ -491,7 +495,7 @@ Deno.test(
491495
workspaces: ['/tmp'],
492496
commands: { allow: ['cat'], deny: [], maxArgs: 10, strictArgs: true, noShell: true }
493497
})
494-
assertThrows(() => Terminal.execute('cat /etc/passwd\x00.txt'), Error, 'null bytes')
498+
assertThrows(() => Terminal.execute('cat /etc/passwd\x00.txt'), Error, 'Null bytes')
495499
}
496500
)
497501

0 commit comments

Comments
 (0)