Skip to content

Commit

Permalink
fix: ensure patterns match on whole path (netlify/edge-bundler#442)
Browse files Browse the repository at this point in the history
  • Loading branch information
Skn0tt authored Jul 27, 2023
1 parent 18596d7 commit 5600df9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
8 changes: 8 additions & 0 deletions packages/edge-bundler/node/declaration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,11 @@ test('Escapes front slashes in a regex pattern', () => {

expect(actual).toEqual(expected)
})

test('Ensures pattern match on the whole path', () => {
const regexPattern = '/foo/.*/bar'
const expected = '^\\/foo\\/.*\\/bar$'
const actual = parsePattern(regexPattern)

expect(actual).toEqual(expected)
})
6 changes: 5 additions & 1 deletion packages/edge-bundler/node/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ const createDeclarationsFromFunctionConfigs = (
// Validates and normalizes a pattern so that it's a valid regular expression
// in Go, which is the engine used by our edge nodes.
export const parsePattern = (pattern: string) => {
const regexp = new RegExp(pattern)
let enclosedPattern = pattern
if (!pattern.startsWith('^')) enclosedPattern = `^${enclosedPattern}`
if (!pattern.endsWith('$')) enclosedPattern = `${enclosedPattern}$`

const regexp = new RegExp(enclosedPattern)
const newRegexp = regexpAST.transform(regexp, {
Assertion(path) {
// Lookaheads are not supported. If we find one, throw an error.
Expand Down

0 comments on commit 5600df9

Please sign in to comment.