Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/utils/src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ function normalizeArray(parts: string[], allowAboveRoot?: boolean): string[] {

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

Check failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data

This [regular expression](1) that depends on [library input](2) may run slow on strings with many repetitions of '.'. This [regular expression](1) that depends on [library input](3) may run slow on strings with many repetitions of '.'. This [regular expression](1) that depends on [library input](4) may run slow on strings with many repetitions of '.'. This [regular expression](1) that depends on [library input](5) may run slow on strings with many repetitions of '.'.
return parts ? parts.slice(1) : [];
}

Expand Down
33 changes: 33 additions & 0 deletions packages/utils/test/path.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { basename, dirname } from '../src/path';

describe('path', () => {
describe('basename', () => {
test('unix', () => {
expect(basename('/foo/bar/baz/asdf/quux.html')).toEqual('quux.html');
expect(basename('foo/bar/baz/asdf/quux.html')).toEqual('quux.html');
expect(basename('../baz/asdf/quux.html')).toEqual('quux.html');
expect(basename('quux.html')).toEqual('quux.html');
});
test('windows', () => {
expect(basename('c:\\foo\\bar\\baz\\asdf\\quux.html')).toEqual('quux.html');
expect(basename('\\foo\\bar\\baz\\asdf\\quux.html')).toEqual('quux.html');
expect(basename('..\\bar\\baz\\asdf\\quux.html')).toEqual('quux.html');
expect(basename('quux.html')).toEqual('quux.html');
});
});

describe('dirname', () => {
test('unix', () => {
expect(dirname('/foo/bar/baz/asdf/quux.html')).toEqual('/foo/bar/baz/asdf');
expect(dirname('foo/bar/baz/asdf/quux.html')).toEqual('foo/bar/baz/asdf');
expect(dirname('../baz/asdf/quux.html')).toEqual('../baz/asdf');
expect(dirname('/quux.html')).toEqual('/');
});
test('windows', () => {
expect(dirname('C:\\foo\\bar\\baz\\asdf\\quux.html')).toEqual('C:\\foo\\bar\\baz\\asdf');
expect(dirname('\\foo\\bar\\baz\\asdf\\quux.html')).toEqual('\\foo\\bar\\baz\\asdf');
expect(dirname('..\\bar\\baz\\asdf\\quux.html')).toEqual('..\\bar\\baz\\asdf');
expect(dirname('quux.html')).toEqual('.');
});
});
});