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

Handle HTTP headers as case insensitive #663

Closed
wants to merge 10 commits into from
Closed

Handle HTTP headers as case insensitive #663

wants to merge 10 commits into from

Conversation

tinovyatkin
Copy link
Contributor

By HTTP standard headers are case insensitive, so checking for existing Idempotency-Key like headers.hasOwnProperty('Idempotency-Key') is wrong. Should we fix that?

@paulasjes-stripe
Copy link
Contributor

We always set the header name to use the format Idempotency-Keys when a key is provided in a request, however it can't hurt to be extra safe.

Could you add tests for this change? They should go under the Network Retry section here.

return false;
})
) {
headers['Idempotency-Key'] = idempotencyKey = uuid();
Copy link
Contributor

Choose a reason for hiding this comment

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

If we're doing this for Idempotency-Key, then we should do the same for Stripe-Version and Stripe-Account

stripe-node/lib/utils.js

Lines 141 to 146 in a5885d7

if (params.stripe_account) {
opts.headers['Stripe-Account'] = params.stripe_account;
}
if (params.stripe_version) {
opts.headers['Stripe-Version'] = params.stripe_version;
}

Can you move this functionality to a helper function in utils and reuse it for the version and account headers?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is no problem with setter of headers, only getter of headers must be case insensitive.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What I see weird here, is that you are using hardcoded string like this at setters and then lowecased hardcoded strings at getters:

api_version: headers['stripe-version'],

It looks like a bad style and human errors prone. I would prefer move all private headers to a http-headers.js file as lowercase constants and use constants everywhere. Are you OK with that?

Copy link
Contributor

Choose a reason for hiding this comment

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

While I don't think a separate constants file is necessary, I think that's fine!

Copy link
Contributor

Choose a reason for hiding this comment

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

I do think we should have a getHeader(header, 'Header-Name') function in utils, and use that to check whether idempotency-key is already defined, and also to get the stripe-version/stripe-account values.

Copy link
Contributor

Choose a reason for hiding this comment

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

(Alternatively, we could normalize the headers hash to be all lower-case keys prior to this processing)

Copy link
Contributor Author

@tinovyatkin tinovyatkin Jul 25, 2019

Choose a reason for hiding this comment

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

While I don't think a separate constants file is necessary, I think that's fine!

You already have around 10 private HTTP headers sprinkled all around SDK. Some of them are prefixed with X-, like X-Stripe-Client-Telemetry, some doesn't like Stripe-Signature or Stripe-Version. Having them all in the same file will set good conventions how to add new one and enforce constants use to prevent any errors in future version regarding them. If you are OK, I'll try to do that today in this PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure, if you think it would help readability to users, please do!

@tinovyatkin tinovyatkin changed the title [BUG] fix Idempotency-Key verification Handle HTTP headers as case insensitive Jul 26, 2019
@rattrayalex-stripe
Copy link
Contributor

Hey Tino, Just wanted to say thanks again for all your work here. I'm pretty busy today but hope to give this and #661 a thorough review Monday or Tuesday; appreciate your patience!

@rattrayalex-stripe
Copy link
Contributor

Trying to give this a thorough, careful review made my head spin because our headers were just all over the place (our fault, not yours) so I threw together #667 which ended up obviating this, so closing.

I know you were looking forward to exporting constants for the headers, but on second thought I'd rather not treat this as a public API, and with #667 it's at least easy to see them in one place. I hope you understand.

Thanks for all your terrific work here @tinovyatkin !

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.

3 participants