From 01e13c4c8bb24f0d76884a2cc806a511674d6fbb Mon Sep 17 00:00:00 2001 From: Nick Taylor Date: Thu, 27 Apr 2023 11:57:32 -0400 Subject: [PATCH 1/7] fix: front slashes only get escaped now if they aren't already --- node/declaration.test.ts | 27 ++++++++++++++++++++++++++- node/declaration.ts | 5 +++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/node/declaration.test.ts b/node/declaration.test.ts index ca700b26..42f15dea 100644 --- a/node/declaration.test.ts +++ b/node/declaration.test.ts @@ -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[] = [] @@ -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', () => { + 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', () => { + 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 if they are not already escaped', () => { + const regexPattern = '^(?:/(_next/data/[^/]{1,}))?(?:/([^/.]{1,}))/shows(?:/(.*))(.json)?[/#\\?]?$' + const expected = '^(?:\\/(_next\\/data\\/[^\\/]{1,}))?(?:\\/([^\\/.]{1,}))\\/shows(?:\\/(.*))(.json)?[/#\\?]?$' + const actual = parsePattern(regexPattern) + + expect(actual).toEqual(expected) +}) diff --git a/node/declaration.ts b/node/declaration.ts index 143645af..02dfef6c 100644 --- a/node/declaration.ts +++ b/node/declaration.ts @@ -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 + const normalizedPattern = pattern.replace(/(? Date: Thu, 27 Apr 2023 12:10:49 -0400 Subject: [PATCH 2/7] chore: updated test description --- node/declaration.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node/declaration.test.ts b/node/declaration.test.ts index 42f15dea..09927ca6 100644 --- a/node/declaration.test.ts +++ b/node/declaration.test.ts @@ -209,7 +209,7 @@ test('does not escape front slashes in a regex pattern if they are already escap expect(actual).toEqual(expected) }) -test('escapes front slashes in a regex pattern if they are not already escaped', () => { +test('escapes front slashes in a regex pattern', () => { const regexPattern = '^(?:/(_next/data/[^/]{1,}))?(?:/([^/.]{1,}))/shows(?:/(.*))(.json)?[/#\\?]?$' const expected = '^(?:\\/(_next\\/data\\/[^\\/]{1,}))?(?:\\/([^\\/.]{1,}))\\/shows(?:\\/(.*))(.json)?[/#\\?]?$' const actual = parsePattern(regexPattern) From 715a986deaa6bd7f527bd272ff54dfba73539c91 Mon Sep 17 00:00:00 2001 From: Nick Taylor Date: Thu, 27 Apr 2023 14:40:31 -0400 Subject: [PATCH 3/7] chore: cleaned up logic --- node/declaration.test.ts | 2 +- node/declaration.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/node/declaration.test.ts b/node/declaration.test.ts index 09927ca6..516ead7d 100644 --- a/node/declaration.test.ts +++ b/node/declaration.test.ts @@ -211,7 +211,7 @@ test('does not escape front slashes in a regex pattern if they are already escap test('escapes front slashes in a regex pattern', () => { const regexPattern = '^(?:/(_next/data/[^/]{1,}))?(?:/([^/.]{1,}))/shows(?:/(.*))(.json)?[/#\\?]?$' - const expected = '^(?:\\/(_next\\/data\\/[^\\/]{1,}))?(?:\\/([^\\/.]{1,}))\\/shows(?:\\/(.*))(.json)?[/#\\?]?$' + const expected = '^(?:\\/(_next\\/data\\/[^\\/]{1,}))?(?:\\/([^\\/.]{1,}))\\/shows(?:\\/(.*))(.json)?[\\/#\\?]?$' const actual = parsePattern(regexPattern) expect(actual).toEqual(expected) diff --git a/node/declaration.ts b/node/declaration.ts index 02dfef6c..36a3ec05 100644 --- a/node/declaration.ts +++ b/node/declaration.ts @@ -131,7 +131,7 @@ const createDeclarationsFromFunctionConfigs = ( // in Go, which is the engine used by our edge nodes. export const parsePattern = (pattern: string) => { // only escape front slashes if they haven't been escaped already - const normalizedPattern = pattern.replace(/(? Date: Fri, 28 Apr 2023 10:46:13 -0400 Subject: [PATCH 4/7] Update node/declaration.test.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Eduardo Bouças --- node/declaration.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node/declaration.test.ts b/node/declaration.test.ts index 516ead7d..31312d45 100644 --- a/node/declaration.test.ts +++ b/node/declaration.test.ts @@ -209,7 +209,7 @@ test('does not escape front slashes in a regex pattern if they are already escap expect(actual).toEqual(expected) }) -test('escapes front slashes in a regex pattern', () => { +test('Escapes front slashes in a regex pattern', () => { const regexPattern = '^(?:/(_next/data/[^/]{1,}))?(?:/([^/.]{1,}))/shows(?:/(.*))(.json)?[/#\\?]?$' const expected = '^(?:\\/(_next\\/data\\/[^\\/]{1,}))?(?:\\/([^\\/.]{1,}))\\/shows(?:\\/(.*))(.json)?[\\/#\\?]?$' const actual = parsePattern(regexPattern) From e8366f1552e2c7d2e890b9be99fbee1c2da22b34 Mon Sep 17 00:00:00 2001 From: Nick Taylor Date: Fri, 28 Apr 2023 10:46:22 -0400 Subject: [PATCH 5/7] Update node/declaration.test.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Eduardo Bouças --- node/declaration.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node/declaration.test.ts b/node/declaration.test.ts index 31312d45..896da9f7 100644 --- a/node/declaration.test.ts +++ b/node/declaration.test.ts @@ -201,7 +201,7 @@ test('throws if there are lookaheads in a regex pattern', () => { }).toThrow('Regular expressions with lookaheads are not supported') }) -test('does not escape front slashes in a regex pattern if they are already escaped', () => { +test('Does not escape front slashes in a regex pattern if they are already escaped', () => { const regexPattern = '^(?:\\/(_next\\/data\\/[^/]{1,}))?(?:\\/([^/.]{1,}))\\/shows(?:\\/(.*))(.json)?[\\/#\\?]?$' const expected = '^(?:\\/(_next\\/data\\/[^\\/]{1,}))?(?:\\/([^\\/.]{1,}))\\/shows(?:\\/(.*))(.json)?[\\/#\\?]?$' const actual = parsePattern(regexPattern) From 719add61913d4ede784cb2ff93d9a82fbfbff94c Mon Sep 17 00:00:00 2001 From: Nick Taylor Date: Fri, 28 Apr 2023 10:48:28 -0400 Subject: [PATCH 6/7] chore: removed lookahead test as we cover this in another test already --- node/declaration.test.ts | 9 --------- 1 file changed, 9 deletions(-) diff --git a/node/declaration.test.ts b/node/declaration.test.ts index 896da9f7..59f59016 100644 --- a/node/declaration.test.ts +++ b/node/declaration.test.ts @@ -192,15 +192,6 @@ test('netlify.toml-defined excludedPath are respected', () => { expect(declarations).toEqual(expectedDeclarations) }) -test('throws if there are lookaheads in a regex pattern', () => { - 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', () => { const regexPattern = '^(?:\\/(_next\\/data\\/[^/]{1,}))?(?:\\/([^/.]{1,}))\\/shows(?:\\/(.*))(.json)?[\\/#\\?]?$' const expected = '^(?:\\/(_next\\/data\\/[^\\/]{1,}))?(?:\\/([^\\/.]{1,}))\\/shows(?:\\/(.*))(.json)?[\\/#\\?]?$' From 1a8bb5281db850d53eb7aca3693ecab70425461c Mon Sep 17 00:00:00 2001 From: Nick Taylor Date: Fri, 28 Apr 2023 10:49:24 -0400 Subject: [PATCH 7/7] Update node/declaration.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Eduardo Bouças --- node/declaration.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node/declaration.ts b/node/declaration.ts index 36a3ec05..9e284c3f 100644 --- a/node/declaration.ts +++ b/node/declaration.ts @@ -130,7 +130,7 @@ 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) => { - // only escape front slashes if they haven't been escaped already + // Only escape front slashes if they haven't been escaped already. const normalizedPattern = pattern.replace(/(?