Error thrown when escaping front slashes in regex patterns that have already been escaped
nickytonline opened this issue · comments
Describe the bug
There is logic in the edge bundler that escapes front slashes for regexes, but doesn’t take into account if they are already escaped.
See
edge-bundler/node/declaration.ts
Line 134 in 9d798de
So basically
Lines 183 to 197 in 9d798de
runs and parsePattern
converts
"^(?:\\/(_next\\/data\\/[^/]{1,}))?(?:\\/([^/.]{1,}))\\/api(?:\\/((?:[^\\/#\\?]+?)(?:\\/(?:[^\\/#\\?]+?))*))?(.json)?[\\/#\\?]?$"
to
"^(?:\\\\/(_next\\\\/data\\\\/[^\\/]{1,}))?(?:\\\\/([^\\/.]{1,}))\\\\/api(?:\\\\/((?:[^\\\\/#\\?]+?)(?:\\\\/(?:[^\\\\/#\\?]+?))*))?(.json)?[\\\\/#\\?]?$"
which results in the following error
Unexpected token: "/" at 1:7.
◈ Rewrote URL to /static/chunks/pages/shows/static/[id]-52e80ca069ee1610.js
Function 'next_middleware' uses an unsupported regular expression and will not be invoked:
/^(?:\\/(_next\\/data\\/[^\/]{1,}))?(?:\\/([^\/.]{1,}))?\\/api(?:\\/((?:[^\\/#\?]+?)(?:\\/(?:[^\\/#\?]+?))*))?(.json)?[\\/#\?]?$/
because there is an extra escaped backslash added.
In our scenario on the frameworks team, the failing regular expressions are typicially generated from the Next.js framework.
The first question I have is do we need to normalize these regex patterns at the edge bundler level?
const normalizedPattern = pattern.replace(/\//g, '\\/')
In user and framework land, if they weren't escaped properly, I would expect it to fail if I did not escape things properly. Is there a use case on the edge functions team for this? I would assume so given the code was added. 😎
For more context see https://netlify.slack.com/archives/C01TKAEBP3Q/p1682545091365449
Configuration
Please enter the following command in a terminal and copy/paste its output:
npx envinfo --system --binaries
System:
OS: macOS 13.3.1
CPU: (10) arm64 Apple M1 Pro
Memory: 139.58 MB / 32.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 18.15.0 - ~/.n/bin/node
Yarn: 1.22.19 - ~/.n/bin/yarn
npm: 9.5.0 - ~/.n/bin/npm
Pull requests
Pull requests are welcome! If you would like to help us fix this bug, please check our
contributions guidelines.
Hey @nickytonline, this appears to be done - do you mind confirming and closing if so? Thanks!
Work on this is complete.