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] Expand list of allowed mime types for binary bodies #1687

Merged
merged 21 commits into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from 9 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
4 changes: 2 additions & 2 deletions packages/adapter-cloudflare-workers/files/entry.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// TODO hardcoding the relative location makes this brittle
import { init, render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved
import { getAssetFromKV, NotFoundError } from '@cloudflare/kv-asset-handler'; // eslint-disable-line import/no-unresolved
import { get_body_type } from '@sveltejs/kit/utils'; // eslint-disable-line import/no-unresolved

init();

Expand Down Expand Up @@ -57,9 +58,8 @@ async function handle(event) {
/** @param {Request} request */
async function read(request) {
const type = request.headers.get('content-type') || '';
if (type.includes('application/octet-stream')) {
if (get_body_type(type) === 'buffer') {
return new Uint8Array(await request.arrayBuffer());
}

return request.text();
}
4 changes: 3 additions & 1 deletion packages/adapter-netlify/files/entry.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// TODO hardcoding the relative location makes this brittle
import { init, render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved
import { get_body_type } from '@sveltejs/kit/utils'; // eslint-disable-line import/no-unresolved

init();

Expand All @@ -8,8 +9,9 @@ export async function handler(event) {

const query = new URLSearchParams(rawQuery);

const type = headers['content-type'];
const rawBody =
headers['content-type'] === 'application/octet-stream'
get_body_type(type) === 'buffer'
? new TextEncoder('base64').encode(body)
: isBase64Encoded
? Buffer.from(body, 'base64').toString()
Expand Down
3 changes: 3 additions & 0 deletions packages/kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@
"./install-fetch": {
"import": "./dist/install-fetch.js"
},
"./utils": {
"import": "./dist/utils.js"
},
"./types": "./types/index.d.ts"
},
"types": "types/index.d.ts",
Expand Down
3 changes: 2 additions & 1 deletion packages/kit/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ export default [
cli: 'src/cli.js',
ssr: 'src/runtime/server/index.js',
node: 'src/core/node/index.js',
'install-fetch': 'src/install-fetch.js'
'install-fetch': 'src/install-fetch.js',
utils: 'src/core/utils.js'
benmccann marked this conversation as resolved.
Show resolved Hide resolved
},
output: {
dir: 'dist',
Expand Down
4 changes: 3 additions & 1 deletion packages/kit/src/core/node/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { get_body_type } from '../utils.js';

/**
* @param {import('http').IncomingMessage} req
* @returns {Promise<import('types/hooks').StrictBody>}
Expand Down Expand Up @@ -48,7 +50,7 @@ export function getRawBody(req) {
req.on('end', () => {
const [type] = h['content-type'].split(/;\s*/);

if (type === 'application/octet-stream') {
if (get_body_type(type) === 'buffer') {
return fulfil(data);
}

Expand Down
17 changes: 17 additions & 0 deletions packages/kit/src/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,20 @@ function find_svelte_packages(cwd) {
export function get_no_external(cwd, user_specified_deps = []) {
return [...user_specified_deps, ...find_svelte_packages(cwd)];
}

/**
* Decides how the body should be parsed based on its mime type.
*
* This is intended to be used with both requests and responses, to have a consistent body parsing across adapters.
*
* @param {string} content_type The `content-type` header of a request/response.
* @returns {"string"|"buffer"}
*/
export function get_body_type(content_type) {
benmccann marked this conversation as resolved.
Show resolved Hide resolved
return content_type.startsWith('image') ||
content_type.startsWith('audio') ||
content_type.startsWith('video') ||
content_type.startsWith('application/octet-stream')
? 'buffer'
: 'string';
}
18 changes: 11 additions & 7 deletions packages/kit/src/runtime/server/endpoint.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { get_body_type } from '../../core/utils.js';
import { lowercase_keys } from './utils.js';

/** @param {string} body */
Expand Down Expand Up @@ -37,24 +38,27 @@ export default async function render_route(request, route) {
headers = lowercase_keys(headers);
const type = headers['content-type'];

const is_type_binary = get_body_type(type) === 'buffer';
/** @type {import('types/hooks').StrictBody} */
let normalized_body;
Copy link
Member

Choose a reason for hiding this comment

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

I would move this line back down where it was originally. No reason to declare it higher up since it's not used yet


// validation
if (type === 'application/octet-stream' && !(body instanceof Uint8Array)) {
if (is_type_binary && !(body instanceof Uint8Array)) {
return error(
`${preface}: body must be an instance of Uint8Array if content type is application/octet-stream`
`${preface}: body must be an instance of Uint8Array if content type is image/*, audio/*, video/* or application/octet-stream `
benmccann marked this conversation as resolved.
Show resolved Hide resolved
);
}

if (body instanceof Uint8Array && type !== 'application/octet-stream') {
if (body instanceof Uint8Array && !is_type_binary) {
return error(
`${preface}: Uint8Array body must be accompanied by content-type: application/octet-stream header`
`${preface}: Uint8Array body must have content-type header of image/*, audio/*, video/* or application/octet-stream`
);
}

/** @type {import('types/hooks').StrictBody} */
let normalized_body;

// ensure the body is an object
if (
(typeof body === 'object' || typeof body === 'undefined') &&
!(body instanceof Uint8Array) &&
(!type || type.startsWith('application/json'))
) {
headers = { ...headers, 'content-type': 'application/json; charset=utf-8' };
Expand Down