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

Rewrite image urls to exclude unwanted params #86

Merged
merged 5 commits into from
Nov 7, 2023
Merged

Conversation

pookmish
Copy link
Contributor

@pookmish pookmish commented Nov 3, 2023

READY FOR REVIEW/NOT READY

  • (Edit the above to reflect status)

Summary

  • TL;DR - what's this PR for?

Review By (Date)

  • When does this need to be reviewed by?

Urgency

  • How critical is this PR?

Steps to Test

  1. Do this
  2. Then this
  3. Then this

Affected Projects or Products

  • Does this PR impact any particular projects, products, or modules?

Associated Issues and/or People

  • JIRA ticket
  • Other PRs
  • Any other contextual information that might be helpful (e.g., description of a bug that this PR fixes, new functionality that it adds, etc.)
  • Anyone who should be notified? (@mention them here)

See Also

Copy link

vercel bot commented Nov 3, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
su-library ✅ Ready (Inspect) Visit Preview Nov 7, 2023 3:16am

next.config.js Outdated
{
source: '/_next/image',
destination: '/_next/image?url=:url',
has: [{type: 'query', key: 'url', value: '(?<url>.*[jpg|png|jpeg|gif]\?itok=\\w+).*'}]
Copy link
Member

Choose a reason for hiding this comment

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

Looks smart.

Any exotic files like (webp, SVG)?

Is itok always the first parameter after the ??

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no webp, or svg uploads are allowed.
It should always be the first parameter if the data comes from Drupal.. i guess this probably wouldn't help it if bots remove the itok param

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added some logic to also check for image urls without the itok param. It'll return a default image as a fallback.
A few scenarios:

  • https://domain.edu/_next/image?url=https://google..... -> this will automatically be blocked due to the images.domains settings only allowing configured domains.
  • https://domain.edu/_next/image?url=https://drupal/path/to/image.jpg -> this will go to the no-image since drupal will block it anyways without the itok param
  • https://domain.edu/_next/image?url=https://drupal/path/to/image.jpg?itok=abc-12_3-> this will not be changed.
  • https://domain.edu/_next/image?url=https://drupal/path/to/image.jpg?foo=bar&itok=abc-12_3 -> this will go to no-image since it's likely malicious.
  • https://domain.edu/_next/image?url=https://drupal/path/to/image.jpg?itok=abc-12_3&foo=bar... -> this will rewrite to https://domain.edu/_next/image?url=https://drupal/path/to/image.jpg?itok=abc-12_3. This doesn't block bots, but it should reduce the variety of unique images being derived.

Can you think of any other scenarios?

Copy link
Member

Choose a reason for hiding this comment

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

I think that should cover it. In a nutshell, you're looking for a valid-looking URL with only the parameters you want and everything else gets sent to the no-image file, which will be cached after the first time it's hit. Bots could still troll valid image paths and their derivatives but that is a different issue than cache-busting URLs.

@pookmish pookmish merged commit 4d340eb into 1.x Nov 7, 2023
1 of 2 checks passed
@pookmish pookmish deleted the rewrite-image-urls branch November 7, 2023 16:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants