pax_global_header00006660000000000000000000000064124025105610014506gustar00rootroot0000000000000052 comment=893bf91a8a4e49c63b6e32065991b13a7d27ed4c cookie-parser-1.3.3/000077500000000000000000000000001240251056100142555ustar00rootroot00000000000000cookie-parser-1.3.3/.gitignore000066400000000000000000000000261240251056100162430ustar00rootroot00000000000000coverage node_modules cookie-parser-1.3.3/.travis.yml000066400000000000000000000003711240251056100163670ustar00rootroot00000000000000language: node_js node_js: - "0.8" - "0.10" - "0.11" matrix: allow_failures: - node_js: "0.11" fast_finish: true script: "npm run-script test-travis" after_script: "npm install coveralls@2.10.0 && cat ./coverage/lcov.info | coveralls" cookie-parser-1.3.3/HISTORY.md000066400000000000000000000015321240251056100157410ustar00rootroot000000000000001.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` cookie-parser-1.3.3/LICENSE000066400000000000000000000021121240251056100152560ustar00rootroot00000000000000(The MIT License) Copyright (c) 2014 TJ Holowaychuk 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. cookie-parser-1.3.3/README.md000066400000000000000000000054121240251056100155360ustar00rootroot00000000000000# cookie-parser [![NPM Version][npm-image]][npm-url] [![NPM Downloads][downloads-image]][downloads-url] [![Build Status][travis-image]][travis-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 express = require('express') var cookieParser = require('cookie-parser') var app = express() app.use(cookieParser()) ``` ### cookieParser(secret, options) - `secret` a string used for signing cookies. This is optional and if not specified, will not parse signed cookies. - `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 ### 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. This will return the same object 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, otherwise it will return the passed value. ### 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. ## Example ```js var express = require('express') var cookieParser = require('cookie-parser') var app = express() app.use(cookieParser()) app.get('/', function(req, res) { console.log("Cookies: ", req.cookies) }) 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" ``` ### [MIT Licensed](LICENSE) [npm-image]: https://img.shields.io/npm/v/cookie-parser.svg?style=flat [npm-url]: https://npmjs.org/package/cookie-parser [travis-image]: https://img.shields.io/travis/expressjs/cookie-parser.svg?style=flat [travis-url]: https://travis-ci.org/expressjs/cookie-parser [coveralls-image]: https://img.shields.io/coveralls/expressjs/cookie-parser.svg?style=flat [coveralls-url]: https://coveralls.io/r/expressjs/cookie-parser?branch=master [downloads-image]: https://img.shields.io/npm/dm/cookie-parser.svg?style=flat [downloads-url]: https://npmjs.org/package/cookie-parser cookie-parser-1.3.3/index.js000066400000000000000000000023441240251056100157250ustar00rootroot00000000000000/*! * cookie-parser * MIT Licensed */ /** * Module dependencies. */ var cookie = require('cookie'); var parse = require('./lib/parse'); /** * Parse Cookie header and populate `req.cookies` * with an object keyed by the cookie names. * * @param {String} [secret] * @param {Object} [options] * @return {Function} * @api public */ exports = module.exports = function cookieParser(secret, options){ return function cookieParser(req, res, next) { if (req.cookies) return next(); var cookies = req.headers.cookie; req.secret = secret; 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 (secret) { req.signedCookies = parse.signedCookies(req.cookies, secret); req.signedCookies = parse.JSONCookies(req.signedCookies); } // parse JSON cookies req.cookies = parse.JSONCookies(req.cookies); next(); }; }; /** * Export parsing functions. */ exports.JSONCookie = parse.JSONCookie; exports.JSONCookies = parse.JSONCookies; exports.signedCookie = parse.signedCookie; exports.signedCookies = parse.signedCookies; cookie-parser-1.3.3/lib/000077500000000000000000000000001240251056100150235ustar00rootroot00000000000000cookie-parser-1.3.3/lib/parse.js000066400000000000000000000031371240251056100164770ustar00rootroot00000000000000var signature = require('cookie-signature'); /** * Parse signed cookies, returning an object * containing the decoded key/value pairs, * while removing the signed key from `obj`. * * @param {Object} obj * @return {Object} * @api private */ exports.signedCookies = function(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 = exports.signedCookie(val, secret); if (val !== dec) { ret[key] = dec; delete obj[key]; } } return ret; }; /** * Parse a signed cookie string, return the decoded value * * @param {String} str signed cookie string * @param {String} secret * @return {String} decoded value * @api private */ exports.signedCookie = function(str, secret){ return str.substr(0, 2) === 's:' ? signature.unsign(str.slice(2), secret) : str; }; /** * Parse JSON cookies. * * @param {Object} obj * @return {Object} * @api private */ exports.JSONCookies = function(obj){ var cookies = Object.keys(obj); var key; var val; for (var i = 0; i < cookies.length; i++) { key = cookies[i]; val = exports.JSONCookie(obj[key]); if (val) { obj[key] = val; } } return obj; }; /** * Parse JSON cookie string * * @param {String} str * @return {Object} Parsed object or null if not json cookie * @api private */ exports.JSONCookie = function(str) { if (!str || str.substr(0, 2) !== 'j:') return; try { return JSON.parse(str.slice(2)); } catch (err) { // no op } }; cookie-parser-1.3.3/package.json000066400000000000000000000016171240251056100165500ustar00rootroot00000000000000{ "name": "cookie-parser", "description": "cookie parsing with signatures", "version": "1.3.3", "author": "TJ Holowaychuk (http://tjholowaychuk.com)", "licenses": "MIT", "repository": "expressjs/cookie-parser", "keywords": [ "cookie", "middleware" ], "dependencies": { "cookie": "0.1.2", "cookie-signature": "1.0.5" }, "devDependencies": { "istanbul": "0.3.2", "mocha": "~1.21.4", "supertest": "~0.13.0" }, "files": [ "lib/", "LICENSE", "HISTORY.md", "index.js" ], "engines": { "node": ">= 0.8.0" }, "scripts": { "test": "mocha --reporter spec --bail --check-leaks test/", "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/", "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/" } } cookie-parser-1.3.3/test/000077500000000000000000000000001240251056100152345ustar00rootroot00000000000000cookie-parser-1.3.3/test/cookieParser.js000066400000000000000000000071531240251056100202260ustar00rootroot00000000000000 var assert = require('assert') var cookieParser = require('..') var http = require('http') var request = require('supertest') var signature = require('cookie-signature') describe('cookieParser()', function(){ var server before(function(){ server = createServer('keyboard cat') }) it('should export JSONCookie function', function(){ assert(typeof cookieParser.JSONCookie, 'function') }) it('should export JSONCookies function', function(){ assert(typeof cookieParser.JSONCookies, 'function') }) it('should export signedCookie function', function(){ assert(typeof cookieParser.signedCookie, 'function') }) it('should export signedCookies function', function(){ assert(typeof cookieParser.signedCookies, 'function') }) describe('when no cookies are sent', function(){ it('should default req.cookies to {}', function(done){ request(server) .get('/') .expect(200, '{}', done) }) it('should default req.signedCookies to {}', function(done){ request(server) .get('/signed') .expect(200, '{}', done) }) }) describe('when cookies are sent', function(){ 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 inflate JSON cookies', function(done){ request(server) .get('/') .set('Cookie', 'foo=j:{"foo":"bar"}') .expect(200, '{"foo":{"foo":"bar"}}', done) }) it('should not inflate invalid JSON cookies', function(done){ request(server) .get('/') .set('Cookie', 'foo=j:{"foo":') .expect(200, '{"foo":"j:{\\"foo\\":"}', 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(server) .get('/signed') .set('Cookie', 'foo=s:' + val) .expect(200, '{"foo":"foobarbaz"}', done) }) it('should remove the signed value from req.cookies', function(done){ request(server) .get('/') .set('Cookie', 'foo=s:' + val) .expect(200, '{}', done) }) it('should omit invalid signatures', function(done){ server.listen() 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 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) }) }) }) 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 = '/signed' === req.url ? req.signedCookies : req.cookies res.end(JSON.stringify(cookies)) }) }) }