Skip to content

Commit e82da5b

Browse files
committed
Don't expose absolute paths to source files
This changes componentization to make paths relative to the current working directory. E.g. when componentizing `[project dir]/src/index.js`, instead of seeing that full path, content will now see `src/index.js`, because we preopen `[project dir]` as `/` and can thus treat all paths as relative to that. This only works for Wizer, since Weval doesn't have a way to map directories in this way.
1 parent 0a90d72 commit e82da5b

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/componentize.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from '@bytecodealliance/jco';
88
import { spawnSync } from 'node:child_process';
99
import { tmpdir } from 'node:os';
10-
import { resolve, join, basename, dirname } from 'node:path';
10+
import { resolve, join, dirname } from 'node:path';
1111
import { readFile, writeFile, mkdir, rm } from 'node:fs/promises';
1212
import { rmSync, existsSync } from 'node:fs';
1313
import { createHash } from 'node:crypto';
@@ -16,7 +16,7 @@ import {
1616
stubWasi,
1717
} from '../lib/spidermonkey-embedding-splicer.js';
1818
import { fileURLToPath } from 'node:url';
19-
import { stdout, platform } from 'node:process';
19+
import { cwd, stdout, platform } from 'node:process';
2020
export const { version } = JSON.parse(
2121
await readFile(new URL('../package.json', import.meta.url), 'utf8')
2222
);
@@ -206,11 +206,27 @@ export async function componentize(opts,
206206
console.log(env);
207207
}
208208

209-
let initializerPath = maybeWindowsPath(join(sourceDir, 'initializer.js'));
209+
let initializerPath = join(sourceDir, 'initializer.js');
210210
sourcePath = maybeWindowsPath(sourcePath);
211+
let workspacePrefix = dirname(sourcePath);
212+
213+
// If the source path is within the current working directory, strip the
214+
// cwd as a prefix from the source path, and remap the paths seen by the
215+
// component to be relative to the current working directory.
216+
// This only works in wizer, not in weval, because the latter doesn't
217+
// support --mapdir.
218+
if (!opts.enableAot && workspacePrefix.startsWith(cwd())) {
219+
workspacePrefix = cwd();
220+
sourcePath = sourcePath.slice(workspacePrefix.length + 1);
221+
}
211222
let args = `--initializer-script-path ${initializerPath} ${sourcePath}`;
212223
runtimeArgs = runtimeArgs ? `${runtimeArgs} ${args}` : args;
213-
let preopens = [`--dir ${maybeWindowsPath(sourceDir)}`, `--dir ${maybeWindowsPath(dirname(sourcePath))}`];
224+
let preopens = [`--dir ${sourceDir}`];
225+
if (opts.enableAot) {
226+
preopens.push(`--dir ${workspacePrefix}`);
227+
} else {
228+
preopens.push(`--mapdir /::${workspacePrefix}`);
229+
}
214230

215231
let wizerProcess;
216232

0 commit comments

Comments
 (0)