Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: front slashes only get escaped now if they aren't already #376

Closed
Closed
Show file tree
Hide file tree
Changes from 3 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
27 changes: 26 additions & 1 deletion node/declaration.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { test, expect } from 'vitest'

import { FunctionConfig } from './config.js'
import { Declaration, mergeDeclarations } from './declaration.js'
import { Declaration, mergeDeclarations, parsePattern } from './declaration.js'

const deployConfigDeclarations: Declaration[] = []

Expand Down Expand Up @@ -191,3 +191,28 @@ test('netlify.toml-defined excludedPath are respected', () => {

expect(declarations).toEqual(expectedDeclarations)
})

test('throws if there are lookaheads in a regex pattern', () => {
nickytonline marked this conversation as resolved.
Show resolved Hide resolved
const regexPattern =
'^(?:\\/(_next\\/data\\/[^/]{1,}))?(?:\\/([^/.]{1,}))\\/shows(?:\\/((?!99|88).*))(.json)?[\\/#\\?]?$'

expect(() => {
parsePattern(regexPattern)
}).toThrow('Regular expressions with lookaheads are not supported')
})

test('does not escape front slashes in a regex pattern if they are already escaped', () => {
nickytonline marked this conversation as resolved.
Show resolved Hide resolved
const regexPattern = '^(?:\\/(_next\\/data\\/[^/]{1,}))?(?:\\/([^/.]{1,}))\\/shows(?:\\/(.*))(.json)?[\\/#\\?]?$'
const expected = '^(?:\\/(_next\\/data\\/[^\\/]{1,}))?(?:\\/([^\\/.]{1,}))\\/shows(?:\\/(.*))(.json)?[\\/#\\?]?$'
const actual = parsePattern(regexPattern)

expect(actual).toEqual(expected)
})

test('escapes front slashes in a regex pattern', () => {
nickytonline marked this conversation as resolved.
Show resolved Hide resolved
const regexPattern = '^(?:/(_next/data/[^/]{1,}))?(?:/([^/.]{1,}))/shows(?:/(.*))(.json)?[/#\\?]?$'
const expected = '^(?:\\/(_next\\/data\\/[^\\/]{1,}))?(?:\\/([^\\/.]{1,}))\\/shows(?:\\/(.*))(.json)?[\\/#\\?]?$'
const actual = parsePattern(regexPattern)

expect(actual).toEqual(expected)
})
5 changes: 3 additions & 2 deletions node/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ 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) => {
// Escaping forward slashes with back slashes.
const normalizedPattern = pattern.replace(/\//g, '\\/')
// only escape front slashes if they haven't been escaped already
nickytonline marked this conversation as resolved.
Show resolved Hide resolved
const normalizedPattern = pattern.replace(/(?<!\\)\//g, '\\/')

const regex = regexpAST.transform(`/${normalizedPattern}/`, {
Assertion(path) {
// Lookaheads are not supported. If we find one, throw an error.
Expand Down