Skip to content

Redirect requests with repeated slashes #863

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

Merged
merged 5 commits into from
May 18, 2025

Conversation

dnewbound0
Copy link
Contributor

Let's say you had the following middleware function:

export async function middleware(request: NextRequest) {
  return NextResponse.redirect(new URL("/foo", request.url))
}

A request to https://yourdomain.com//evil.com would result in request.url === "https://evil.com", leading to a malicious redirect. Here we redirect these bad requests containing repeated slashes to a sanitised path.

Copy link

changeset-bot bot commented May 16, 2025

🦋 Changeset detected

Latest commit: d998bb8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@opennextjs/aws Patch
app-pages-router Patch
app-router Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@conico974
Copy link
Contributor

@dnewbound0 I'm sorry but i don't understand what you're trying to solve here.
If you had this in your middleware, it would redirect to https://yourdomain.com/foo, that's the case for me on my test.
And modifying handleRedirects have absolutely no effect since middleware redirect won't go there at all

@dnewbound0
Copy link
Contributor Author

@dnewbound0 I'm sorry but i don't understand what you're trying to solve here. If you had this in your middleware, it would redirect to https://yourdomain.com/foo, that's the case for me on my test. And modifying handleRedirects have absolutely no effect since middleware redirect won't go there at all

(i've replied to this in the OpenNext discord already but i figured i'd post my response here as well for posterity)

i can recreate this using OpenNext v3.6.1, SST v2.48.5, and Next.js v14.2.28. making a request to http://yourdomain.com//evil.com gives an internal event where the raw path is //evil.com. as far as i understand it that means

const initialUrl = new URL(normalizedPath, internalEvent.url);
evaluates to new URL("//evil.com", "http://yourdomain.com//evil.com") so you're going to a get a middleware invocation where request.url === "http://evil.com".

the PR isn't trying to change any functionality about redirecting within the middleware function, but to avoid calling the middleware entirely with that bad path. so it would go:

  1. request to http://yourdomain.com//evil.com
  2. before the middleware is invoked, redirect to http://yourdomain.com/evil.com
  3. middleware is invoked like normal

as far as i can tell, this is how the default NextJS server handles this case?

apologies if i've missed something obvious here, this is my first PR and i'm just trying to resolve something that i noticed in our deployment 😅

Copy link
Contributor

@conico974 conico974 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

pkg-pr-new bot commented May 17, 2025

Open in StackBlitz

pnpm add https://pkg.pr.new/@opennextjs/aws@863

commit: d998bb8

Copy link
Contributor

@conico974 conico974 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just 2 little nit, after that we should be good

Copy link
Contributor

@conico974 conico974 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR

@conico974 conico974 merged commit d434708 into opennextjs:main May 18, 2025
3 checks passed
@dnewbound0 dnewbound0 deleted the feat/repeated-slash-redirect branch May 20, 2025 00:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants