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

prevent body format errors and wasted json parsing on correct but "non json" repsonses #95

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

lucidNTR
Copy link

@lucidNTR lucidNTR commented Nov 8, 2017

currently non json responses will break the server and always tried to be parsed as json, even when the content type says they are not in json format.

this PR fixes that behaviour

@seanpk seanpk self-requested a review November 8, 2017 13:04
@seanpk seanpk self-assigned this Nov 8, 2017
Copy link
Member

@seanpk seanpk left a comment

Choose a reason for hiding this comment

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

Thanks for submitting this PR!

At a high-level the comments boil down to:

  1. Does this still do response model validation for users who are sending JSON but haven't explicitly set the Content-Type response header?
  2. Let's rename some symbols for clarity
  3. Let's not include the yarn.lock file as part of this PR

Then, one last request:

  • let's add a test that shows the current bad behavior and proves we've corrected it

logger.warn('Invalid content type specified: %s. Expecting one of %s', type, allowedTypes);
}
// Wrap the set function, which is responsible for setting headers
var type = '';
Copy link
Member

Choose a reason for hiding this comment

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

for clarity, I think it would be good to call the variable something like responseContentType

});
}
}
});

if (responseModelValidationLevel) {
var responseSender = res.send;
res.send = function (body) {
var isBodyValid = isValidDataType(body);
Copy link
Member

Choose a reason for hiding this comment

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

it looks like it might be good to refactor this function's name, maybe to isValidJsonData

With that change, it may make sense to move the check for 'application/json' to the outermost extent to protect the subsequent code. However, I am not sure how that will affect users who just call res.send without explicitly setting the Content-Type ...

res.send = responseSender;
responseSender.call(res, body);
return;
if ( type === 'application/json') {
Copy link
Member

Choose a reason for hiding this comment

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

There's an extra space between the bracket and type ...

Also, how does this affect users who are not explicitly setting their response content type?
Has express already set that header by this time?
Above, should the default value for type be 'application/json'?
Or should this check be more like: if (!type || type === 'application/json')?

yarn.lock Outdated
@@ -0,0 +1,2212 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
Copy link
Member

Choose a reason for hiding this comment

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

let's not add this file as part of this PR - please add it to the .gitignore file for now.

@lucidNTR
Copy link
Author

lucidNTR commented Nov 8, 2017

@seanpk i agree on the changes, before i make the fixes we should agree on behaviour on unset content-type:
i will include fallback to treat data as json if no content type is set , OK?

@seanpk
Copy link
Member

seanpk commented Nov 8, 2017

@lucidNTR - yes, I think by treating unset response content-type as application/json, the change will be backward compatible for existing code that doesn't set it explicitly.

Thanks again.

@lucidNTR
Copy link
Author

lucidNTR commented Nov 28, 2017

i made the first set of changes as you had in mind.
still Missing:

  • moving the content type check to the top of the function scope, as i also am not sure about the implications
  • test, i am still struggling to make swagger tests work

@lucidNTR
Copy link
Author

@seanpk ok i found the problem:

_swagger.js : line 354
validateResponseModels does not contain validation errors or warnings for content type, so these errors are just logged and treated different from other validation errors. Integration tests ignore all errors that are just logged if they are not handled in the response.

What is your recommended next step?

@seanpk
Copy link
Member

seanpk commented Nov 29, 2017

@lucidNTR - can you provide more details on the problem?

Looking at that code in the context of this PR, are you looking to add a check, and subsequent warning, if the Accept header is one thing but the Content-Type of the response being sent is something different?
That seems like a useful check.

If that is what you're asking about, after doing the check, we could call _createResponseValidationError with an appropriate synthetic [subError] to get it to produce meaningful output. (Although I'm not entirely sure what that synthetic would look like right now, and maybe it's better just to provide no sub errors ...)

@lucidNTR
Copy link
Author

lucidNTR commented Dec 4, 2017

@seanpk that was exactly my idea, but i have to admit the error handling is a bit confusing to me with 3 levels of indirection and hirarchical validation errors im struggling to find the right way to add the new synthetic subError at the moment.

@lucidNTR
Copy link
Author

lucidNTR commented Dec 4, 2017

i start to get the feeling this is a whole new PR with its own caveats. to finish this PR in a timely manner, would you be OK if i just add the possibility to check for console log errors in integration tests and make sure the tests fail first and then succeed with this PR?

@seanpk
Copy link
Member

seanpk commented Dec 5, 2017

@lucidNTR - yes, I can support that - I think there are other tests that check the log for errors now already (or if not in this repo, I've seen the pattern before)

Thanks again for your diligence and contributions!

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.

None yet

2 participants