pax_global_header00006660000000000000000000000064147012741770014524gustar00rootroot0000000000000052 comment=5d61e1e2f3f18efe8f94e5fad2886b48864d6c78 expressjs-cookie-parser-5d61e1e/000077500000000000000000000000001470127417700167275ustar00rootroot00000000000000expressjs-cookie-parser-5d61e1e/.eslintignore000066400000000000000000000000261470127417700214300ustar00rootroot00000000000000coverage node_modules expressjs-cookie-parser-5d61e1e/.eslintrc.yml000066400000000000000000000002321470127417700213500ustar00rootroot00000000000000root: true extends: - standard - plugin:markdown/recommended plugins: - markdown overrides: - files: '**/*.md' processor: 'markdown/markdown' expressjs-cookie-parser-5d61e1e/.github/000077500000000000000000000000001470127417700202675ustar00rootroot00000000000000expressjs-cookie-parser-5d61e1e/.github/workflows/000077500000000000000000000000001470127417700223245ustar00rootroot00000000000000expressjs-cookie-parser-5d61e1e/.github/workflows/ci.yml000066400000000000000000000127051470127417700234470ustar00rootroot00000000000000name: ci on: - pull_request - push jobs: test: runs-on: ubuntu-latest strategy: matrix: name: - Node.js 0.8 - Node.js 0.10 - Node.js 0.12 - io.js 1.x - io.js 2.x - io.js 3.x - Node.js 4.x - Node.js 5.x - Node.js 6.x - Node.js 7.x - Node.js 8.x - Node.js 9.x - Node.js 10.x - Node.js 11.x - Node.js 12.x - Node.js 13.x - Node.js 14.x - Node.js 15.x - Node.js 16.x - Node.js 17.x - Node.js 18.x - Node.js 19.x - Node.js 20.x - Node.js 21.x - Node.js 22.x include: - name: Node.js 0.8 node-version: "0.8" npm-i: mocha@2.5.3 supertest@1.1.0 npm-rm: nyc - name: Node.js 0.10 node-version: "0.10" npm-i: mocha@3.5.3 nyc@10.3.2 supertest@2.0.0 - name: Node.js 0.12 node-version: "0.12" npm-i: mocha@3.5.3 nyc@10.3.2 supertest@2.0.0 - name: io.js 1.x node-version: "1.8" npm-i: mocha@3.5.3 nyc@10.3.2 supertest@2.0.0 - name: io.js 2.x node-version: "2.5" npm-i: mocha@3.5.3 nyc@10.3.2 supertest@2.0.0 - name: io.js 3.x node-version: "3.3" npm-i: mocha@3.5.3 nyc@10.3.2 supertest@2.0.0 - name: Node.js 4.x node-version: "4.9" npm-i: mocha@5.2.0 nyc@11.9.0 supertest@3.4.2 - name: Node.js 5.x node-version: "5.12" npm-i: mocha@5.2.0 nyc@11.9.0 supertest@3.4.2 - name: Node.js 6.x node-version: "6.17" npm-i: mocha@6.2.2 nyc@14.1.1 supertest@3.4.2 - name: Node.js 7.x node-version: "7.10" npm-i: mocha@6.2.2 nyc@14.1.1 - name: Node.js 8.x node-version: "8.17" npm-i: mocha@7.2.0 nyc@14.1.1 - name: Node.js 9.x node-version: "9.11" npm-i: mocha@7.2.0 nyc@14.1.1 - name: Node.js 10.x node-version: "10.24" npm-i: mocha@8.4.0 - name: Node.js 11.x node-version: "11.15" npm-i: mocha@8.4.0 - name: Node.js 12.x node-version: "12.22" - name: Node.js 13.x node-version: "13.14" - name: Node.js 14.x node-version: "14.19" - name: Node.js 15.x node-version: "15.14" - name: Node.js 16.x node-version: "16.14" - name: Node.js 17.x node-version: "17.6" - name: Node.js 18.x node-version: "18.14" - name: Node.js 19.x node-version: "19.6" - name: Node.js 20.x node-version: "20.12" - name: Node.js 21.x node-version: "21.7" - name: Node.js 22.x node-version: "22.0" steps: - uses: actions/checkout@v4 - name: Install Node.js ${{ matrix.node-version }} shell: bash -eo pipefail -l {0} run: | nvm install --default ${{ matrix.node-version }} if [[ "${{ matrix.node-version }}" == 0.* && "$(cut -d. -f2 <<< "${{ matrix.node-version }}")" -lt 10 ]]; then nvm install --alias=npm 0.10 nvm use ${{ matrix.node-version }} sed -i '1s;^.*$;'"$(printf '#!%q' "$(nvm which npm)")"';' "$(readlink -f "$(which npm)")" npm config set strict-ssl false fi dirname "$(nvm which ${{ matrix.node-version }})" >> "$GITHUB_PATH" - name: Configure npm run: | if [[ "$(npm config get package-lock)" == "true" ]]; then npm config set package-lock false else npm config set shrinkwrap false fi - name: Remove npm module(s) ${{ matrix.npm-rm }} run: npm rm --silent --save-dev ${{ matrix.npm-rm }} if: matrix.npm-rm != '' - name: Install npm module(s) ${{ matrix.npm-i }} run: npm install --save-dev ${{ matrix.npm-i }} if: matrix.npm-i != '' - name: Setup Node.js version-specific dependencies shell: bash run: | # eslint for linting # - remove on Node.js < 10 if [[ "$(cut -d. -f1 <<< "${{ matrix.node-version }}")" -lt 10 ]]; then node -pe 'Object.keys(require("./package").devDependencies).join("\n")' | \ grep -E '^eslint(-|$)' | \ sort -r | \ xargs -n1 npm rm --silent --save-dev fi - name: Install Node.js dependencies run: npm install - name: List environment id: list_env shell: bash run: | echo "node@$(node -v)" echo "npm@$(npm -v)" npm -s ls ||: (npm -s ls --depth=0 ||:) | awk -F'[ @]' 'NR>1 && $2 { print "" $2 "=" $3 }' >> "$GITHUB_OUTPUT" - name: Run tests shell: bash run: | if npm -ps ls nyc | grep -q nyc; then npm run test-ci else npm test fi - name: Lint code if: steps.list_env.outputs.eslint != '' run: npm run lint - name: Collect code coverage uses: coverallsapp/github-action@master if: steps.list_env.outputs.nyc != '' with: github-token: ${{ secrets.GITHUB_TOKEN }} flag-name: run-${{ matrix.test_number }} parallel: true coverage: needs: test runs-on: ubuntu-latest steps: - name: Upload code coverage uses: coverallsapp/github-action@master with: github-token: ${{ secrets.github_token }} parallel-finished: true expressjs-cookie-parser-5d61e1e/.github/workflows/scorecard.yml000066400000000000000000000056461470127417700250270ustar00rootroot00000000000000# 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@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.2 with: persist-credentials: false - name: "Run analysis" uses: ossf/scorecard-action@0864cf19026789058feabb7e87baa5f140aac736 # v2.3.1 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@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 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 expressjs-cookie-parser-5d61e1e/.gitignore000066400000000000000000000000641470127417700207170ustar00rootroot00000000000000.nyc_output coverage node_modules package-lock.json expressjs-cookie-parser-5d61e1e/HISTORY.md000066400000000000000000000053451470127417700204210ustar00rootroot000000000000001.4.7 / 2024-10-08 ========== * deps: cookie@0.7.2 - Fix object assignment of `hasOwnProperty` * deps: cookie@0.7.1 - Allow leading dot for domain - Although not permitted in the spec, some users expect this to work and user agents ignore the leading dot according to spec - Add fast path for `serialize` without options, use `obj.hasOwnProperty` when parsing * deps: cookie@0.7.0 - perf: parse cookies ~10% faster - fix: narrow the validation of cookies to match RFC6265 - fix: add `main` to `package.json` for rspack * deps: cookie@0.6.0 - Add `partitioned` option * deps: cookie@0.5.0 - Add `priority` option - Fix `expires` option to reject invalid dates - pref: improve default decode speed - pref: remove slow string split in parse * deps: cookie@0.4.2 - pref: read value only when assigning in parse - pref: remove unnecessary regexp in parse 1.4.6 / 2021-11-16 ================== * deps: cookie@0.4.1 1.4.5 / 2020-03-14 ================== * deps: cookie@0.4.0 1.4.4 / 2019-02-12 ================== * perf: normalize `secret` argument only once 1.4.3 / 2016-05-26 ================== * deps: cookie@0.3.1 - perf: use for loop in parse 1.4.2 / 2016-05-20 ================== * deps: cookie@0.2.4 - perf: enable strict mode - perf: use for loop in parse - perf: use string concatenation for serialization 1.4.1 / 2016-01-11 ================== * deps: cookie@0.2.3 * perf: enable strict mode 1.4.0 / 2015-09-18 ================== * Accept array of secrets in addition to a single secret * Fix `JSONCookie` to return `undefined` for non-string arguments * Fix `signedCookie` to return `undefined` for non-string arguments * deps: cookie@0.2.2 1.3.5 / 2015-05-19 ================== * deps: cookie@0.1.3 - Slight optimizations 1.3.4 / 2015-02-15 ================== * deps: cookie-signature@1.0.6 1.3.3 / 2014-09-05 ================== * deps: cookie-signature@1.0.5 1.3.2 / 2014-06-26 ================== * deps: cookie-signature@1.0.4 - fix for timing attacks 1.3.1 / 2014-06-17 ================== * actually export `signedCookie` 1.3.0 / 2014-06-17 ================== * add `signedCookie` export for single cookie unsigning 1.2.0 / 2014-06-17 ================== * export parsing functions * `req.cookies` and `req.signedCookies` are now plain objects * slightly faster parsing of many cookies 1.1.0 / 2014-05-12 ================== * Support for NodeJS version 0.8 * deps: cookie@0.1.2 - Fix for maxAge == 0 - made compat with expires field - tweak maxAge NaN error message 1.0.1 / 2014-02-20 ================== * add missing dependencies 1.0.0 / 2014-02-15 ================== * Genesis from `connect` expressjs-cookie-parser-5d61e1e/LICENSE000066400000000000000000000022211470127417700177310ustar00rootroot00000000000000(The MIT License) Copyright (c) 2014 TJ Holowaychuk Copyright (c) 2015 Douglas Christopher Wilson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. expressjs-cookie-parser-5d61e1e/README.md000066400000000000000000000105721470127417700202130ustar00rootroot00000000000000# cookie-parser [![NPM Version][npm-version-image]][npm-url] [![NPM Downloads][npm-downloads-image]][npm-url] [![Build Status][ci-image]][ci-url] [![Test Coverage][coveralls-image]][coveralls-url] Parse `Cookie` header and populate `req.cookies` with an object keyed by the cookie names. Optionally you may enable signed cookie support by passing a `secret` string, which assigns `req.secret` so it may be used by other middleware. ## Installation ```sh $ npm install cookie-parser ``` ## API ```js var cookieParser = require('cookie-parser') ``` ### cookieParser(secret, options) Create a new cookie parser middleware function using the given `secret` and `options`. - `secret` a string or array used for signing cookies. This is optional and if not specified, will not parse signed cookies. If a string is provided, this is used as the secret. If an array is provided, an attempt will be made to unsign the cookie with each secret in order. - `options` an object that is passed to `cookie.parse` as the second option. See [cookie](https://www.npmjs.org/package/cookie) for more information. - `decode` a function to decode the value of the cookie The middleware will parse the `Cookie` header on the request and expose the cookie data as the property `req.cookies` and, if a `secret` was provided, as the property `req.signedCookies`. These properties are name value pairs of the cookie name to cookie value. When `secret` is provided, this module will unsign and validate any signed cookie values and move those name value pairs from `req.cookies` into `req.signedCookies`. A signed cookie is a cookie that has a value prefixed with `s:`. Signed cookies that fail signature validation will have the value `false` instead of the tampered value. In addition, this module supports special "JSON cookies". These are cookie where the value is prefixed with `j:`. When these values are encountered, the value will be exposed as the result of `JSON.parse`. If parsing fails, the original value will remain. ### cookieParser.JSONCookie(str) Parse a cookie value as a JSON cookie. This will return the parsed JSON value if it was a JSON cookie, otherwise, it will return the passed value. ### cookieParser.JSONCookies(cookies) Given an object, this will iterate over the keys and call `JSONCookie` on each value, replacing the original value with the parsed value. This returns the same object that was passed in. ### cookieParser.signedCookie(str, secret) Parse a cookie value as a signed cookie. This will return the parsed unsigned value if it was a signed cookie and the signature was valid. If the value was not signed, the original value is returned. If the value was signed but the signature could not be validated, `false` is returned. The `secret` argument can be an array or string. If a string is provided, this is used as the secret. If an array is provided, an attempt will be made to unsign the cookie with each secret in order. ### cookieParser.signedCookies(cookies, secret) Given an object, this will iterate over the keys and check if any value is a signed cookie. If it is a signed cookie and the signature is valid, the key will be deleted from the object and added to the new object that is returned. The `secret` argument can be an array or string. If a string is provided, this is used as the secret. If an array is provided, an attempt will be made to unsign the cookie with each secret in order. ## Example ```js var express = require('express') var cookieParser = require('cookie-parser') var app = express() app.use(cookieParser()) app.get('/', function (req, res) { // Cookies that have not been signed console.log('Cookies: ', req.cookies) // Cookies that have been signed console.log('Signed Cookies: ', req.signedCookies) }) app.listen(8080) // curl command that sends an HTTP request with two cookies // curl http://127.0.0.1:8080 --cookie "Cho=Kim;Greet=Hello" ``` ## License [MIT](LICENSE) [ci-image]: https://badgen.net/github/checks/expressjs/cookie-parser/master?label=ci [ci-url]: https://github.com/expressjs/cookie-parser/actions?query=workflow%3Aci [coveralls-image]: https://badgen.net/coveralls/c/github/expressjs/cookie-parser/master [coveralls-url]: https://coveralls.io/r/expressjs/cookie-parser?branch=master [npm-downloads-image]: https://badgen.net/npm/dm/cookie-parser [npm-url]: https://npmjs.org/package/cookie-parser [npm-version-image]: https://badgen.net/npm/v/cookie-parser expressjs-cookie-parser-5d61e1e/index.js000066400000000000000000000065051470127417700204020ustar00rootroot00000000000000/*! * cookie-parser * Copyright(c) 2014 TJ Holowaychuk * Copyright(c) 2015 Douglas Christopher Wilson * MIT Licensed */ 'use strict' /** * Module dependencies. * @private */ var cookie = require('cookie') var signature = require('cookie-signature') /** * Module exports. * @public */ module.exports = cookieParser module.exports.JSONCookie = JSONCookie module.exports.JSONCookies = JSONCookies module.exports.signedCookie = signedCookie module.exports.signedCookies = signedCookies /** * Parse Cookie header and populate `req.cookies` * with an object keyed by the cookie names. * * @param {string|array} [secret] A string (or array of strings) representing cookie signing secret(s). * @param {Object} [options] * @return {Function} * @public */ function cookieParser (secret, options) { var secrets = !secret || Array.isArray(secret) ? (secret || []) : [secret] return function cookieParser (req, res, next) { if (req.cookies) { return next() } var cookies = req.headers.cookie req.secret = secrets[0] req.cookies = Object.create(null) req.signedCookies = Object.create(null) // no cookies if (!cookies) { return next() } req.cookies = cookie.parse(cookies, options) // parse signed cookies if (secrets.length !== 0) { req.signedCookies = signedCookies(req.cookies, secrets) req.signedCookies = JSONCookies(req.signedCookies) } // parse JSON cookies req.cookies = JSONCookies(req.cookies) next() } } /** * Parse JSON cookie string. * * @param {String} str * @return {Object} Parsed object or undefined if not json cookie * @public */ function JSONCookie (str) { if (typeof str !== 'string' || str.substr(0, 2) !== 'j:') { return undefined } try { return JSON.parse(str.slice(2)) } catch (err) { return undefined } } /** * Parse JSON cookies. * * @param {Object} obj * @return {Object} * @public */ function JSONCookies (obj) { var cookies = Object.keys(obj) var key var val for (var i = 0; i < cookies.length; i++) { key = cookies[i] val = JSONCookie(obj[key]) if (val) { obj[key] = val } } return obj } /** * Parse a signed cookie string, return the decoded value. * * @param {String} str signed cookie string * @param {string|array} secret * @return {String} decoded value * @public */ function signedCookie (str, secret) { if (typeof str !== 'string') { return undefined } if (str.substr(0, 2) !== 's:') { return str } var secrets = !secret || Array.isArray(secret) ? (secret || []) : [secret] for (var i = 0; i < secrets.length; i++) { var val = signature.unsign(str.slice(2), secrets[i]) if (val !== false) { return val } } return false } /** * Parse signed cookies, returning an object containing the decoded key/value * pairs, while removing the signed key from obj. * * @param {Object} obj * @param {string|array} secret * @return {Object} * @public */ function signedCookies (obj, secret) { var cookies = Object.keys(obj) var dec var key var ret = Object.create(null) var val for (var i = 0; i < cookies.length; i++) { key = cookies[i] val = obj[key] dec = signedCookie(val, secret) if (val !== dec) { ret[key] = dec delete obj[key] } } return ret } expressjs-cookie-parser-5d61e1e/package.json000066400000000000000000000021741470127417700212210ustar00rootroot00000000000000{ "name": "cookie-parser", "description": "Parse HTTP request cookies", "version": "1.4.7", "author": "TJ Holowaychuk (http://tjholowaychuk.com)", "contributors": [ "Douglas Christopher Wilson " ], "license": "MIT", "repository": "expressjs/cookie-parser", "keywords": [ "cookie", "middleware" ], "dependencies": { "cookie": "0.7.2", "cookie-signature": "1.0.6" }, "devDependencies": { "eslint": "7.32.0", "eslint-config-standard": "14.1.1", "eslint-plugin-import": "2.25.2", "eslint-plugin-markdown": "2.2.1", "eslint-plugin-node": "11.1.0", "eslint-plugin-promise": "4.3.1", "eslint-plugin-standard": "4.1.0", "mocha": "9.2.1", "nyc": "15.1.0", "supertest": "6.1.6" }, "files": [ "LICENSE", "HISTORY.md", "index.js" ], "engines": { "node": ">= 0.8.0" }, "scripts": { "lint": "eslint .", "test": "mocha --reporter spec --bail --check-leaks test/", "test-ci": "nyc --reporter=lcov --reporter=text npm test", "test-cov": "nyc --reporter=html --reporter=text npm test" } } expressjs-cookie-parser-5d61e1e/test/000077500000000000000000000000001470127417700177065ustar00rootroot00000000000000expressjs-cookie-parser-5d61e1e/test/.eslintrc.yml000066400000000000000000000000231470127417700223250ustar00rootroot00000000000000env: mocha: true expressjs-cookie-parser-5d61e1e/test/cookieParser.js000066400000000000000000000241561470127417700227020ustar00rootroot00000000000000 var assert = require('assert') var cookieParser = require('..') var http = require('http') var request = require('supertest') var signature = require('cookie-signature') describe('cookieParser()', function () { it('should export JSONCookies function', function () { assert(typeof cookieParser.JSONCookies, 'function') }) describe('when no cookies are sent', function () { it('should default req.cookies to {}', function (done) { request(createServer('keyboard cat')) .get('/') .expect(200, '{}', done) }) it('should default req.signedCookies to {}', function (done) { request(createServer('keyboard cat')) .get('/signed') .expect(200, '{}', done) }) }) describe('when cookies are sent', function () { it('should populate req.cookies', function (done) { request(createServer('keyboard cat')) .get('/') .set('Cookie', 'foo=bar; bar=baz') .expect(200, '{"foo":"bar","bar":"baz"}', done) }) it('should inflate JSON cookies', function (done) { request(createServer('keyboard cat')) .get('/') .set('Cookie', 'foo=j:{"foo":"bar"}') .expect(200, '{"foo":{"foo":"bar"}}', done) }) it('should not inflate invalid JSON cookies', function (done) { request(createServer('keyboard cat')) .get('/') .set('Cookie', 'foo=j:{"foo":') .expect(200, '{"foo":"j:{\\"foo\\":"}', done) }) }) describe('when req.cookies exists', function () { it('should do nothing', function (done) { var _parser = cookieParser() var server = http.createServer(function (req, res) { req.cookies = { fizz: 'buzz' } _parser(req, res, function (err) { if (err) { res.statusCode = 500 res.end(err.message) return } res.end(JSON.stringify(req.cookies)) }) }) request(server) .get('/') .set('Cookie', 'foo=bar; bar=baz') .expect(200, '{"fizz":"buzz"}', done) }) }) describe('when a secret is given', function () { var val = signature.sign('foobarbaz', 'keyboard cat') // TODO: "bar" fails... it('should populate req.signedCookies', function (done) { request(createServer('keyboard cat')) .get('/signed') .set('Cookie', 'foo=s:' + val) .expect(200, '{"foo":"foobarbaz"}', done) }) it('should remove the signed value from req.cookies', function (done) { request(createServer('keyboard cat')) .get('/') .set('Cookie', 'foo=s:' + val) .expect(200, '{}', done) }) it('should omit invalid signatures', function (done) { var server = createServer('keyboard cat') request(server) .get('/signed') .set('Cookie', 'foo=' + val + '3') .expect(200, '{}', function (err) { if (err) return done(err) request(server) .get('/') .set('Cookie', 'foo=' + val + '3') .expect(200, '{"foo":"foobarbaz.CP7AWaXDfAKIRfH49dQzKJx7sKzzSoPq7/AcBBRVwlI3"}', done) }) }) }) describe('when multiple secrets are given', function () { it('should populate req.signedCookies', function (done) { request(createServer(['keyboard cat', 'nyan cat'])) .get('/signed') .set('Cookie', 'buzz=s:foobar.N5r0C3M8W+IPpzyAJaIddMWbTGfDSO+bfKlZErJ+MeE; fizz=s:foobar.JTCAgiMWsnuZpN3mrYnEUjXlGxmDi4POCBnWbRxse88') .expect(200, '{"buzz":"foobar","fizz":"foobar"}', done) }) }) describe('when no secret is given', function () { var server before(function () { server = createServer() }) it('should populate req.cookies', function (done) { request(server) .get('/') .set('Cookie', 'foo=bar; bar=baz') .expect(200, '{"foo":"bar","bar":"baz"}', done) }) it('should not populate req.signedCookies', function (done) { var val = signature.sign('foobarbaz', 'keyboard cat') request(server) .get('/signed') .set('Cookie', 'foo=s:' + val) .expect(200, '{}', done) }) }) }) describe('cookieParser.JSONCookie(str)', function () { it('should return undefined for non-string arguments', function () { assert.strictEqual(cookieParser.JSONCookie(), undefined) assert.strictEqual(cookieParser.JSONCookie(undefined), undefined) assert.strictEqual(cookieParser.JSONCookie(null), undefined) assert.strictEqual(cookieParser.JSONCookie(42), undefined) assert.strictEqual(cookieParser.JSONCookie({}), undefined) assert.strictEqual(cookieParser.JSONCookie([]), undefined) assert.strictEqual(cookieParser.JSONCookie(function () {}), undefined) }) it('should return undefined for non-JSON cookie string', function () { assert.strictEqual(cookieParser.JSONCookie(''), undefined) assert.strictEqual(cookieParser.JSONCookie('foo'), undefined) assert.strictEqual(cookieParser.JSONCookie('{}'), undefined) }) it('should return object for JSON cookie string', function () { assert.deepEqual(cookieParser.JSONCookie('j:{"foo":"bar"}'), { foo: 'bar' }) }) it('should return undefined on invalid JSON', function () { assert.strictEqual(cookieParser.JSONCookie('j:{foo:"bar"}'), undefined) }) }) describe('cookieParser.signedCookie(str, secret)', function () { it('should return undefined for non-string arguments', function () { assert.strictEqual(cookieParser.signedCookie(undefined, 'keyboard cat'), undefined) assert.strictEqual(cookieParser.signedCookie(null, 'keyboard cat'), undefined) assert.strictEqual(cookieParser.signedCookie(42, 'keyboard cat'), undefined) assert.strictEqual(cookieParser.signedCookie({}, 'keyboard cat'), undefined) assert.strictEqual(cookieParser.signedCookie([], 'keyboard cat'), undefined) assert.strictEqual(cookieParser.signedCookie(function () {}, 'keyboard cat'), undefined) }) it('should pass through non-signed string', function () { assert.strictEqual(cookieParser.signedCookie('', 'keyboard cat'), '') assert.strictEqual(cookieParser.signedCookie('foo', 'keyboard cat'), 'foo') assert.strictEqual(cookieParser.signedCookie('j:{}', 'keyboard cat'), 'j:{}') }) it('should return false for tampered signed string', function () { assert.strictEqual(cookieParser.signedCookie('s:foobaz.N5r0C3M8W+IPpzyAJaIddMWbTGfDSO+bfKlZErJ+MeE', 'keyboard cat'), false) }) it('should return unsigned value for signed string', function () { assert.strictEqual(cookieParser.signedCookie('s:foobar.N5r0C3M8W+IPpzyAJaIddMWbTGfDSO+bfKlZErJ+MeE', 'keyboard cat'), 'foobar') }) describe('when secret is an array', function () { it('should return false for tampered signed string', function () { assert.strictEqual(cookieParser.signedCookie('s:foobaz.N5r0C3M8W+IPpzyAJaIddMWbTGfDSO+bfKlZErJ+MeE', [ 'keyboard cat', 'nyan cat' ]), false) }) it('should return unsigned value for first secret', function () { assert.strictEqual(cookieParser.signedCookie('s:foobar.N5r0C3M8W+IPpzyAJaIddMWbTGfDSO+bfKlZErJ+MeE', [ 'keyboard cat', 'nyan cat' ]), 'foobar') }) it('should return unsigned value for second secret', function () { assert.strictEqual(cookieParser.signedCookie('s:foobar.JTCAgiMWsnuZpN3mrYnEUjXlGxmDi4POCBnWbRxse88', [ 'keyboard cat', 'nyan cat' ]), 'foobar') }) }) }) describe('cookieParser.signedCookies(obj, secret)', function () { it('should ignore non-signed strings', function () { assert.deepEqual(cookieParser.signedCookies({}, 'keyboard cat'), {}) assert.deepEqual(cookieParser.signedCookies({ foo: 'bar' }, 'keyboard cat'), {}) }) it('should include tampered strings as false', function () { assert.deepEqual(cookieParser.signedCookies({ foo: 's:foobaz.N5r0C3M8W+IPpzyAJaIddMWbTGfDSO+bfKlZErJ+MeE' }, 'keyboard cat'), { foo: false }) }) it('should include unsigned strings', function () { assert.deepEqual(cookieParser.signedCookies({ foo: 's:foobar.N5r0C3M8W+IPpzyAJaIddMWbTGfDSO+bfKlZErJ+MeE' }, 'keyboard cat'), { foo: 'foobar' }) }) it('should remove signed strings from original object', function () { var obj = { foo: 's:foobar.N5r0C3M8W+IPpzyAJaIddMWbTGfDSO+bfKlZErJ+MeE' } assert.deepEqual(cookieParser.signedCookies(obj, 'keyboard cat'), { foo: 'foobar' }) assert.deepEqual(obj, {}) }) it('should remove tampered strings from original object', function () { var obj = { foo: 's:foobaz.N5r0C3M8W+IPpzyAJaIddMWbTGfDSO+bfKlZErJ+MeE' } assert.deepEqual(cookieParser.signedCookies(obj, 'keyboard cat'), { foo: false }) assert.deepEqual(obj, {}) }) it('should leave unsigned string in original object', function () { var obj = { fizz: 'buzz', foo: 's:foobar.N5r0C3M8W+IPpzyAJaIddMWbTGfDSO+bfKlZErJ+MeE' } assert.deepEqual(cookieParser.signedCookies(obj, 'keyboard cat'), { foo: 'foobar' }) assert.deepEqual(obj, { fizz: 'buzz' }) }) describe('when secret is an array', function () { it('should include unsigned strings for matching secrets', function () { var obj = { buzz: 's:foobar.N5r0C3M8W+IPpzyAJaIddMWbTGfDSO+bfKlZErJ+MeE', fizz: 's:foobar.JTCAgiMWsnuZpN3mrYnEUjXlGxmDi4POCBnWbRxse88' } assert.deepEqual(cookieParser.signedCookies(obj, ['keyboard cat']), { buzz: 'foobar', fizz: false }) }) it('should include unsigned strings for all secrets', function () { var obj = { buzz: 's:foobar.N5r0C3M8W+IPpzyAJaIddMWbTGfDSO+bfKlZErJ+MeE', fizz: 's:foobar.JTCAgiMWsnuZpN3mrYnEUjXlGxmDi4POCBnWbRxse88' } assert.deepEqual(cookieParser.signedCookies(obj, ['keyboard cat', 'nyan cat']), { buzz: 'foobar', fizz: 'foobar' }) }) }) }) function createServer (secret) { var _parser = cookieParser(secret) return http.createServer(function (req, res) { _parser(req, res, function (err) { if (err) { res.statusCode = 500 res.end(err.message) return } var cookies = req.url === '/signed' ? req.signedCookies : req.cookies res.end(JSON.stringify(cookies)) }) }) }