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

Gatsby serve SSR does not resolve encoded url and throws error #39010

Open
2 tasks done
emkeythekeyem opened this issue Jun 6, 2024 · 3 comments · May be fixed by #39157
Open
2 tasks done

Gatsby serve SSR does not resolve encoded url and throws error #39010

emkeythekeyem opened this issue Jun 6, 2024 · 3 comments · May be fixed by #39157
Labels
status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer type: bug An issue or pull request relating to a bug in Gatsby

Comments

@emkeythekeyem
Copy link

emkeythekeyem commented Jun 6, 2024

Preliminary Checks

Description

When serving a Gatsby site with server-side rendering (SSR), encoded URLs are not correctly translated, resulting in a 500 internal server error.

TypeError: Cannot destructure property 'params' of '(0 , _reachRouter.match)(...)' as it is null.

Reproduction Link

CODESANDBOX

Steps to Reproduce

  1. bottom terminal do a yarn build and then a yarn serve
  2. right page go to APP-LINK/ and it shows the console log and it's working, now go to https://yrg5ww-9000.csb.app/test-d%27alviano and it shows my error
Screenshot 2024-06-06 at 13 06 48 Screenshot 2024-06-06 at 13 06 58

Expected Result

Translating the url and showing the exact thing is showing with the decoded url, this does not happen if i am not using gatsby serve & ssr.

Actual Result

TypeError: Cannot destructure property 'params' of '(0 , _reachRouter.match)(...)' as it is null.

Environment

System:
    OS: Linux 6.1 Ubuntu 20.04.6 LTS (Focal Fossa)
    CPU: (2) x64 AMD EPYC
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 20.12.1 - /tmp/yarn--1717672196079-0.3539445966095174/node
    Yarn: 1.22.19 - /tmp/yarn--1717672196079-0.3539445966095174/yarn
    npm: 10.5.0 - /home/codespace/nvm/current/bin/npm
  Languages:
    Python: 3.10.13 - /home/codespace/.python/current/bin/python
  npmPackages:
    gatsby: ^5.13.5 => 5.13.5

Config Flags

DEV_SSR: true

@emkeythekeyem emkeythekeyem added the type: bug An issue or pull request relating to a bug in Gatsby label Jun 6, 2024
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Jun 6, 2024
@emmron
Copy link

emmron commented Nov 2, 2024

@emkeythekeyem Fixed

@capndesign
Copy link

Looks like this has the same root cause as #38253. @emmron I see you had a proposed fix, but closed it. Any suggestions on workarounds in our projects?

emmron added a commit to emmron/gatsby-issue-25207-fix that referenced this issue Nov 7, 2024
@emmron emmron linked a pull request Nov 7, 2024 that will close this issue
emmron added a commit to emmron/gatsby-issue-25207-fix that referenced this issue Nov 7, 2024
- Replace single quotes with backticks for consistency
- Add explicit express.RequestHandler return type
- Fix prettier formatting for template literals
- Improve code formatting and readability
emmron added a commit to emmron/gatsby-issue-25207-fix that referenced this issue Nov 7, 2024
emmron added a commit to emmron/gatsby-issue-25207-fix that referenced this issue Nov 7, 2024
- Replace single quotes with backticks for consistency
- Add explicit express.RequestHandler return type
- Fix prettier formatting for template literals
- Improve code formatting and readability
@emmron
Copy link

emmron commented Nov 7, 2024

@

Looks like this has the same root cause as #38253. @emmron I see you had a proposed fix, but closed it. Any suggestions on workarounds in our projects?

@capndesign

emmron added a commit to emmron/gatsby-issue-25207-fix that referenced this issue Nov 7, 2024
fix(ssr): Improve URL sanitization and routing in serve.js for SSR

- Enhance `sanitizeUrl` function to comprehensively handle nested encodings, control characters, query parameters, hash fragments, and path traversal.
- Add iterative decoding in `sanitizeUrl` to support URLs with multiple encoding layers.
- Normalize URLs by stripping extraneous characters, collapsing redundant slashes, ensuring a leading slash, and conditionally removing trailing slashes.
- Implement `createMatchPathMiddleware` with modularized helpers:
  - `sanitizeUrl`: Cleans and normalizes URL paths.
  - `findMatchPath`: Safely finds a matching path with error handling.
  - `logMatch`: Logs match details if logging is enabled.
  - `handleError`: Centralized error handler for consistent logging and responses.
- Add caching for URL matches to improve performance.
- Add informative logs for matched paths, request durations, and errors.
- Ensure compatibility with various URL structures and improve security against path traversal vulnerabilities.
- Improve maintainability by breaking down the code into modular, reusable functions.

This commit addresses [Issue gatsbyjs#39010](gatsbyjs#39010) and enhances SSR handling for encoded URLs, improving both performance and robustness in the `serve.js` middleware.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer type: bug An issue or pull request relating to a bug in Gatsby
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants