-
-
Notifications
You must be signed in to change notification settings - Fork 376
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(http-security-headers): support array responses #615
fix(http-security-headers): support array responses #615
Conversation
Thanks for opening a PR (FYI you're committing to an unreleased branch). Applying the middleware that modify headers earlier so that they are executed after response modifiers will resolve this issue. For a better developer experience, we should handler this in ( Thank you for your additional comments.
|
Thank you very much for merging this, @willfarrell.
I'm aware 👍 . Thanks for clarifying, though. Thought this was the right way to go since the public alpha had already been released?
I agree - but this introduces a level of dependency which makes individual middlewares require others to be declared in a certain order for them to function properly. Making middlewares resilient to changes and generally isolated from others is key, IMHO.
Not a bad idea. Up until now, I've been always adding my own "response normalizer" middleware, containing a variation of the following: /**
* Ensures that the given `response` payload conforms to AWS APIGateway proxy
* spec, returning at least a `body` property.
*
* @example
* normalizeResponse('foo'); // { body: 'foo' };
* normalizeResponse({ statusCode: 404 }); // { statusCode: 404 };
*
* @function
* @param {*} response The response to normalize.
* @returns {import('aws-lambda').APIGatewayProxyStructuredResult} The normalized response.
*/
const normalizeResponse = ifElse(
anyPass([has('body'), has('headers'), has('statusCode')]),
applySpec({
body: either(prop('body'), omit(['headers', 'statusCode'])),
headers: prop('headers'),
statusCode: prop('statusCode')
}),
objOf('body')
); The snippet uses
Not really bad per sé, more like a (really) nice thing to have when debugging and tracing errors. Right now, a stacktrace for a thrown error within a Uncaught TypeError: cannot read property 'filter' of undefined
at module.exports Whereas, if it had properly named functions, it'd look something like: Uncaught TypeError: cannot read property 'filter' of undefined
at httpCors All I did was modify
You'll see nothing but gains. Go ahead and try it out. |
I've already pushed the changes for the http normalization. I kept it simple. The case with an object without body is not handled specifically to not introduce reserved words.
That is a good reason in my books, I'll push an update shortly, but will use a different pattern that more consistent with the code base.
Thanks again for the feedback, we really appreciate you taking the time to share it with us. |
Well, but there already are reserved keywords, aren't there? How would you return a non You'd need to be able to do something like this from your function: function handleEvent(event, context) {
// ...
return {
statusCode: 404,
headers: { 'Set-Cookie': '__uid=76g-Xz9883==' }
};
} Evidently, that can't be serialized and interpreted as being the For |
What does this implement/fix? Explain your changes.
Add support for handlers returning arrays as responses in
@middy/http-security-response-headers
. Typically, API Gateway response shape and serialization are handled by other middlewares down the chain. In its current form,http-security-headers
restricts flexibility by expecting an object ashandler.response
, producing malformed responses if it's not.Any other comments?
middy
are anonymous. I was really tempted to change themodule.exports
forhttp-security-headers
to a proper namedfunction
- but didn't to keep changes to the point. I could start a new PR to address this issue for all@middy/core
and@middy/*
individual middlewares if there's any interest for it.middy
could greatly benefit from having aneslint
,prettier
and.editorconfig
config files."husky"
config is callinggit add
manually, which is no longer needed and raises a warning.⚠ Some of your tasks use `git add` command. Please remove it from the config since all modifications made by tasks will be automatically added to the git commit index.
test:packages:typings
script was failing for me, which causesnpm test
to also fail. Maybe I was doing something wrong or missing a step?Where has this been tested?
Node.js Versions: v14.15.4
Middy Versions: Any version.
AWS SDK Versions: Not relevant.
Todo list