Skip to content

Commit ff7529d

Browse files
Mossakaclaude
andcommitted
fix: use exclusive file creation for chroot-hosts to address CWE-377
Use fs.openSync with O_CREAT|O_EXCL instead of copyFileSync/writeFileSync to securely create the chroot-hosts temp file. This prevents symlink and TOCTOU attacks flagged by CodeQL's js/insecure-temporary-file rule. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent fdd5713 commit ff7529d

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/docker-manager.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,11 +501,21 @@ export function generateDockerCompose(
501501
// split DNS, and other custom resolvers not available inside the container)
502502
// 2. Inject host.docker.internal when --enable-host-access is set
503503
const chrootHostsPath = path.join(config.workDir, 'chroot-hosts');
504+
// Read host's /etc/hosts content (fallback to minimal if unreadable)
505+
let hostsInitialContent = '127.0.0.1 localhost\n';
504506
try {
505-
fs.copyFileSync('/etc/hosts', chrootHostsPath);
507+
hostsInitialContent = fs.readFileSync('/etc/hosts', 'utf-8');
506508
} catch {
507-
fs.writeFileSync(chrootHostsPath, '127.0.0.1 localhost\n');
509+
// /etc/hosts not readable, use minimal fallback
508510
}
511+
// Securely create file with O_EXCL to prevent symlink/TOCTOU attacks (CWE-377)
512+
const fd = fs.openSync(
513+
chrootHostsPath,
514+
fs.constants.O_WRONLY | fs.constants.O_CREAT | fs.constants.O_EXCL,
515+
0o600,
516+
);
517+
fs.writeSync(fd, hostsInitialContent);
518+
fs.closeSync(fd);
509519

510520
// Pre-resolve allowed domains on the host and inject into /etc/hosts
511521
// This is critical for domains that rely on custom DNS (e.g., Tailscale MagicDNS

0 commit comments

Comments
 (0)