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

test(serialize): additional tests for name, domain and path RFC validations #171

Merged
merged 4 commits into from
Oct 2, 2024
Merged
Changes from all commits
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
145 changes: 126 additions & 19 deletions test/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,98 @@ describe('cookie.serialize(name, value)', function () {
assert.equal(cookie.serialize('foo', ''), 'foo=')
})

it('should serialize valid name', function () {
var validNames = [
'foo',
'foo!bar',
'foo#bar',
'foo$bar',
"foo'bar",
'foo*bar',
'foo+bar',
'foo-bar',
'foo.bar',
'foo^bar',
'foo_bar',
'foo`bar',
'foo|bar',
'foo~bar',
'foo7bar',
];

validNames.forEach((name) => {
hdtmccallie marked this conversation as resolved.
Show resolved Hide resolved
assert.equal(cookie.serialize(name, 'baz'), `${name}=baz`, `Expected serialized value for name: "${name}"`);
});
});

it('should throw for invalid name', function () {
assert.throws(cookie.serialize.bind(cookie, 'foo\n', 'bar'), /argument name is invalid/)
assert.throws(cookie.serialize.bind(cookie, 'foo\u280a', 'bar'), /argument name is invalid/)
assert.throws(cookie.serialize.bind(cookie, 'foo bar', 'bar'), /argument name is invalid/)
})
})
var invalidNames = [
'foo\n',
'foo\u280a',
'foo/foo',
'foo,foo',
'foo;foo',
'foo@foo',
'foo[foo]',
'foo?foo',
'foo:foo',
'foo!foo',
hdtmccallie marked this conversation as resolved.
Show resolved Hide resolved
'foo{foo}',
'foo foo',
'foo\tfoo',
'foo"foo',
'foo<script>foo'
];

invalidNames.forEach((name) => {
assert.throws(
cookie.serialize.bind(cookie, name, 'bar'),
/argument name is invalid/,
`Expected an error for invalid name: "${name}"`
);
});
});
});

describe('cookie.serialize(name, value, options)', function () {
describe('with "domain" option', function () {
it('should serialize domain', function () {
assert.equal(cookie.serialize('foo', 'bar', { domain: 'example.com' }),
'foo=bar; Domain=example.com')
})

it('should throw for invalid value', function () {
assert.throws(cookie.serialize.bind(cookie, 'foo', 'bar', { domain: 'example.com\n' }),
/option domain is invalid/)
})
it('should serialize valid domain', function () {
const validDomains = [
'example.com',
'sub.example.com',
'my-site.org',
'localhost'
];

validDomains.forEach((domain) => {
assert.equal(
cookie.serialize('foo', 'bar', { domain }),
`foo=bar; Domain=${domain}`,
`Expected serialized value for domain: "${domain}"`
);
});
});

it('should throw for invalid domain', function () {
const invalidDomains = [
'example.com\n',
'sub.example.com\u0000',
'my site.org',
'domain..com',
'.example.com',
'example.com; Path=/',
'example.com /* inject a comment */'
];

invalidDomains.forEach((domain) => {
assert.throws(
cookie.serialize.bind(cookie, 'foo', 'bar', { domain }),
/option domain is invalid/,
`Expected an error for invalid domain: "${domain}"`
);
});
});
})

describe('with "encode" option', function () {
Expand Down Expand Up @@ -133,14 +207,47 @@ describe('cookie.serialize(name, value, options)', function () {

describe('with "path" option', function () {
it('should serialize path', function () {
assert.equal(cookie.serialize('foo', 'bar', { path: '/' }), 'foo=bar; Path=/')
})
const validPaths = [
'/',
'/login',
'/foo.bar/baz',
'/foo-bar',
'/foo=bar?baz',
'/foo"bar"',
'/../foo/bar',
'../foo/',
'./'
];

validPaths.forEach((path) => {
assert.equal(
cookie.serialize('foo', 'bar', { path }),
`foo=bar; Path=${path}`,
`Expected serialized value for path: "${path}"`
);
});
});

it('should throw for invalid value', function () {
assert.throws(cookie.serialize.bind(cookie, 'foo', 'bar', { path: '/\n' }),
/option path is invalid/)
})
})
const invalidPaths = [
'/\n',
'/foo\u0000',
'/foo bar',
hdtmccallie marked this conversation as resolved.
Show resolved Hide resolved
'/path/with\rnewline',
'/path\\with\\backslash',
hdtmccallie marked this conversation as resolved.
Show resolved Hide resolved
'/; Path=/sensitive-data',
'/login"><script>alert(1)</script>'
];

invalidPaths.forEach((path) => {
assert.throws(
cookie.serialize.bind(cookie, 'foo', 'bar', { path }),
/option path is invalid/,
`Expected an error for invalid path: "${path}"`
);
});
});
});

describe('with "priority" option', function () {
it('should throw on invalid priority', function () {
Expand Down
Loading