Unverified Commit 2027d1fe authored by ihmpavel's avatar ihmpavel Committed by GitHub

Fix invalid character in `next/image` header "Content-Disposition" (#30287)

* Image content disposition

* Add tests

* Fixed import

* Add TS types

* Revert readme.md

* Alphabet sorting

* Compile `content-disposition`

* Rename for tests

* Fix test

* Fix accidentally added letter
Co-authored-by: 's avatarSteven <steven@ceriously.com>
parent beedff29
(The MIT License)
Copyright (c) 2014-2017 Douglas Christopher Wilson
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
module.exports=(()=>{var r={565:(r,e,t)=>{"use strict";r.exports=contentDisposition;r.exports.parse=parse;var a=t(622).basename;var n=t(505).Buffer;var o=/[\x00-\x20"'()*,/:;<=>?@[\\\]{}\x7f]/g;var i=/%[0-9A-Fa-f]{2}/;var f=/%([0-9A-Fa-f]{2})/g;var s=/[^\x20-\x7e\xa0-\xff]/g;var p=/\\([\u0000-\u007f])/g;var u=/([\\"])/g;var c=/;[\x09\x20]*([!#$%&'*+.0-9A-Z^_`a-z|~-]+)[\x09\x20]*=[\x09\x20]*("(?:[\x20!\x23-\x5b\x5d-\x7e\x80-\xff]|\\[\x20-\x7e])*"|[!#$%&'*+.0-9A-Z^_`a-z|~-]+)[\x09\x20]*/g;var v=/^[\x20-\x7e\x80-\xff]+$/;var x=/^[!#$%&'*+.0-9A-Z^_`a-z|~-]+$/;var l=/^([A-Za-z0-9!#$%&+\-^_`{}~]+)'(?:[A-Za-z]{2,3}(?:-[A-Za-z]{3}){0,3}|[A-Za-z]{4,8}|)'((?:%[0-9A-Fa-f]{2}|[A-Za-z0-9!#$&+.^_`|~-])+)$/;var w=/^([!#$%&'*+.0-9A-Z^_`a-z|~-]+)[\x09\x20]*(?:$|;)/;function contentDisposition(r,e){var t=e||{};var a=t.type||"attachment";var n=createparams(r,t.fallback);return format(new ContentDisposition(a,n))}function createparams(r,e){if(r===undefined){return}var t={};if(typeof r!=="string"){throw new TypeError("filename must be a string")}if(e===undefined){e=true}if(typeof e!=="string"&&typeof e!=="boolean"){throw new TypeError("fallback must be a string or boolean")}if(typeof e==="string"&&s.test(e)){throw new TypeError("fallback must be ISO-8859-1 string")}var n=a(r);var o=v.test(n);var f=typeof e!=="string"?e&&getlatin1(n):a(e);var p=typeof f==="string"&&f!==n;if(p||!o||i.test(n)){t["filename*"]=n}if(o||p){t.filename=p?f:n}return t}function format(r){var e=r.parameters;var t=r.type;if(!t||typeof t!=="string"||!x.test(t)){throw new TypeError("invalid type")}var a=String(t).toLowerCase();if(e&&typeof e==="object"){var n;var o=Object.keys(e).sort();for(var i=0;i<o.length;i++){n=o[i];var f=n.substr(-1)==="*"?ustring(e[n]):qstring(e[n]);a+="; "+n+"="+f}}return a}function decodefield(r){var e=l.exec(r);if(!e){throw new TypeError("invalid extended field value")}var t=e[1].toLowerCase();var a=e[2];var o;var i=a.replace(f,pdecode);switch(t){case"iso-8859-1":o=getlatin1(i);break;case"utf-8":o=n.from(i,"binary").toString("utf8");break;default:throw new TypeError("unsupported charset in extended field")}return o}function getlatin1(r){return String(r).replace(s,"?")}function parse(r){if(!r||typeof r!=="string"){throw new TypeError("argument string is required")}var e=w.exec(r);if(!e){throw new TypeError("invalid type format")}var t=e[0].length;var a=e[1].toLowerCase();var n;var o=[];var i={};var f;t=c.lastIndex=e[0].substr(-1)===";"?t-1:t;while(e=c.exec(r)){if(e.index!==t){throw new TypeError("invalid parameter format")}t+=e[0].length;n=e[1].toLowerCase();f=e[2];if(o.indexOf(n)!==-1){throw new TypeError("invalid duplicate parameter")}o.push(n);if(n.indexOf("*")+1===n.length){n=n.slice(0,-1);f=decodefield(f);i[n]=f;continue}if(typeof i[n]==="string"){continue}if(f[0]==='"'){f=f.substr(1,f.length-2).replace(p,"$1")}i[n]=f}if(t!==-1&&t!==r.length){throw new TypeError("invalid parameter format")}return new ContentDisposition(a,i)}function pdecode(r,e){return String.fromCharCode(parseInt(e,16))}function pencode(r){return"%"+String(r).charCodeAt(0).toString(16).toUpperCase()}function qstring(r){var e=String(r);return'"'+e.replace(u,"\\$1")+'"'}function ustring(r){var e=String(r);var t=encodeURIComponent(e).replace(o,pencode);return"UTF-8''"+t}function ContentDisposition(r,e){this.type=r;this.parameters=e}},505:(r,e,t)=>{var a=t(293);var n=a.Buffer;function copyProps(r,e){for(var t in r){e[t]=r[t]}}if(n.from&&n.alloc&&n.allocUnsafe&&n.allocUnsafeSlow){r.exports=a}else{copyProps(a,e);e.Buffer=SafeBuffer}function SafeBuffer(r,e,t){return n(r,e,t)}copyProps(n,SafeBuffer);SafeBuffer.from=function(r,e,t){if(typeof r==="number"){throw new TypeError("Argument must not be a number")}return n(r,e,t)};SafeBuffer.alloc=function(r,e,t){if(typeof r!=="number"){throw new TypeError("Argument must be a number")}var a=n(r);if(e!==undefined){if(typeof t==="string"){a.fill(e,t)}else{a.fill(e)}}else{a.fill(0)}return a};SafeBuffer.allocUnsafe=function(r){if(typeof r!=="number"){throw new TypeError("Argument must be a number")}return n(r)};SafeBuffer.allocUnsafeSlow=function(r){if(typeof r!=="number"){throw new TypeError("Argument must be a number")}return a.SlowBuffer(r)}},293:r=>{"use strict";r.exports=require("buffer")},622:r=>{"use strict";r.exports=require("path")}};var e={};function __nccwpck_require__(t){if(e[t]){return e[t].exports}var a=e[t]={exports:{}};var n=true;try{r[t](a,a.exports,__nccwpck_require__);n=false}finally{if(n)delete e[t]}return a.exports}__nccwpck_require__.ab=__dirname+"/";return __nccwpck_require__(565)})();
\ No newline at end of file
{"name":"content-disposition","main":"index.js","author":"Douglas Christopher Wilson <doug@somethingdoug.com>","license":"MIT"}
......@@ -167,6 +167,7 @@
"@types/babel__traverse": "7.11.0",
"@types/ci-info": "2.0.0",
"@types/compression": "0.0.36",
"@types/content-disposition": "0.5.4",
"@types/content-type": "1.1.3",
"@types/cookie": "0.3.3",
"@types/cross-spawn": "6.0.0",
......@@ -203,6 +204,7 @@
"comment-json": "3.0.3",
"compression": "1.7.4",
"conf": "5.0.0",
"content-disposition": "0.5.3",
"content-type": "1.0.4",
"cookie": "0.4.1",
"cross-spawn": "6.0.5",
......
......@@ -6,6 +6,7 @@ import imageSizeOf from 'image-size'
import { IncomingMessage, ServerResponse } from 'http'
// @ts-ignore no types for is-animated
import isAnimated from 'next/dist/compiled/is-animated'
import contentDisposition from 'next/dist/compiled/content-disposition'
import { join } from 'path'
import Stream from 'stream'
import nodeUrl, { UrlWithParsedQuery } from 'url'
......@@ -541,7 +542,10 @@ function setResponseHeaders(
const fileName = getFileNameWithExtension(url, contentType)
if (fileName) {
res.setHeader('Content-Disposition', `inline; filename="${fileName}"`)
res.setHeader(
'Content-Disposition',
contentDisposition(fileName, { type: 'inline' })
)
}
res.setHeader('Content-Security-Policy', `script-src 'none'; sandbox;`)
......
......@@ -199,6 +199,16 @@ export async function ncc_conf(task, opts) {
.target('compiled/conf')
}
// eslint-disable-next-line camelcase
externals['content-disposition'] = 'next/dist/compiled/content-disposition'
export async function ncc_content_disposition(task, opts) {
await task
.source(
opts.src || relative(__dirname, require.resolve('content-disposition'))
)
.ncc({ packageName: 'content-disposition', externals })
.target('compiled/content-disposition')
}
// eslint-disable-next-line camelcase
externals['content-type'] = 'next/dist/compiled/content-type'
export async function ncc_content_type(task, opts) {
await task
......@@ -929,6 +939,7 @@ export async function ncc(task, opts) {
'ncc_comment_json',
'ncc_compression',
'ncc_conf',
'ncc_content_disposition',
'ncc_content_type',
'ncc_cookie',
'ncc_cross_spawn',
......
......@@ -87,6 +87,10 @@ declare module 'next/dist/compiled/conf' {
import m from 'conf'
export = m
}
declare module 'next/dist/compiled/content-disposition' {
import m from 'content-disposition'
export = m
}
declare module 'next/dist/compiled/content-type' {
import m from 'content-type'
export = m
......
import React from 'react'
import Image from 'next/image'
import img from '../public/äöü.png'
import img from '../public/äöüščří.png'
const Page = () => {
return (
<div>
<h1>Unicode Image URL</h1>
<Image id="static" src={img} />
<Image id="internal" src="/äöü.png" width={400} height={400} />
<Image id="internal" src="/äöüščří.png" width={400} height={400} />
<Image
id="external"
src="https://image-optimization-test.vercel.app/äöü.png"
src="https://image-optimization-test.vercel.app/äöüščří.png"
width={400}
height={400}
/>
......
......@@ -20,7 +20,9 @@ let browser
function runTests() {
it('should load static unicode image', async () => {
const src = await browser.elementById('static').getAttribute('src')
expect(src).toMatch(/_next%2Fstatic%2Fmedia%2F%C3%A4%C3%B6%C3%BC(.+)png/)
expect(src).toMatch(
/_next%2Fstatic%2Fmedia%2F%C3%A4%C3%B6%C3%BC%C5%A1%C4%8D%C5%99%C3%AD(.+)png/
)
const fullSrc = new URL(src, `http://localhost:${appPort}`)
const res = await fetch(fullSrc)
expect(res.status).toBe(200)
......@@ -28,7 +30,9 @@ function runTests() {
it('should load internal unicode image', async () => {
const src = await browser.elementById('internal').getAttribute('src')
expect(src).toMatch('/_next/image?url=%2F%C3%A4%C3%B6%C3%BC.png')
expect(src).toMatch(
'/_next/image?url=%2F%C3%A4%C3%B6%C3%BC%C5%A1%C4%8D%C5%99%C3%AD.png'
)
const fullSrc = new URL(src, `http://localhost:${appPort}`)
const res = await fetch(fullSrc)
expect(res.status).toBe(200)
......@@ -37,7 +41,7 @@ function runTests() {
it('should load external unicode image', async () => {
const src = await browser.elementById('external').getAttribute('src')
expect(src).toMatch(
'/_next/image?url=https%3A%2F%2Fimage-optimization-test.vercel.app%2F%C3%A4%C3%B6%C3%BC.png'
'/_next/image?url=https%3A%2F%2Fimage-optimization-test.vercel.app%2F%C3%A4%C3%B6%C3%BC%C5%A1%C4%8D%C5%99%C3%AD.png'
)
const fullSrc = new URL(src, `http://localhost:${appPort}`)
const res = await fetch(fullSrc)
......
......@@ -63,7 +63,7 @@ function runTests({
})
it('should handle non-ascii characters in image url', async () => {
const query = { w, q: 90, url: '/äöü.png' }
const query = { w, q: 90, url: '/äöüščří.png' }
const res = await fetchViaHTTP(appPort, '/_next/image', query, {})
expect(res.status).toBe(200)
})
......
......@@ -4498,6 +4498,11 @@
dependencies:
"@types/node" "*"
"@types/content-disposition@0.5.4":
version "0.5.4"
resolved "https://registry.yarnpkg.com/@types/content-disposition/-/content-disposition-0.5.4.tgz#de48cf01c79c9f1560bcfd8ae43217ab028657f8"
integrity sha512-0mPF08jn9zYI0n0Q/Pnz7C4kThdSt+6LD4amsrYDDpgBfrVWa3TcCOxKX1zkGgYniGagRv8heN2cbh+CAn+uuQ==
"@types/content-type@1.1.3":
version "1.1.3"
resolved "https://registry.yarnpkg.com/@types/content-type/-/content-type-1.1.3.tgz#3688bd77fc12f935548eef102a4e34c512b03a07"
......@@ -7483,6 +7488,7 @@ contains-path@^0.1.0:
content-disposition@0.5.3:
version "0.5.3"
resolved "https://registry.yarnpkg.com/content-disposition/-/content-disposition-0.5.3.tgz#e130caf7e7279087c5616c2007d0485698984fbd"
integrity sha512-ExO0774ikEObIAEV9kDo50o+79VCUdEB6n6lzKgGwupcVeRlhrj3qGAfwq8G6uBJjkqLrhT0qEYFcWng8z1z0g==
dependencies:
safe-buffer "5.1.2"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment