Skip to content

Commit a4ae291

Browse files
authored
fix(utils): dirname and basename should handle Windows paths (#8737)
The Electron SDK uses `basename` to get the file name without path but this returned the full path on Windows. This PR adds Windows support to the regex and adds some tests.
1 parent fac2be8 commit a4ae291

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

packages/utils/src/path.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,13 @@ function normalizeArray(parts: string[], allowAboveRoot?: boolean): string[] {
5151

5252
// Split a filename into [root, dir, basename, ext], unix version
5353
// 'root' is just a slash, or nothing.
54-
const splitPathRe = /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^/]+?|)(\.[^./]*|))(?:[/]*)$/;
54+
const splitPathRe = /^(\S+:\\|\/?)([\s\S]*?)((?:\.{1,2}|[^/\\]+?|)(\.[^./\\]*|))(?:[/\\]*)$/;
5555
/** JSDoc */
5656
function splitPath(filename: string): string[] {
57-
const parts = splitPathRe.exec(filename);
57+
// Truncate files names greater than 1024 characters to avoid regex dos
58+
// https://github.com/getsentry/sentry-javascript/pull/8737#discussion_r1285719172
59+
const truncated = filename.length > 1024 ? `<truncated>${filename.slice(-1024)}` : filename;
60+
const parts = splitPathRe.exec(truncated);
5861
return parts ? parts.slice(1) : [];
5962
}
6063

packages/utils/test/path.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { basename, dirname } from '../src/path';
2+
3+
describe('path', () => {
4+
describe('basename', () => {
5+
test('unix', () => {
6+
expect(basename('/foo/bar/baz/asdf/quux.html')).toEqual('quux.html');
7+
expect(basename('foo/bar/baz/asdf/quux.html')).toEqual('quux.html');
8+
expect(basename('../baz/asdf/quux.html')).toEqual('quux.html');
9+
expect(basename('quux.html')).toEqual('quux.html');
10+
});
11+
test('windows', () => {
12+
expect(basename('c:\\foo\\bar\\baz\\asdf\\quux.html')).toEqual('quux.html');
13+
expect(basename('\\foo\\bar\\baz\\asdf\\quux.html')).toEqual('quux.html');
14+
expect(basename('..\\bar\\baz\\asdf\\quux.html')).toEqual('quux.html');
15+
expect(basename('quux.html')).toEqual('quux.html');
16+
});
17+
});
18+
19+
describe('dirname', () => {
20+
test('unix', () => {
21+
expect(dirname('/foo/bar/baz/asdf/quux.html')).toEqual('/foo/bar/baz/asdf');
22+
expect(dirname('foo/bar/baz/asdf/quux.html')).toEqual('foo/bar/baz/asdf');
23+
expect(dirname('../baz/asdf/quux.html')).toEqual('../baz/asdf');
24+
expect(dirname('/quux.html')).toEqual('/');
25+
});
26+
test('windows', () => {
27+
expect(dirname('C:\\foo\\bar\\baz\\asdf\\quux.html')).toEqual('C:\\foo\\bar\\baz\\asdf');
28+
expect(dirname('\\foo\\bar\\baz\\asdf\\quux.html')).toEqual('\\foo\\bar\\baz\\asdf');
29+
expect(dirname('..\\bar\\baz\\asdf\\quux.html')).toEqual('..\\bar\\baz\\asdf');
30+
expect(dirname('quux.html')).toEqual('.');
31+
});
32+
});
33+
});

0 commit comments

Comments
 (0)