File tree 3 files changed +51
-2
lines changed
packages/next/src/server/lib/router-utils
3 files changed +51
-2
lines changed Original file line number Diff line number Diff line change @@ -31,6 +31,25 @@ function warnOrBlockRequest(
31
31
return true
32
32
}
33
33
34
+ function isInternalDevEndpoint ( req : IncomingMessage ) : boolean {
35
+ if ( ! req . url ) return false
36
+
37
+ try {
38
+ // TODO: We should standardize on a single prefix for this
39
+ const isMiddlewareRequest = req . url . includes ( '/__nextjs' )
40
+ const isInternalAsset = req . url . includes ( '/_next' )
41
+ // Static media requests are excluded, as they might be loaded via CSS and would fail
42
+ // CORS checks.
43
+ const isIgnoredRequest =
44
+ req . url . includes ( '/_next/image' ) ||
45
+ req . url . includes ( '/_next/static/media' )
46
+
47
+ return ! isIgnoredRequest && ( isInternalAsset || isMiddlewareRequest )
48
+ } catch ( err ) {
49
+ return false
50
+ }
51
+ }
52
+
34
53
export const blockCrossSite = (
35
54
req : IncomingMessage ,
36
55
res : ServerResponse | Duplex ,
@@ -51,8 +70,7 @@ export const blockCrossSite = (
51
70
}
52
71
53
72
// only process internal URLs/middleware
54
- // TODO: We should standardize on a single prefix for this
55
- if ( ! req . url ?. includes ( '/_next' ) && ! req . url ?. includes ( '/__nextjs' ) ) {
73
+ if ( ! isInternalDevEndpoint ( req ) ) {
56
74
return false
57
75
}
58
76
// block non-cors request from cross-site e.g. script tag on
Original file line number Diff line number Diff line change @@ -318,6 +318,37 @@ describe.each([['', '/docs']])(
318
318
server . close ( )
319
319
}
320
320
} )
321
+
322
+ it ( 'should load images regardless of allowed origins' , async ( ) => {
323
+ const { server, port } = await createHostServer ( )
324
+ try {
325
+ const browser = await webdriver ( `http://127.0.0.1:${ port } ` , '/about' )
326
+
327
+ const imageSnippet = `(() => {
328
+ const statusEl = document.createElement('p')
329
+ statusEl.id = 'status'
330
+ document.querySelector('body').appendChild(statusEl)
331
+
332
+ const image = document.createElement('img')
333
+ image.src = "${ next . url } /_next/image?url=%2Fimage.png&w=256&q=75"
334
+ document.querySelector('body').appendChild(image)
335
+ image.onload = () => {
336
+ statusEl.innerText = 'OK'
337
+ }
338
+ image.onerror = () => {
339
+ statusEl.innerText = 'Unauthorized'
340
+ }
341
+ })()`
342
+
343
+ await browser . eval ( imageSnippet )
344
+
345
+ await retry ( async ( ) => {
346
+ expect ( await browser . elementByCss ( '#status' ) . text ( ) ) . toBe ( 'OK' )
347
+ } )
348
+ } finally {
349
+ server . close ( )
350
+ }
351
+ } )
321
352
} )
322
353
}
323
354
)
You can’t perform that action at this time.
0 commit comments