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

Fix leaking internal config to user-defined loader prop in next/image #36013

Merged
merged 6 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 38 additions & 10 deletions packages/next/client/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,23 @@ type ImageConfig = ImageConfigComplete & { allSizes: number[] }
export type ImageLoader = (resolverProps: ImageLoaderProps) => string

export type ImageLoaderProps = {
config: Readonly<ImageConfig>
config?: Readonly<ImageConfig>
ijjk marked this conversation as resolved.
Show resolved Hide resolved
src: string
width: number
quality?: number
}

const loaders = new Map<LoaderValue, (props: ImageLoaderProps) => string>([
// Do not export - this is an internal type only
// because `next.config.js` is only meant for the
// built-in loaders, not for a custom loader() prop.
type ImageLoaderPropsWithConfig = ImageLoaderProps & {
config: Readonly<ImageConfig>
}

const loaders = new Map<
LoaderValue,
(props: ImageLoaderPropsWithConfig) => string
>([
['default', defaultLoader],
['imgix', imgixLoader],
['cloudinary', cloudinaryLoader],
Expand Down Expand Up @@ -269,7 +279,7 @@ function getInt(x: unknown): number | undefined {
return undefined
}

function defaultImageLoader(loaderProps: ImageLoaderProps) {
function defaultImageLoader(loaderProps: ImageLoaderPropsWithConfig) {
const loaderKey = loaderProps.config?.loader || 'default'
const load = loaders.get(loaderKey)
if (load) {
Expand Down Expand Up @@ -357,7 +367,6 @@ export default function Image({
objectPosition,
onLoadingComplete,
onError,
loader = defaultImageLoader,
placeholder = 'empty',
blurDataURL,
...all
Expand All @@ -376,8 +385,23 @@ export default function Image({
// Override default layout if the user specified one:
if (rest.layout) layout = rest.layout

// Remove property so it's not spread into image:
delete rest['layout']
// Remove property so it's not spread on <img>:
delete rest.layout
}

let loader = defaultImageLoader as ImageLoader
if ('loader' in rest) {
if (rest.loader) {
const customImageLoader = rest.loader
loader = (obj) => {
// The config object is internal only so we must
// delete before invoking the user-defined loader()
delete obj.config
return customImageLoader(obj)
}
}
// Remove property so it's not spread on <img>
delete rest.loader
}

let staticSrc = ''
Expand Down Expand Up @@ -957,7 +981,7 @@ function imgixLoader({
src,
width,
quality,
}: ImageLoaderProps): string {
}: ImageLoaderPropsWithConfig): string {
// Demo: https://static.imgix.net/daisy.png?auto=format&fit=max&w=300
const url = new URL(`${config.path}${normalizeSrc(src)}`)
const params = url.searchParams
Expand All @@ -973,7 +997,11 @@ function imgixLoader({
return url.href
}

function akamaiLoader({ config, src, width }: ImageLoaderProps): string {
function akamaiLoader({
config,
src,
width,
}: ImageLoaderPropsWithConfig): string {
return `${config.path}${normalizeSrc(src)}?imwidth=${width}`
}

Expand All @@ -982,7 +1010,7 @@ function cloudinaryLoader({
src,
width,
quality,
}: ImageLoaderProps): string {
}: ImageLoaderPropsWithConfig): string {
// Demo: https://res.cloudinary.com/demo/image/upload/w_300,c_limit,q_auto/turtles.jpg
const params = ['f_auto', 'c_limit', 'w_' + width, 'q_' + (quality || 'auto')]
const paramsString = params.join(',') + '/'
Expand All @@ -1001,7 +1029,7 @@ function defaultLoader({
src,
width,
quality,
}: ImageLoaderProps): string {
}: ImageLoaderPropsWithConfig): string {
if (process.env.NODE_ENV !== 'production') {
const missingValues = []

Expand Down
23 changes: 23 additions & 0 deletions test/integration/image-component/basic/pages/loader-prop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Image from 'next/image'

const LoaderExample = () => {
return (
<div>
<p>Custom loader in both next.config.js and loader prop</p>
<Image
id="loader-prop-img"
src="foo.jpg"
width={300}
height={400}
loader={({ config, src, width }) => {
if (config) {
return 'https://example.vercel.sh/error-unexpected-config'
}
return `https://example.vercel.sh/success/${src}?width=${width}`
}}
/>
</div>
)
}

export default LoaderExample
11 changes: 11 additions & 0 deletions test/integration/image-component/basic/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,17 @@ describe('Image Component Tests', () => {
await browser.elementById('basic-image').getAttribute('data-nimg')
).toBe('intrinsic')
})
it('should not pass config to custom loader prop', async () => {
browser = await webdriver(appPort, '/loader-prop')
expect(
await browser.elementById('loader-prop-img').getAttribute('src')
).toBe('https://example.vercel.sh/success/foo.jpg?width=1024')
expect(
await browser.elementById('loader-prop-img').getAttribute('srcset')
).toBe(
'https://example.vercel.sh/success/foo.jpg?width=480 1x, https://example.vercel.sh/success/foo.jpg?width=1024 2x'
)
})
})
describe('Client-side Image Component Tests', () => {
beforeAll(async () => {
Expand Down