Skip to content

Commit 4bc5c6f

Browse files
committed
fix: path traversal on windows
1 parent 73d8afb commit 4bc5c6f

File tree

4 files changed

+47
-6
lines changed

4 files changed

+47
-6
lines changed

packages/webcrack/src/unpack/bundle.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import traverse from '@babel/traverse';
22
import type * as m from '@codemod/matchers';
3-
import { posix } from 'node:path';
3+
import { dirname, join, normalize } from 'node:path';
44
import type { Module } from './module';
55

6-
// eslint-disable-next-line @typescript-eslint/unbound-method
7-
const { dirname, join, normalize } = posix;
8-
96
export class Bundle {
107
type: 'webpack' | 'browserify';
118
entryId: string;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
(function (e) {
2+
var n = {};
3+
function o(r) {
4+
if (n[r]) {
5+
return n[r].exports;
6+
}
7+
var a = (n[r] = {
8+
i: r,
9+
l: false,
10+
exports: {},
11+
});
12+
e[r].call(a.exports, a, a.exports, o);
13+
a.l = true;
14+
return a.exports;
15+
}
16+
o.p = '';
17+
o((o.s = 386));
18+
})({
19+
'./\\..\\node_modules\\debug\\src\\index': function (e, t, n) {},
20+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
WebpackBundle {
2+
"entryId": "386",
3+
"modules": Map {
4+
"./\..\node_modules\debug\src\index" => WebpackModule {
5+
"ast": ,
6+
"id": "./\..\node_modules\debug\src\index",
7+
"isEntry": false,
8+
"path": "././\..\node_modules\debug\src\index.js",
9+
},
10+
},
11+
"type": "webpack",
12+
}

packages/webcrack/src/unpack/test/unpack.test.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as m from '@codemod/matchers';
22
import { readFile } from 'fs/promises';
33
import { tmpdir } from 'os';
4-
import { join } from 'path';
4+
import { join, sep } from 'path';
55
import { expect, test } from 'vitest';
66
import { unpack } from '../index';
77

@@ -26,7 +26,7 @@ test('path mapping', async () => {
2626
expect(bundle!).toMatchSnapshot();
2727
});
2828

29-
test('prevent path traversal', async () => {
29+
test.runIf(sep === '/')('prevent path traversal (posix)', async () => {
3030
const code = await readFile(
3131
join(SAMPLES_DIR, 'webpack-path-traversal.js'),
3232
'utf8',
@@ -37,3 +37,15 @@ test('prevent path traversal', async () => {
3737
const dir = join(tmpdir(), 'path-traversal-test');
3838
await expect(bundle!.save(dir)).rejects.toThrow('path traversal');
3939
});
40+
41+
test.runIf(sep === '\\')('prevent path traversal (windows)', async () => {
42+
const code = await readFile(
43+
join(SAMPLES_DIR, 'webpack-path-traversal-windows.js'),
44+
'utf8',
45+
);
46+
const bundle = unpack(code);
47+
expect(bundle).toBeDefined();
48+
49+
const dir = join(tmpdir(), 'path-traversal-test');
50+
await expect(bundle!.save(dir)).rejects.toThrow('path traversal');
51+
});

0 commit comments

Comments
 (0)