Skip to content

Commit

Permalink
Merge branch 'master' into test/cookie-rfc-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey authored Oct 2, 2024
2 parents f3c7217 + 927d48a commit 0bb3ecb
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 14 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,39 +105,50 @@ jobs:
- name: Node.js 10.x
node-version: "10.24"
npm-i: [email protected]
npm-rm: beautify-benchmark benchmark top-sites

- name: Node.js 11.x
node-version: "11.15"
npm-i: [email protected]
npm-rm: beautify-benchmark benchmark top-sites

- name: Node.js 12.x
node-version: "12.22"
npm-i: [email protected]
npm-rm: beautify-benchmark benchmark top-sites

- name: Node.js 13.x
node-version: "13.14"
npm-i: [email protected]
npm-rm: beautify-benchmark benchmark top-sites

- name: Node.js 14.x
node-version: "14.21"
npm-rm: beautify-benchmark benchmark top-sites

- name: Node.js 15.x
node-version: "15.14"
npm-rm: beautify-benchmark benchmark top-sites

- name: Node.js 16.x
node-version: "16.20"
npm-rm: beautify-benchmark benchmark top-sites

- name: Node.js 17.x
node-version: "17.9"
npm-rm: beautify-benchmark benchmark top-sites

- name: Node.js 18.x
node-version: "18.18"
npm-rm: beautify-benchmark benchmark top-sites

- name: Node.js 19.x
node-version: "19.9"
npm-rm: beautify-benchmark benchmark top-sites

- name: Node.js 20.x
node-version: "20.9"
npm-rm: beautify-benchmark benchmark top-sites

- name: Node.js 21.x
node-version: "21.1"
Expand Down
72 changes: 72 additions & 0 deletions .github/workflows/scorecard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# This workflow uses actions that are not certified by GitHub. They are provided
# by a third-party and are governed by separate terms of service, privacy
# policy, and support documentation.

name: Scorecard supply-chain security
on:
# For Branch-Protection check. Only the default branch is supported. See
# https://github.com/ossf/scorecard/blob/main/docs/checks.md#branch-protection
branch_protection_rule:
# To guarantee Maintained check is occasionally updated. See
# https://github.com/ossf/scorecard/blob/main/docs/checks.md#maintained
schedule:
- cron: '16 21 * * 1'
push:
branches: [ "master" ]

# Declare default permissions as read only.
permissions: read-all

jobs:
analysis:
name: Scorecard analysis
runs-on: ubuntu-latest
permissions:
# Needed to upload the results to code-scanning dashboard.
security-events: write
# Needed to publish results and get a badge (see publish_results below).
id-token: write
# Uncomment the permissions below if installing in a private repository.
# contents: read
# actions: read

steps:
- name: "Checkout code"
uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0
with:
persist-credentials: false

- name: "Run analysis"
uses: ossf/scorecard-action@99c53751e09b9529366343771cc321ec74e9bd3d # v2.0.6
with:
results_file: results.sarif
results_format: sarif
# (Optional) "write" PAT token. Uncomment the `repo_token` line below if:
# - you want to enable the Branch-Protection check on a *public* repository, or
# - you are installing Scorecard on a *private* repository
# To create the PAT, follow the steps in https://github.com/ossf/scorecard-action#authentication-with-pat.
# repo_token: ${{ secrets.SCORECARD_TOKEN }}

# Public repositories:
# - Publish results to OpenSSF REST API for easy access by consumers
# - Allows the repository to include the Scorecard badge.
# - See https://github.com/ossf/scorecard-action#publishing-results.
# For private repositories:
# - `publish_results` will always be set to `false`, regardless
# of the value entered here.
publish_results: true

# Upload the results as artifacts (optional). Commenting out will disable uploads of run results in SARIF
# format to the repository Actions tab.
- name: "Upload artifact"
uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 # v3.1.3
with:
name: SARIF file
path: results.sarif
retention-days: 5

# Upload the results to GitHub's code scanning dashboard.
- name: "Upload to code-scanning"
uses: github/codeql-action/upload-sarif@2f93e4319b2f04a2efc38fa7f78bd681bc3f7b2f # v2.23.2
with:
sarif_file: results.sarif
68 changes: 57 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,60 @@ exports.serialize = serialize;
var __toString = Object.prototype.toString

/**
* RegExp to match field-content in RFC 7230 sec 3.2
* RegExp to match cookie-name in RFC 6265 sec 4.1.1
* This refers out to the obsoleted definition of token in RFC 2616 sec 2.2
* which has been replaced by the token definition in RFC 7230 appendix B.
*
* field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
* field-vchar = VCHAR / obs-text
* obs-text = %x80-FF
* cookie-name = token
* token = 1*tchar
* tchar = "!" / "#" / "$" / "%" / "&" / "'" /
* "*" / "+" / "-" / "." / "^" / "_" /
* "`" / "|" / "~" / DIGIT / ALPHA
*/

var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
var cookieNameRegExp = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;

/**
* RegExp to match cookie-value in RFC 6265 sec 4.1.1
*
* cookie-value = *cookie-octet / ( DQUOTE *cookie-octet DQUOTE )
* cookie-octet = %x21 / %x23-2B / %x2D-3A / %x3C-5B / %x5D-7E
* ; US-ASCII characters excluding CTLs,
* ; whitespace DQUOTE, comma, semicolon,
* ; and backslash
*/

var cookieValueRegExp = /^("?)[\u0021\u0023-\u002B\u002D-\u003A\u003C-\u005B\u005D-\u007E]*\1$/;

/**
* RegExp to match domain-value in RFC 6265 sec 4.1.1
*
* domain-value = <subdomain>
* ; defined in [RFC1034], Section 3.5, as
* ; enhanced by [RFC1123], Section 2.1
* <subdomain> = <label> | <subdomain> "." <label>
* <label> = <let-dig> [ [ <ldh-str> ] <let-dig> ]
* Labels must be 63 characters or less.
* 'let-dig' not 'letter' in the first char, per RFC1123
* <ldh-str> = <let-dig-hyp> | <let-dig-hyp> <ldh-str>
* <let-dig-hyp> = <let-dig> | "-"
* <let-dig> = <letter> | <digit>
* <letter> = any one of the 52 alphabetic characters A through Z in
* upper case and a through z in lower case
* <digit> = any one of the ten digits 0 through 9
*/

var domainValueRegExp = /^([a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)([.][a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)*$/i;

/**
* RegExp to match path-value in RFC 6265 sec 4.1.1
*
* path-value = <any CHAR except CTLs or ";">
* CHAR = %x01-7F
* ; defined in RFC 5234 appendix B.1
*/

var pathValueRegExp = /^[\u0020-\u003A\u003D-\u007E]*$/;

/**
* Parse a cookie header.
Expand Down Expand Up @@ -95,8 +141,8 @@ function parse(str, options) {
/**
* Serialize data into a cookie header.
*
* Serialize the a name value pair into a cookie string suitable for
* http headers. An optional options object specified cookie parameters.
* Serialize a name value pair into a cookie string suitable for
* http headers. An optional options object specifies cookie parameters.
*
* serialize('foo', 'bar', { httpOnly: true })
* => "foo=bar; httpOnly"
Expand All @@ -116,13 +162,13 @@ function serialize(name, val, options) {
throw new TypeError('option encode is invalid');
}

if (!fieldContentRegExp.test(name)) {
if (!cookieNameRegExp.test(name)) {
throw new TypeError('argument name is invalid');
}

var value = enc(val);

if (value && !fieldContentRegExp.test(value)) {
if (value && !cookieValueRegExp.test(value)) {
throw new TypeError('argument val is invalid');
}

Expand All @@ -139,15 +185,15 @@ function serialize(name, val, options) {
}

if (opt.domain) {
if (!fieldContentRegExp.test(opt.domain)) {
if (!domainValueRegExp.test(opt.domain)) {
throw new TypeError('option domain is invalid');
}

str += '; Domain=' + opt.domain;
}

if (opt.path) {
if (!fieldContentRegExp.test(opt.path)) {
if (!pathValueRegExp.test(opt.path)) {
throw new TypeError('option path is invalid');
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"SECURITY.md",
"index.js"
],
"main": "index.js",
"engines": {
"node": ">= 0.6"
},
Expand Down
9 changes: 6 additions & 3 deletions test/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('cookie.serialize(name, value)', function () {
})

it('should serialize valid name', function () {
const validNames = [
var validNames = [
'foo',
'foo!bar',
'foo#bar',
Expand All @@ -42,7 +42,7 @@ describe('cookie.serialize(name, value)', function () {
});

it('should throw for invalid name', function () {
const invalidNames = [
var invalidNames = [
'foo\n',
'foo\u280a',
'foo/foo',
Expand All @@ -68,7 +68,7 @@ describe('cookie.serialize(name, value)', function () {
);
});
});
})
});

describe('cookie.serialize(name, value, options)', function () {
describe('with "domain" option', function () {
Expand Down Expand Up @@ -127,6 +127,9 @@ describe('cookie.serialize(name, value, options)', function () {
assert.throws(cookie.serialize.bind(cookie, 'foo', '+ \n', {
encode: function (v) { return v }
}), /argument val is invalid/)
assert.throws(cookie.serialize.bind(cookie, 'foo', 'foo bar', {
encode: function (v) { return v }
}), /argument val is invalid/)
})
})

Expand Down

0 comments on commit 0bb3ecb

Please sign in to comment.