Skip to content

Commit 3172dcd

Browse files
committed
escape distDir in regex
1 parent ab7bf85 commit 3172dcd

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

packages/nextjs/src/index.server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { RewriteFrames } from '@sentry/integrations';
22
import { configureScope, getCurrentHub, init as nodeInit, Integrations } from '@sentry/node';
3-
import { logger } from '@sentry/utils';
3+
import { escapeStringForRegex, logger } from '@sentry/utils';
44

55
import { instrumentServer } from './utils/instrumentServer';
66
import { MetadataBuilder } from './utils/metadataBuilder';
@@ -47,7 +47,7 @@ function sdkAlreadyInitialized(): boolean {
4747
}
4848

4949
// webpack will replace this placeholder at build time
50-
const SOURCEMAP_FILENAME_REGEX = new RegExp('__rewriteFramesDistDir__');
50+
const SOURCEMAP_FILENAME_REGEX = new RegExp(escapeStringForRegex('__rewriteFramesDistDir__'));
5151

5252
const defaultRewriteFramesIntegration = new RewriteFrames({
5353
iteratee: frame => {

packages/nextjs/test/config.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,9 @@ describe('webpack config', () => {
361361
);
362362
});
363363
describe('`RewriteFrames` ends up with correct `distDir` value', () => {
364-
// TODO: this, along with any number of other parts of the build process, should be tested with an integration test
364+
// TODO: this, along with any number of other parts of the build process, should be tested with an integration
365+
// test (and that integration test should test custom `distDir` values with and without a `.`, to make sure the
366+
// regex escaping is working)
365367
});
366368
});
367369
});

packages/utils/src/string.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,20 @@ export function isMatchingPattern(value: string, pattern: RegExp | string): bool
101101
}
102102
return false;
103103
}
104+
105+
/**
106+
* Given a string, escape characters which have meaning in the regex grammar, such that the result is safe to feed to
107+
* `new RegExp()`.
108+
*
109+
* Based on https://github.com/sindresorhus/escape-string-regexp. Vendored to a) reduce the size by skipping the runtime
110+
* type-checking, and b) ensure it gets down-compiled for old versions of Node (the published package only supports Node
111+
* 12+).
112+
*
113+
* @param regexString The string to escape
114+
* @returns An version of the string with all special regex characters escaped
115+
*/
116+
export function escapeStringForRegex(regexString: string): string {
117+
// escape the hyphen separately so we can also replace it with a unicode literal hyphen, to avoid the problems
118+
// discussed in https://github.com/sindresorhus/escape-string-regexp/issues/20.
119+
return regexString.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d');
120+
}

0 commit comments

Comments
 (0)