|
| 1 | +/** |
| 2 | + * Unit tests for browse/src/file-permissions.ts |
| 3 | + * |
| 4 | + * Strategy: |
| 5 | + * - POSIX assertions check fs.statSync.mode bits directly (cheap, reliable, |
| 6 | + * runs on every CI config). |
| 7 | + * - Windows assertions don't check ACLs (that'd require parsing icacls |
| 8 | + * output, which is brittle across Windows versions / locales). Instead |
| 9 | + * we verify the helper doesn't throw and the file ends up accessible |
| 10 | + * to the current user — the "doesn't crash, file still usable" |
| 11 | + * contract the callers rely on. |
| 12 | + */ |
| 13 | + |
| 14 | +import { afterEach, beforeEach, describe, expect, test } from 'bun:test'; |
| 15 | +import * as fs from 'fs'; |
| 16 | +import * as os from 'os'; |
| 17 | +import * as path from 'path'; |
| 18 | + |
| 19 | +import { |
| 20 | + restrictFilePermissions, |
| 21 | + restrictDirectoryPermissions, |
| 22 | + writeSecureFile, |
| 23 | + appendSecureFile, |
| 24 | + mkdirSecure, |
| 25 | + __resetWarnedForTests, |
| 26 | +} from '../src/file-permissions'; |
| 27 | + |
| 28 | +let tmpDir: string; |
| 29 | + |
| 30 | +beforeEach(() => { |
| 31 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'file-perms-')); |
| 32 | + __resetWarnedForTests(); |
| 33 | +}); |
| 34 | + |
| 35 | +afterEach(() => { |
| 36 | + try { fs.rmSync(tmpDir, { recursive: true, force: true }); } catch { /* best-effort */ } |
| 37 | +}); |
| 38 | + |
| 39 | +describe('restrictFilePermissions', () => { |
| 40 | + test('on POSIX, sets file mode to 0o600', () => { |
| 41 | + if (process.platform === 'win32') return; |
| 42 | + const p = path.join(tmpDir, 'secret'); |
| 43 | + fs.writeFileSync(p, 'token'); |
| 44 | + fs.chmodSync(p, 0o644); // start world-readable to prove the call mutates it |
| 45 | + restrictFilePermissions(p); |
| 46 | + expect(fs.statSync(p).mode & 0o777).toBe(0o600); |
| 47 | + }); |
| 48 | + |
| 49 | + test('on Windows, does not throw on an existing file', () => { |
| 50 | + if (process.platform !== 'win32') return; |
| 51 | + const p = path.join(tmpDir, 'secret'); |
| 52 | + fs.writeFileSync(p, 'token'); |
| 53 | + expect(() => restrictFilePermissions(p)).not.toThrow(); |
| 54 | + // File remains readable by the caller — core contract. |
| 55 | + expect(fs.readFileSync(p, 'utf8')).toBe('token'); |
| 56 | + }); |
| 57 | + |
| 58 | + test('on Windows, does not throw when icacls fails (bad path)', () => { |
| 59 | + if (process.platform !== 'win32') return; |
| 60 | + // icacls emits an error for a nonexistent path; helper must swallow. |
| 61 | + expect(() => restrictFilePermissions(path.join(tmpDir, 'nonexistent'))).not.toThrow(); |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +describe('restrictDirectoryPermissions', () => { |
| 66 | + test('on POSIX, sets directory mode to 0o700', () => { |
| 67 | + if (process.platform === 'win32') return; |
| 68 | + const d = path.join(tmpDir, 'subdir'); |
| 69 | + fs.mkdirSync(d, { mode: 0o755 }); |
| 70 | + restrictDirectoryPermissions(d); |
| 71 | + expect(fs.statSync(d).mode & 0o777).toBe(0o700); |
| 72 | + }); |
| 73 | + |
| 74 | + test('on Windows, does not throw on an existing directory', () => { |
| 75 | + if (process.platform !== 'win32') return; |
| 76 | + const d = path.join(tmpDir, 'subdir'); |
| 77 | + fs.mkdirSync(d); |
| 78 | + expect(() => restrictDirectoryPermissions(d)).not.toThrow(); |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +describe('writeSecureFile', () => { |
| 83 | + test('writes the payload and restricts permissions atomically', () => { |
| 84 | + const p = path.join(tmpDir, 'data'); |
| 85 | + writeSecureFile(p, 'hello'); |
| 86 | + expect(fs.readFileSync(p, 'utf8')).toBe('hello'); |
| 87 | + if (process.platform !== 'win32') { |
| 88 | + expect(fs.statSync(p).mode & 0o777).toBe(0o600); |
| 89 | + } |
| 90 | + }); |
| 91 | + |
| 92 | + test('accepts Buffer payloads', () => { |
| 93 | + const p = path.join(tmpDir, 'buffer'); |
| 94 | + writeSecureFile(p, Buffer.from([0xde, 0xad, 0xbe, 0xef])); |
| 95 | + const out = fs.readFileSync(p); |
| 96 | + expect(out.length).toBe(4); |
| 97 | + expect(out[0]).toBe(0xde); |
| 98 | + }); |
| 99 | + |
| 100 | + test('overwrites existing file', () => { |
| 101 | + const p = path.join(tmpDir, 'existing'); |
| 102 | + fs.writeFileSync(p, 'old', { mode: 0o644 }); |
| 103 | + writeSecureFile(p, 'new'); |
| 104 | + expect(fs.readFileSync(p, 'utf8')).toBe('new'); |
| 105 | + }); |
| 106 | +}); |
| 107 | + |
| 108 | +describe('appendSecureFile', () => { |
| 109 | + test('appends to a new file and sets owner-only permissions', () => { |
| 110 | + const p = path.join(tmpDir, 'log'); |
| 111 | + appendSecureFile(p, 'line1\n'); |
| 112 | + expect(fs.readFileSync(p, 'utf8')).toBe('line1\n'); |
| 113 | + if (process.platform !== 'win32') { |
| 114 | + expect(fs.statSync(p).mode & 0o777).toBe(0o600); |
| 115 | + } |
| 116 | + }); |
| 117 | + |
| 118 | + test('appends without re-applying ACL on subsequent writes', () => { |
| 119 | + const p = path.join(tmpDir, 'log'); |
| 120 | + appendSecureFile(p, 'line1\n'); |
| 121 | + appendSecureFile(p, 'line2\n'); |
| 122 | + expect(fs.readFileSync(p, 'utf8')).toBe('line1\nline2\n'); |
| 123 | + }); |
| 124 | +}); |
| 125 | + |
| 126 | +describe('mkdirSecure', () => { |
| 127 | + test('creates directory with owner-only mode (POSIX)', () => { |
| 128 | + if (process.platform === 'win32') return; |
| 129 | + const d = path.join(tmpDir, 'nested', 'deep'); |
| 130 | + mkdirSecure(d); |
| 131 | + expect(fs.statSync(d).isDirectory()).toBe(true); |
| 132 | + expect(fs.statSync(d).mode & 0o777).toBe(0o700); |
| 133 | + }); |
| 134 | + |
| 135 | + test('is idempotent — safe to call on existing directory', () => { |
| 136 | + const d = path.join(tmpDir, 'dir'); |
| 137 | + mkdirSecure(d); |
| 138 | + expect(() => mkdirSecure(d)).not.toThrow(); |
| 139 | + }); |
| 140 | + |
| 141 | + test('recursive behavior: creates intermediate directories', () => { |
| 142 | + const d = path.join(tmpDir, 'a', 'b', 'c'); |
| 143 | + mkdirSecure(d); |
| 144 | + expect(fs.existsSync(path.join(tmpDir, 'a'))).toBe(true); |
| 145 | + expect(fs.existsSync(path.join(tmpDir, 'a', 'b'))).toBe(true); |
| 146 | + expect(fs.existsSync(d)).toBe(true); |
| 147 | + }); |
| 148 | +}); |
0 commit comments