File tree Expand file tree Collapse file tree 7 files changed +88
-0
lines changed
packages/next/src/shared/lib
test/integration/next-image-new
invalid-src-leading-space
invalid-src-trailing-space Expand file tree Collapse file tree 7 files changed +88
-0
lines changed Original file line number Diff line number Diff line change @@ -465,6 +465,18 @@ export function getImgProps(
465
465
`Image with src "${ src } " has invalid "height" property. Expected a numeric value in pixels but received "${ height } ".`
466
466
)
467
467
}
468
+ // eslint-disable-next-line no-control-regex
469
+ if ( / ^ [ \x00 - \x20 ] / . test ( src ) ) {
470
+ throw new Error (
471
+ `Image with src "${ src } " cannot start with a space or control character. Use src.trimStart() to remove it or encodeURIComponent(src) to keep it.`
472
+ )
473
+ }
474
+ // eslint-disable-next-line no-control-regex
475
+ if ( / [ \x00 - \x20 ] $ / . test ( src ) ) {
476
+ throw new Error (
477
+ `Image with src "${ src } " cannot end with a space or control character. Use src.trimEnd() to remove it or encodeURIComponent(src) to keep it.`
478
+ )
479
+ }
468
480
}
469
481
}
470
482
if ( ! VALID_LOADING_VALUES . includes ( loading ) ) {
Original file line number Diff line number Diff line change
1
+ import React from 'react'
2
+ import Image from 'next/image'
3
+
4
+ export default function Page ( ) {
5
+ return (
6
+ < div >
7
+ < h2 > Invalid src with leading space</ h2 >
8
+ < Image src = " /test.jpg" width = { 200 } height = { 200 } />
9
+ </ div >
10
+ )
11
+ }
Original file line number Diff line number Diff line change
1
+ import React from 'react'
2
+ import Image from 'next/image'
3
+
4
+ export default function Page ( ) {
5
+ return (
6
+ < div >
7
+ < h2 > Invalid src with trailing space</ h2 >
8
+ < Image src = "/test.png " width = { 200 } height = { 200 } />
9
+ </ div >
10
+ )
11
+ }
Original file line number Diff line number Diff line change @@ -915,6 +915,22 @@ function runTests(mode) {
915
915
)
916
916
} )
917
917
918
+ it ( 'should show invalid src with leading space' , async ( ) => {
919
+ const browser = await webdriver ( appPort , '/invalid-src-leading-space' )
920
+ expect ( await hasRedbox ( browser ) ) . toBe ( true )
921
+ expect ( await getRedboxHeader ( browser ) ) . toContain (
922
+ 'Image with src " /test.jpg" cannot start with a space or control character.'
923
+ )
924
+ } )
925
+
926
+ it ( 'should show invalid src with trailing space' , async ( ) => {
927
+ const browser = await webdriver ( appPort , '/invalid-src-trailing-space' )
928
+ expect ( await hasRedbox ( browser ) ) . toBe ( true )
929
+ expect ( await getRedboxHeader ( browser ) ) . toContain (
930
+ 'Image with src "/test.png " cannot end with a space or control character.'
931
+ )
932
+ } )
933
+
918
934
it ( 'should show error when string src and placeholder=blur and blurDataURL is missing' , async ( ) => {
919
935
const browser = await webdriver ( appPort , '/invalid-placeholder-blur' )
920
936
Original file line number Diff line number Diff line change
1
+ import React from 'react'
2
+ import Image from 'next/image'
3
+
4
+ export default function Page ( ) {
5
+ return (
6
+ < div >
7
+ < h2 > Invalid src with leading space</ h2 >
8
+ < Image src = " /test.jpg" width = { 200 } height = { 200 } />
9
+ </ div >
10
+ )
11
+ }
Original file line number Diff line number Diff line change
1
+ import React from 'react'
2
+ import Image from 'next/image'
3
+
4
+ export default function Page ( ) {
5
+ return (
6
+ < div >
7
+ < h2 > Invalid src with trailing space</ h2 >
8
+ < Image src = "/test.png " width = { 200 } height = { 200 } />
9
+ </ div >
10
+ )
11
+ }
Original file line number Diff line number Diff line change @@ -916,6 +916,22 @@ function runTests(mode) {
916
916
)
917
917
} )
918
918
919
+ it ( 'should show invalid src with leading space' , async ( ) => {
920
+ const browser = await webdriver ( appPort , '/invalid-src-leading-space' )
921
+ expect ( await hasRedbox ( browser ) ) . toBe ( true )
922
+ expect ( await getRedboxHeader ( browser ) ) . toContain (
923
+ 'Image with src " /test.jpg" cannot start with a space or control character.'
924
+ )
925
+ } )
926
+
927
+ it ( 'should show invalid src with trailing space' , async ( ) => {
928
+ const browser = await webdriver ( appPort , '/invalid-src-trailing-space' )
929
+ expect ( await hasRedbox ( browser ) ) . toBe ( true )
930
+ expect ( await getRedboxHeader ( browser ) ) . toContain (
931
+ 'Image with src "/test.png " cannot end with a space or control character.'
932
+ )
933
+ } )
934
+
919
935
it ( 'should show error when string src and placeholder=blur and blurDataURL is missing' , async ( ) => {
920
936
const browser = await webdriver ( appPort , '/invalid-placeholder-blur' )
921
937
You can’t perform that action at this time.
0 commit comments