Skip to content

Recommend router-based route protection over pathname-string auth checks - #14432

Merged
matthewp merged 3 commits into
mainfrom
docs/auth-pathname-guidance
Aug 21, 2026
Merged

Recommend router-based route protection over pathname-string auth checks#14432
matthewp merged 3 commits into
mainfrom
docs/auth-pathname-guidance

Conversation

@matthewp

Copy link
Copy Markdown
Contributor

Description (required)

In the path we have documenting using patterns like context.url.pathname.startsWith('/dashboard') to only apply auth requirements to certain routes. This had led to numerous security issues such as double-encoded URLs, base-stripped URLs not matching the string check, etc. It was a mistake for Astro to make Astro.url by anything other than the raw request URL. We have attempted to "normalize" this value for users to match what they expect. If the user is going to the dashboard.astro route they expect the URL to also be /dashboard. But this means that Astro has taken on the burden of the pathname not actually matching user expectations.

I hope to make a breaking change to how Astro.url works in 8.0 so that we no longer normalize the value for the user at all. In the mean time I want to immediately move away from documenting a bad practice. Instead if you want to apply middleware to certain routes, use a router like Hono.

This updates the docs both to show the preferred method, as well as to discourage the old.

References

Not related to any pull-requests or Astro issues.

@astrobot-houston

astrobot-houston commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Lunaria Status Overview

🌕 This pull request will trigger status changes.

Learn more

By default, every PR changing files present in the Lunaria configuration's files property will be considered and trigger status changes accordingly.

You can change this by adding one of the keywords present in the ignoreKeywords property in your Lunaria configuration file in the PR's title (ignoring all files) or by including a tracker directive in the merged commit's description.

Tracked Files

File Note
en/guides/authentication.mdx Source changed, localizations will be marked as outdated.
en/guides/routing.mdx Source changed, localizations will be marked as outdated.
Warnings reference
Icon Description
🔄️ The source for this localization has been updated since the creation of this pull request, make sure all changes in the source have been applied.

@github-actions

github-actions Bot commented Aug 19, 2026

Copy link
Copy Markdown

Preview deployment

✅ Deployment complete!

@matthewp
matthewp marked this pull request as ready for review August 19, 2026 18:17

@ArmandPhilippot ArmandPhilippot left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I left two suggestions regarding the caution, which may be a bit long for a single paragraph (especially the first one).

Otherwise, LGTM! And, you plan for v8 makes sense to me! 👍🏽

Comment thread src/content/docs/en/guides/authentication.mdx Outdated
Comment thread src/content/docs/en/guides/routing.mdx Outdated
@ArmandPhilippot ArmandPhilippot added the improve or update documentation Enhance / update existing documentation (e.g. add example, improve description, update for changes) label Aug 19, 2026
matthewp and others added 2 commits August 19, 2026 16:54
Co-authored-by: Armand Philippot <git@armand.philippot.eu>
Co-authored-by: Armand Philippot <git@armand.philippot.eu>
@matthewp
matthewp merged commit 741c1b8 into main Aug 21, 2026
9 checks passed
@matthewp
matthewp deleted the docs/auth-pathname-guidance branch August 21, 2026 13:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

improve or update documentation Enhance / update existing documentation (e.g. add example, improve description, update for changes)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants