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 naive content-type sniffing implementation #940

Merged
merged 2 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions packages/toolpad-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"ts-node": "^10.9.1",
"typescript": "^4.8.2",
"web-streams-polyfill": "^3.2.1",
"whatwg-mimetype": "^3.0.0",
"whatwg-url": "^11.0.0"
},
"devDependencies": {
Expand All @@ -118,6 +119,7 @@
"@types/react-dom": "^18.0.6",
"@types/react-inspector": "^4.0.2",
"@types/serialize-javascript": "^5.0.2",
"@types/whatwg-mimetype": "^2.1.1",
"@types/whatwg-url": "^11.0.0",
"ajv": "^8.11.0",
"eslint": "8.23.0",
Expand Down
13 changes: 12 additions & 1 deletion packages/toolpad-app/src/toolpadDataSources/rest/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BindableAttrEntries, BindableAttrValue, BindableAttrValues } from '@mui/toolpad-core';
import fetch, { Headers, RequestInit, Response } from 'node-fetch';
import MIMEType from 'whatwg-mimetype';
import { withHarInstrumentation, createHarLog } from '../../server/har';
import { ServerDataSource, ApiResult } from '../../types';
import {
Expand Down Expand Up @@ -112,8 +113,18 @@ async function resolveBody(body: Body, boundValues: Record<string, string>) {
}
}

function isJSON(mimeType: MIMEType): boolean {
// See https://mimesniff.spec.whatwg.org/#json-mime-type
const essence = `${mimeType.type}/${mimeType.subtype}`;
return (
essence === 'text/json' || essence === 'application/json' || mimeType.subtype.endsWith('+json')
);
}

async function readData(res: Response): Promise<any> {
return res.headers.get('content-type') === 'application/json' ? res.json() : res.text();
const contentType = res.headers.get('content-type');
const mimeType = contentType ? new MIMEType(contentType) : null;
return mimeType && isJSON(mimeType) ? res.json() : res.text();
}

async function execBase(
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2946,6 +2946,11 @@
resolved "https://registry.yarnpkg.com/@types/webidl-conversions/-/webidl-conversions-7.0.0.tgz#2b8e60e33906459219aa587e9d1a612ae994cfe7"
integrity sha512-xTE1E+YF4aWPJJeUzaZI5DRntlkY3+BCVJi0axFptnjGmAoWxkyREIh/XMrfxVLejwQxMCfDXdICo0VLxThrog==

"@types/whatwg-mimetype@^2.1.1":
version "2.1.1"
resolved "https://registry.yarnpkg.com/@types/whatwg-mimetype/-/whatwg-mimetype-2.1.1.tgz#1b7b7aecaa3695209fd2f3a808dd5729973a52fa"
integrity sha512-ojnf89qt5AWnqsjyPqMLN8uVaxm5x23vxNQ1me6EPBOdJe1YYuIZUzg809MZUG8UU6HKhkr6ah4fi2WUvD0DFw==

"@types/whatwg-url@^11.0.0":
version "11.0.0"
resolved "https://registry.yarnpkg.com/@types/whatwg-url/-/whatwg-url-11.0.0.tgz#5c42518a163a6867e14235223a1a558143bccbab"
Expand Down