Skip to content

New generic mode that always returns * (less secure, better caching) #32

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

rprieto
Copy link
Contributor

@rprieto rprieto commented May 22, 2017

New "generic" mode which always returns access-control-allow-origin: *, similar to what the fullResponse Restify plugin used to do.

This is the less secure option, but increases performance since responses can now be cached 100%, instead of having Vary: Origin.

@rprieto rprieto changed the title Support for wildcards + new generic mode New generic mode that always returns * (less secure, better caching) May 22, 2017
src/origin.js Outdated
exports.allowed = function (list, requestOrigin) {
function match (origin) {
if (origin.indexOf('*') !== -1) {
var regex = '^' + origin.replace('.', '\\.').replace('*', '.*') + '$'
Copy link
Contributor

Choose a reason for hiding this comment

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

small performance comment, could we generate a list of these regex when the middleware is created rather then doing it on each request?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll implement that on #33 (of top of which this PR should be rebased)

Copy link

@retrohacker retrohacker left a comment

Choose a reason for hiding this comment

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

Interesting idea! Some code changes requested. On the fence about special casing *, especially if we special case * only when it is the only element in the array.

// If either no origin was set, or the origin isn't supported, continue
// without setting any headers
if (!originHeader || !matcher(originHeader)) {
if (originMatcher.generic(options.origins)) {

Choose a reason for hiding this comment

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

Is there a way to handle this without treating the whole block as a special case? A nested if/else of this size increases the complexity of the code base quite a lot and fragments related changes across multiple lines in the code base.

@@ -1,4 +1,8 @@

exports.generic = function (list) {
return list.length === 1 && list[0] === '*'

Choose a reason for hiding this comment

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

Is there a case where we would want to explicitly match some domains, and wildcard the rest? Would it be surprising that ['*'] would have one behaviour for a domain matched against the wildcard and an entirely different behaviour for a domain matched against the wildcard in this list: ['foo.bar', '*']

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree that's quite strange, not 100% happy with that design. Do you think it would make more sense to have some sort of option flag like

{
  // specify one or more origins, wildcard supported
  // will only return the matched origin, with a Vary header
  origins: ['https://myapp.com', 'https://*.myapp.com']

  // or use the generic mode
  // which always returns "Accept", and can be cached across the board
  // but has security implications
  allowAllOrigins: true
}

Choose a reason for hiding this comment

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

I like the allowAllOrigins override. It seems like it would both simplify the implementation and make the user's intent explicit.

// 6.2.1 and 6.2.2
var originHeader = req.headers['origin']
if (!matcher(originHeader)) return next()
if (originMatcher.generic(options.origins)) {

Choose a reason for hiding this comment

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

Same comment as above, but to a much greater extent. Is there a way we can do this that doesn't treat the whole block as a special case? We are adding an incredibly amount of complexity here to support this.

@kokarn
Copy link

kokarn commented Jan 2, 2018

Is this dead in the water? It would be greatly appreciated if this was something that could be added.
Running into issues with stuff like CloudFront.

@retrohacker
Copy link

Yeah, @kokarn, it looks like this has lost momentum. If you want to pick it up and implement the feedback I would be happy to do a code review on another PR

@kokarn
Copy link

kokarn commented Mar 5, 2018

I'm not opposed at all but pretty sure I won't have the time in the near future. Will keep it in the back of my head tho, so who knows :)

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.

4 participants