Skip to content

Commit 1a53759

Browse files
committed
test: add test for OXC rewriter's handling of cache-busting query strings
feat: implement parserFilename function for improved filename handling
1 parent a106719 commit 1a53759

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

test/js/rewriter.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ test('OXC rewriter supports modules and fails closed on parse errors', async ()
3838
assert.equal(bad.errorCode, 'PARSE_FAILED');
3939
assert.match(rewriter.blockSource(), /Blocked by ZeroProxy rewrite policy/);
4040
});
41+
test('OXC rewriter accepts target URLs with cache-busting query strings', async () => {
42+
const rewriter = await loadRewriter();
43+
const out = rewriter.rewriteScript(`window.location = "/next";`, {
44+
kind: 'classic',
45+
targetUrl: 'https://ipleak.net/static/js/index.js?ts=20220812#frag',
46+
});
47+
assert.equal(out.ok, true, JSON.stringify(out.diagnostics));
48+
assert.match(out.code, /__zp_set\(__zp_get\(globalThis,"window"\),"location","\/next"\)/);
49+
});
4150

4251
test('OXC rewriter blocks constructor escape compound writes', async () => {
4352
const rewriter = await loadRewriter();

web/js-rewriter.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,20 @@
7979
return ok(rewritten.code.slice(prefix.length, rewritten.code.length - suffix.length), parsed.diagnostics.concat(rewritten.diagnostics));
8080
}
8181

82+
function parserFilename(filename) {
83+
let value = String(filename || 'target.js');
84+
let end = value.length;
85+
const query = value.indexOf('?');
86+
const hash = value.indexOf('#');
87+
if (query !== -1 && query < end) end = query;
88+
if (hash !== -1 && hash < end) end = hash;
89+
if (end !== value.length) value = value.slice(0, end);
90+
return value && !value.endsWith('/') && !value.endsWith('\\') ? value : 'target.js';
91+
}
92+
8293
function parse(source, sourceType, filename) {
8394
let result;
84-
try { result = parser.parseSync(source, { sourceType, sourceFilename: filename || 'target.js' }); }
95+
try { result = parser.parseSync(source, { sourceType, sourceFilename: parserFilename(filename) }); }
8596
catch (err) { return blocked('PARSE_FAILED', [{ level: 'error', message: err && err.message || 'PARSE_FAILED' }]); }
8697
const errors = result.errors || [];
8798
if (errors.length) return blocked('PARSE_FAILED', errors.map(e => ({ level: e.severity || 'error', message: e.message || 'PARSE_FAILED', start: e.start, end: e.end })));

0 commit comments

Comments
 (0)