Skip to content

Commit cd530aa

Browse files
committed
fix: make workspace root scan guard cross-platform
1 parent fd44b7f commit cd530aa

2 files changed

Lines changed: 17 additions & 14 deletions

File tree

‎hooks/_runtime/lifecycle-dangerous-command.cjs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ function buildLifecycleDangerousCommandUtils({
5050
* e.g. E:\Worker\queuebit or E:\Worker\queuebit\docs (R-04: no trailing slash required).
5151
*/
5252
function commandTargetsWorkspaceChild(cmdLower, rootLower) {
53-
const escaped = rootLower.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
53+
const rootInCommand = rootLower.replace(/\//g, '\\')
54+
const escaped = rootInCommand.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
5455
return new RegExp(escaped + '[\\\\/][a-z0-9._-]+', 'i').test(cmdLower)
5556
}
5657

‎scripts/test-workspace-root-scan-ban.js‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ const {
99
} = require('./lib/host-parity-scorecard.js')
1010
const { buildLifecycleDangerousCommandUtils } = require('../hooks/_runtime/lifecycle-dangerous-command.cjs')
1111

12-
const ROOT = 'E:\\Worker'
12+
const ROOT = process.platform === 'win32' ? 'E:\\Worker' : '/home/runner/work'
13+
const PROJECT = path.join(ROOT, 'queuebit')
14+
const DOCS = path.join(PROJECT, 'docs')
1315

1416
function makeUtils(contextRoot = ROOT) {
1517
return buildLifecycleDangerousCommandUtils({
@@ -45,35 +47,35 @@ function expectAllow(cmd, label, payloadExtra = {}) {
4547

4648
// R-01 sample / original relapse
4749
expectBan(
48-
'Get-ChildItem -Path "E:\\Worker" -Directory -Filter "*queuebit*" -Recurse -Depth 3',
50+
`Get-ChildItem -Path "${ROOT}" -Directory -Filter "*queuebit*" -Recurse -Depth 3`,
4951
'absolute root recurse depth'
5052
)
5153
// project scoped allow
5254
expectAllow(
53-
'Get-ChildItem -Path "E:\\Worker\\queuebit\\docs" -Recurse -Filter *.md',
55+
`Get-ChildItem -Path "${DOCS}" -Recurse -Filter *.md`,
5456
'project docs recurse'
5557
)
5658
// R-04: first-level child without trailing slash
5759
expectAllow(
58-
'Get-ChildItem -Path "E:\\Worker\\queuebit" -Recurse',
60+
`Get-ChildItem -Path "${PROJECT}" -Recurse`,
5961
'first-level project path'
6062
)
6163
// R-02: dir /s
62-
expectBan('dir /s E:\\Worker', 'dir /s workspace root')
64+
expectBan(`dir /s "${ROOT}"`, 'dir /s workspace root')
6365
expectBan('dir /s', 'dir /s relative at workspace cwd')
6466
// find at workspace root (C16)
65-
expectBan('find E:\\Worker -type f', 'find workspace root')
67+
expectBan(`find "${ROOT}" -type f`, 'find workspace root')
6668
// R-03: relative recurse at workspace cwd
6769
expectBan('Get-ChildItem -Recurse -Depth 3', 'relative recurse at workspace cwd')
6870
expectBan('Get-ChildItem -Recurse', 'relative recurse bare')
6971
// relative child at workspace cwd
7072
expectAllow('Get-ChildItem queuebit -Recurse', 'relative child name')
7173
expectAllow('Get-ChildItem -Path queuebit -Recurse', 'relative -Path child')
7274
// project cwd relative recurse allowed (CONTEXT_ROOT = project)
73-
const projectUtils = makeUtils('E:\\Worker\\queuebit')
75+
const projectUtils = makeUtils(PROJECT)
7476
assert.strictEqual(
7577
projectUtils.checkDangerousCommand({
76-
tool_input: { command: 'Get-ChildItem -Recurse', cwd: 'E:\\Worker\\queuebit' }
78+
tool_input: { command: 'Get-ChildItem -Recurse', cwd: PROJECT }
7779
}, 'grok'),
7880
null,
7981
'relative recurse inside project cwd'
@@ -82,20 +84,20 @@ assert.strictEqual(
8284
// Probe parity with Hook (R-04)
8385
assert.strictEqual(
8486
classifyWorkspaceRootScanSample(
85-
'Get-ChildItem -Path "E:\\Worker\\queuebit" -Recurse',
87+
`Get-ChildItem -Path "${PROJECT}" -Recurse`,
8688
{ workspaceRoot: ROOT }
8789
),
8890
'project-scoped-ok'
8991
)
9092
assert.strictEqual(
9193
classifyWorkspaceRootScanSample(
92-
'Get-ChildItem -Path "E:\\Worker" -Recurse -Depth 3',
94+
`Get-ChildItem -Path "${ROOT}" -Recurse -Depth 3`,
9395
{ workspaceRoot: ROOT }
9496
),
9597
'workspace-root-recurse'
9698
)
9799
assert.strictEqual(
98-
classifyWorkspaceRootScanSample('dir /s E:\\Worker', { workspaceRoot: ROOT }),
100+
classifyWorkspaceRootScanSample(`dir /s "${ROOT}"`, { workspaceRoot: ROOT }),
99101
'workspace-root-recurse'
100102
)
101103
assert.strictEqual(
@@ -115,10 +117,10 @@ assert.strictEqual(
115117

116118
// direct inventory helpers
117119
assert.strictEqual(utils.isWorkspaceRootRecursiveInventory(
118-
'Get-ChildItem -Path "E:\\Worker" -Recurse', ROOT, { cwd: ROOT }
120+
`Get-ChildItem -Path "${ROOT}" -Recurse`, ROOT, { cwd: ROOT }
119121
), true)
120122
assert.strictEqual(utils.isWorkspaceRootRecursiveInventory(
121-
'Get-ChildItem -Path "E:\\Worker\\queuebit" -Recurse', ROOT, { cwd: ROOT }
123+
`Get-ChildItem -Path "${PROJECT}" -Recurse`, ROOT, { cwd: ROOT }
122124
), false)
123125

124126
console.log('workspace-root-scan-ban unit OK (R-01..R-04 matrix)')

0 commit comments

Comments
 (0)