diff --git a/scripts/lib/eval-harness/gate.js b/scripts/lib/eval-harness/gate.js index 6ec49938782..5fbbd80960a 100644 --- a/scripts/lib/eval-harness/gate.js +++ b/scripts/lib/eval-harness/gate.js @@ -52,8 +52,16 @@ class GateError extends Error { } } +// UTF-16 code-unit order (plain `<`). localeCompare follows the host locale +// (sv_SE sorts `ä` after `z`), which would give the same tree a different +// digest on another machine. +function compareNames(a, b) { + if (a.name === b.name) return 0; + return a.name < b.name ? -1 : 1; +} + function listFiles(dir, base = dir, acc = []) { - for (const entry of fs.readdirSync(dir, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) { + for (const entry of fs.readdirSync(dir, { withFileTypes: true }).sort(compareNames)) { if (entry.name === 'node_modules' || entry.name === '.git') { continue; } @@ -94,14 +102,21 @@ function readRegularFile(filePath, encoding) { } } -/** Content digest of a directory tree: sorted relative paths and bytes. */ +/** + * Content digest of a directory tree: sorted relative paths and bytes. + * Each file is framed as `path \0 byteLength \0 bytes`. Paths cannot contain + * NUL, and the length bounds the bytes, so contents holding NUL cannot shift + * a file boundary and two different trees cannot share a byte stream. + */ function digestDir(dir) { const hash = crypto.createHash('sha256'); for (const relative of listFiles(dir)) { + const bytes = readRegularFile(path.join(dir, relative)); hash.update(relative); hash.update('\0'); - hash.update(readRegularFile(path.join(dir, relative))); + hash.update(String(bytes.length)); hash.update('\0'); + hash.update(bytes); } return hash.digest('hex'); } diff --git a/tests/lib/eval-harness/gate.test.js b/tests/lib/eval-harness/gate.test.js index 7fa9344e94c..f7fd8197f0d 100644 --- a/tests/lib/eval-harness/gate.test.js +++ b/tests/lib/eval-harness/gate.test.js @@ -2,6 +2,7 @@ const assert = require('assert'); const fs = require('fs'); const path = require('path'); +const { spawnSync } = require('child_process'); const gate = require('../../../scripts/lib/eval-harness/gate'); const { test, tempDir, cleanup, finish } = require('./helpers'); const example = path.resolve(__dirname,'../../../examples/eval-harness'); @@ -14,6 +15,35 @@ test('directory digests are stable and distinguish baseline from candidate',()=> assert.notEqual(gate.digestDir(candidate),gate.digestDir(baseline)); assert.match(gate.digestDir(candidate),/^[0-9a-f]{64}$/); }); +test('directory digests frame file contents, so NUL bytes cannot merge or split files',()=>{ + const root=tempDir('gate-digest-framing'); + try { + const merged=path.join(root,'merged');const split=path.join(root,'split'); + fs.mkdirSync(merged);fs.mkdirSync(split); + fs.writeFileSync(path.join(merged,'a'),'x\0b\0y'); + fs.writeFileSync(path.join(split,'a'),'x');fs.writeFileSync(path.join(split,'b'),'y'); + assert.notEqual(gate.digestDir(merged),gate.digestDir(split)); + const empty=path.join(root,'empty');const nul=path.join(root,'nul'); + fs.mkdirSync(empty);fs.mkdirSync(nul); + fs.writeFileSync(path.join(empty,'a'),'');fs.writeFileSync(path.join(nul,'a'),'\0'); + assert.notEqual(gate.digestDir(empty),gate.digestDir(nul)); + } finally {cleanup(root);} +}); +test('directory digests order entries by UTF-16 code unit, independent of the host locale',()=>{ + const root=tempDir('gate-digest-locale'); + try { + for (const name of ['a.js','B.js','z.js','ä.js','_x.js','-x.js']) fs.writeFileSync(path.join(root,name),name); + fs.mkdirSync(path.join(root,'a'));fs.writeFileSync(path.join(root,'a','b.js'),'nested'); + const digestUnder=locale=>{ + const child=spawnSync(process.execPath,['-e',`process.stdout.write(require(${JSON.stringify(require.resolve('../../../scripts/lib/eval-harness/gate'))}).digestDir(${JSON.stringify(root)}))`], + {encoding:'utf8',env:{...process.env,LC_ALL:locale,LANG:locale}}); + assert.equal(child.status,0,child.stderr); + return child.stdout; + }; + const reference=gate.digestDir(root); + for (const locale of ['en_US.UTF-8','sv_SE.UTF-8','tr_TR.UTF-8','C']) assert.equal(digestUnder(locale),reference,locale); + } finally {cleanup(root);} +}); test('variant and taskset inspection remains available without execution',()=>{ const v=gate.loadVariant(candidate);const t=gate.loadTaskset(taskset); assert.equal(v.entry,'run.js');assert.equal(t.tasks.length,12);assert.match(t.digest,/^[0-9a-f]{64}$/);