Skip to content
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
10 changes: 10 additions & 0 deletions src/sandbox/linux-sandbox-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,16 @@ async function generateFilesystemArgs(
pushReadDenyDirMounts(args, tmpfsDir, allowedWritePaths, readAllowPaths)
}
}
// Same problem for read-denied files: the /dev/null mask landed before the
// denyWrite ancestor bind, so the real file is back. Re-apply the mask.
for (const maskedFile of maskedFiles) {
if (emittedDenyWriteDests.some(dest => maskedFile.startsWith(dest + '/'))) {
logForDebugging(
`[Sandbox Linux] Re-applying denyRead file mask re-exposed by denyWrite bind: ${maskedFile}`,
)
args.push('--ro-bind', '/dev/null', maskedFile)
}
}

return args
}
Expand Down
36 changes: 36 additions & 0 deletions test/sandbox/wrap-with-sandbox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,42 @@ describe('allowWrite glob suffix handling', () => {
},
)

// File variant of the case above: a read-denied file gets a /dev/null bind,
// and a denyWrite ro-bind over an ancestor would re-expose the real file.
// The /dev/null mask must land again after the ancestor bind.
it.if(isLinux)(
'denyRead file mask survives a denyWrite bind over an ancestor dir (Linux)',
async () => {
const parentDir = join(tmpdir(), `srt-test-file-ancestor-${Date.now()}`)
const secretsDir = join(parentDir, 'secrets')
const keyFile = join(secretsDir, 'key.pem')
mkdirSync(secretsDir, { recursive: true })
writeFileSync(keyFile, '')

try {
await SandboxManager.reset()
await SandboxManager.initialize({
network: { allowedDomains: [], deniedDomains: [] },
filesystem: {
denyRead: [keyFile],
allowWrite: [parentDir],
denyWrite: [secretsDir],
},
})

const result = await SandboxManager.wrapWithSandbox(command)

const bindAt = result.indexOf(`--ro-bind ${secretsDir} ${secretsDir}`)
const maskAt = result.lastIndexOf(`--ro-bind /dev/null ${keyFile}`)
expect(bindAt).toBeGreaterThan(-1)
expect(maskAt).toBeGreaterThan(bindAt)
} finally {
await SandboxManager.reset()
rmSync(parentDir, { recursive: true, force: true })
}
},
)

// The #190 case must keep working: a denyRead tmpfs over an ancestor of an
// allowed write path wipes denyWrite binds under that write path; they are
// restored by emitting denyWrite after the denyRead re-binds. The tmpfs
Expand Down
Loading