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

Max cookie size assert #159

Merged
merged 7 commits into from
Mar 29, 2018
Merged

Conversation

omairvaiyani
Copy link
Contributor

Copy link
Member

@marcoow marcoow left a comment

Choose a reason for hiding this comment

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

Awesome, thanks for submitting! I left a few comments on minor things and this would need a test case.

@@ -76,6 +77,8 @@ export default Service.extend({
assert('Cookies cannot be set to be HTTP-only as those cookies would not be accessible by the Ember.js application itself when running in the browser!', !options.httpOnly);
assert("Cookies cannot be set as signed as signed cookies would not be modifyable in the browser as it has no knowledge of the express server's signing key!", !options.signed);
assert('Cookies cannot be set with both maxAge and an explicit expiration time!', isEmpty(options.expires) || isEmpty(options.maxAge));
assert(`Cookies larger than ${MAX_COOKIE_BYTE_LENGTH} bytes are not supported by most browsers!`, this._isCookieSizeAcceptable(value));
Copy link
Member

Choose a reason for hiding this comment

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

I'd move this to after value is assigned in line 82 and run the check on the actual value we're also writing then.


_getByteCount(value) {
return typeof(value) === 'string' ? encodeURI(value).split(/%(?:u[0-9A-F]{2})?[0-9A-F]{2}|./).length - 1 : 0;
}
Copy link
Member

Choose a reason for hiding this comment

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

I guess these 2 methods could also be combined.

},

_getByteCount(value) {
return typeof(value) === 'string' ? encodeURI(value).split(/%(?:u[0-9A-F]{2})?[0-9A-F]{2}|./).length - 1 : 0;
Copy link
Member

Choose a reason for hiding this comment

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

When the value assigned in line 82 is passed here, the typeof check would no longer be necessary.

Also it would be good to add a comment explaining what the split does.

@omairvaiyani
Copy link
Contributor Author

@marcoow Thanks for the review! I've made the changes and added a unit test

Now, I wasn't sure whether you prefer the MAX_COOKIE_SIZE_LENGTH to be used by the unit test, or whether to decouple the test from the unit entirely. So I've gone for the latter option, the for-loop runs enough times to generate a value that is always above 4096 bytes.

Copy link
Member

@marcoow marcoow left a comment

Choose a reason for hiding this comment

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

Looks good 👍

// This snippet counts the bytes in the value
// about to be stored as the cookie:
// See https://stackoverflow.com/a/25994411/6657064
let _countUtf8Bytes = function (s){
Copy link
Member

Choose a reason for hiding this comment

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

It seems the inner function isn't actually needed here?

it('throws when a larger than allowed cookie is set', function() {
expect(() => {
let value = "";
for(let i = 0; i < 400; i++) { value += Math.random().toString(36).substring(2) }
Copy link
Member

Choose a reason for hiding this comment

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

I guess we could just use a static value here that we know has more than 4096 bytes?


assert(`Cookies larger than ${MAX_COOKIE_BYTE_LENGTH} bytes are not supported by most browsers!`, this._isCookieSizeAcceptable(value));


Copy link
Member

Choose a reason for hiding this comment

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

double newline - this should ideally get caught be ESLint actually…

@marcoow
Copy link
Member

marcoow commented Mar 21, 2018

I restarted the build - seems like the failures were unrelated actually.

@marcoow
Copy link
Member

marcoow commented Mar 21, 2018

@omairvaiyani: tests run now but there are some ESLint issues…

@marcoow
Copy link
Member

marcoow commented Mar 28, 2018

I fixed the linter errors. This should be good to go after #168 is merged and this is rebased.

@marcoow marcoow merged commit a674891 into mainmatter:master Mar 29, 2018
@omairvaiyani
Copy link
Contributor Author

Hey @marcoow thanks for sorting this!

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