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 for incorrect Content-Type headers returned for Admin UI bundles #2864

Merged
merged 3 commits into from
Apr 30, 2020

Conversation

molomby
Copy link
Member

@molomby molomby commented Apr 30, 2020

Fixes #2741.

When the Express res.format() function matches a request (to a non-default function) it replaces the responses Content-Type header with the key it matched on. This causes our */* format handler (which is basically a noop) to return requests with header of Content-Type: */*. This in turn causes problems if a X-Content-Type-Options: nosniff header is also set on the request (ie. somewhere else in the Express middleware, by a reverse proxy, etc.).

We can fix this by resetting the Content-Type header with a mime-type derived from the requested files extension.

Unfortunately, despite being quite redundant, we can't just remove the */* handler. Doing so causes requests with an Accept: */* header or no Accept at all to fall though to the text/html handler, rather than the default.

The default handler itself doesn't require this additional logic as Express has already given the response a correct Content-Type header and doesn't override it when the default format handler is used. If the text/html handler is hit, the overridden Content-Type is correctly set to text/html.

@changeset-bot
Copy link

changeset-bot bot commented Apr 30, 2020

🦋 Changeset is good to go

Latest commit: 2e56648

We got this.

This PR includes changesets to release 1 package
Name Type
@keystonejs/app-admin-ui Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@molomby molomby requested a review from MadeByMike April 30, 2020 02:13
Copy link
Contributor

@dominikwilkowski dominikwilkowski left a comment

Choose a reason for hiding this comment

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

LGTM

// We need to reset the res 'Content-Type' otherwise it gets replaced by the format we've matched on: '*/*'.
// Returning a wildcard mimetype causes problems if a 'X-Content-Type-Options: nosniff' header is also set.
// See.. https://github.com/keystonejs/keystone/issues/2741
res.type(path.extname(req.url));
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens when req.url = /foo/bar?

Wouldn't we want the content type to be text/html then? Or maybe */* to let the client decide?

Copy link
Contributor

Choose a reason for hiding this comment

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

Don't we control 100% of the requests? In the adminUI... which so far I can only see /admin/js/*

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe something like this?

Suggested change
res.type(path.extname(req.url));
const extension = path.extname(req.url);
res.type(extension ? extension : '*/*');

Copy link
Contributor

Choose a reason for hiding this comment

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

@dominikwilkowski This express app handles everything under /admin, which could include custom pages. Not to mention the pages such as /admin/todos which doesn't have an extension.

Copy link
Member Author

@molomby molomby Apr 30, 2020

Choose a reason for hiding this comment

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

Great point. So...

  1. If I request /admin/blerg from the browser (address bar), the browser (Chrome in this case) adds a sensible Accept header of..

    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
    

    Which works fine as it matches the text/html resolver (returns with Content-Type: text/html; charset=utf-8).

  2. If I request /admin/blerg without an Accept header, it hits the */* format resolver and the Content-Type is returned as application/octet-stream. Presumably this is some kind of default that's applied since we're passing res.type() and empty string.

  3. If I request with an unrecognised Accept header (food/pasta) it hits the default format resolver, returning with Content-Type: text/html; charset=UTF-8 which is correct for the content -- the main html page for the admin UI.

This all seems to be true regardless of the depth of the path, trailing slashes, etc.

Point 2 is probably wrong. The content being returned is actual html; the incorrect mime type here is our fault -- I've patched to only override if a file extension is present, meaning it will return */* as before.

Note this still isn't totally correct. If we removed the res.format() block entirely, these requests would return text/html. But as you say, at least the browser can decide (and it's still correct more often than what we have now).

@molomby molomby merged commit 4af9e40 into master Apr 30, 2020
@molomby molomby deleted the molomby/admin-bundle-mimetype branch April 30, 2020 03:37
@github-actions github-actions bot mentioned this pull request Apr 30, 2020
Wkasel pushed a commit to gosignal/keystone that referenced this pull request Apr 30, 2020
… (eg. bundles) (keystonejs#2864)

* Fix for incorrect Content-Type headers returned for Admin UI bundles
* Changeset 🦋✨
* Being more sensible about paths without a file extension
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Keystone Admin UI serves incorrect MimeType
4 participants