Skip to content

fix: use allowlist for remote images #330

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 1 commit into from
May 20, 2021
Merged

fix: use allowlist for remote images #330

merged 1 commit into from
May 20, 2021

Conversation

ascorbic
Copy link
Contributor

This PR adds an allowlist for remote images, to prevent the function being used to proxy arbitrary images. The standard way to support this is by setting a value in next.config.js, however we can't do this as we don't have access to this in the function. Instead we use an env variable and warn if the user sets the value in config instead.

This also adds docs for this and for the WebP env var

@github-actions github-actions bot added the type: bug code to address defects in shipped code label May 20, 2021
@ascorbic ascorbic requested a review from lindsaylevine May 20, 2021 07:04
@ascorbic ascorbic enabled auto-merge May 20, 2021 07:21
Copy link

@lindsaylevine lindsaylevine left a comment

Choose a reason for hiding this comment

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

just the one link otherwise 👍

`Image domains set in next.config.js are ignored.\nPlease set the env variable NEXT_IMAGE_ALLOWED_DOMAINS to "${nextConfig.images.domains.join(
',',
)}" instead`,
)

Choose a reason for hiding this comment

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

noice


The plugin includes a function to generate images for `next/image`. The images are resized on the fly, so the first request will have a short delay. However because the function uses [On-Demand Builders](https://docs.netlify.com/configure-builds/on-demand-builders/), any subsequent requests for that image are served from the edge cache and are super-fast.

By default, images are returned in the same format as the source image if they are in JPEG, PNG, WebP or AVIF format. If you are only targeting modern browsers and want to live life on the edge, you can [set the environment variable](https://docs.netlify.com/configure-builds/environment-variables/) `FORCE_WEBP_OUTPUT` to `"true"`, and it will return all images in WebP format. This will often lead to significant improvements in file size. However you should not use this if you need to support older browsers, as `next/image` does not support picture tag source fallback and images will appear broken. Check [browser support](https://caniuse.com/webp) to see if you are happy to do this.

Choose a reason for hiding this comment

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

live life on the ledge lul


By default, images are returned in the same format as the source image if they are in JPEG, PNG, WebP or AVIF format. If you are only targeting modern browsers and want to live life on the edge, you can [set the environment variable](https://docs.netlify.com/configure-builds/environment-variables/) `FORCE_WEBP_OUTPUT` to `"true"`, and it will return all images in WebP format. This will often lead to significant improvements in file size. However you should not use this if you need to support older browsers, as `next/image` does not support picture tag source fallback and images will appear broken. Check [browser support](https://caniuse.com/webp) to see if you are happy to do this.

If you want to use remote images in `next/image`, you will need to add the image domains to an allow list. Setting them in `images.domains` in `next.config.js` is not supported: instead you should set the environment variable `NEXT_IMAGE_ALLOWED_DOMAINS` to a comma-separated list of domains, e.g. `NEXT_IMAGE_ALLOWED_DOMAINS="placekitten.com,unsplash.com"`.

Choose a reason for hiding this comment

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

o

@@ -126,6 +126,14 @@ Similarly, you can customize your publish directory in your `netlify.toml` file:

Read more about [Netlify's build settings](https://docs.netlify.com/configure-builds/get-started/#basic-build-settings) in our docs.

## Image handling

Choose a reason for hiding this comment

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

do you wanna add an anchor to this with the other section headings at the top? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug code to address defects in shipped code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants