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

Add parseCookieName, parseCookieValue methods #1

Merged
merged 4 commits into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ strictCookieParser.parseCookieHeader('not a cookie')

strictCookieParser.parseCookiePair('single=pair')
// { name: 'single', value: 'pair' }

strictCookieParser.parseCookieName('foo')
// 'foo'

strictCookieParser.parseCookieName('m=m')
// invalid - cookie names cannot contain =
// null

strictCookieParser.parseCookieValue('"foo"')
// 'foo'

strictCookieParser.parseCookieValue(' foo')
// invalid - unquoted cookie values cannot begin with a space
// null
```


Expand Down
38 changes: 30 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const COOKIE_PAIR = /^([^\x00-\x20\x7f()<>@,;:\\"/[\]?={}]+)=(?:([\x21\x23-\x2b\x2d-\x3a\x3c-\x5b\x5d-\x7e]*)|"([\x21\x23-\x2b\x2d-\x3a\x3c-\x5b\x5d-\x7e]*)")$/;
const COOKIE_NAME = /^([^\x00-\x20\x7f()<>@,;:\\"/[\]?={}]+)$/;
const COOKIE_VALUE = /^(?:([\x21\x23-\x2b\x2d-\x3a\x3c-\x5b\x5d-\x7e]*)|"([\x21\x23-\x2b\x2d-\x3a\x3c-\x5b\x5d-\x7e]*)")$/;

const isOptionalWhitespace = c =>
c === 32 || c === 9;
Expand All @@ -26,18 +27,37 @@ const stripCookieHeader = header => {
return header.substring(start, end + 1);
};

const parseCookieName = cookieName => {
const nameMatch = COOKIE_NAME.exec(cookieName);
flotwig marked this conversation as resolved.
Show resolved Hide resolved
if (nameMatch === null) {
return null;
}
return nameMatch[1];
};

const parseCookieValue = cookieValue => {
const valueMatch = COOKIE_VALUE.exec(cookieValue);
if (valueMatch === null) {
return null;
}
return valueMatch[1] === undefined ?
valueMatch[2] :
valueMatch[1];
};

const parseCookiePair = cookiePair => {
const match = COOKIE_PAIR.exec(cookiePair);
const parts = cookiePair.split('=', 2);
charmander marked this conversation as resolved.
Show resolved Hide resolved

if (match === null) {
if (parts.length !== 2) {
return null;
}

const name = match[1];
const value =
match[2] === undefined ?
match[3] :
match[2];
const name = parseCookieName(parts[0]);
const value = parseCookieValue(parts[1]);

if (name === null || value === null) {
return null;
}

return {
name,
Expand Down Expand Up @@ -75,6 +95,8 @@ const parseCookieHeader = cookieHeader =>
);

module.exports = {
parseCookieName,
parseCookieValue,
parseCookiePair,
parseCookieHeader,
};
17 changes: 16 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
const assert = require('assert');
const test = require('@charmander/test')(module);

const {parseCookieHeader} = require('./');
const {
parseCookieHeader,
parseCookieName,
parseCookieValue,
} = require('./');

test('Single unquoted cookies are parsed correctly', () => {
const result = parseCookieHeader('hello=world');
Expand Down Expand Up @@ -57,3 +61,14 @@ test('Invalid cookies are rejected', () => {
assert.strictEqual(parseCookieHeader('a=comma,character'), null);
assert.strictEqual(parseCookieHeader('a=double"quote'), null);
});

test('.parseCookieValue works as expected', () => {
assert.strictEqual(parseCookieValue(' foo'), null);
assert.strictEqual(parseCookieValue('foo'), 'foo');
assert.strictEqual(parseCookieValue('"foo"'), 'foo');
});

test('.parseCookieName works as expected', () => {
assert.strictEqual(parseCookieName('foo'), 'foo');
assert.strictEqual(parseCookieName('m=m'), null);
});