-
-
Notifications
You must be signed in to change notification settings - Fork 157
chore: minimally replaces keygrip dep #174
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
Open
yowainwright
wants to merge
2
commits into
pillarjs:master
Choose a base branch
from
yowainwright:KOA_ISSUE_1915
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ var Cookies = require('..') | |
| var fs = require('fs') | ||
| var http = require('http') | ||
| var https = require('https') | ||
| var Keygrip = require('keygrip') | ||
| var Keygrip = require('..').Keygrip | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We may need this change in other test files that require |
||
| var path = require('path') | ||
| var request = require('supertest') | ||
|
|
||
|
|
@@ -605,6 +605,216 @@ describe('Cookies(req, res, [options])', function () { | |
| }) | ||
| }) | ||
|
|
||
| describe('Keygrip', function () { | ||
| describe('constructor', function () { | ||
| it('should have correct constructor', function () { | ||
| var keys = new Keygrip(['key1']) | ||
| assert.strictEqual(keys.constructor, Keygrip) | ||
| }) | ||
|
|
||
| it('should work without new keyword', function () { | ||
| var keys = Keygrip(['key1']) | ||
| assert.strictEqual(keys.constructor, Keygrip) | ||
| }) | ||
|
|
||
| it('should throw without keys', function () { | ||
| assert.throws(function () { | ||
| new Keygrip() | ||
| }, /Keys must be provided/) | ||
| }) | ||
|
|
||
| it('should throw with empty array', function () { | ||
| assert.throws(function () { | ||
| new Keygrip([]) | ||
| }, /Keys must be provided/) | ||
| }) | ||
|
|
||
| it('should default to sha1 algorithm', function () { | ||
| var keys = new Keygrip(['key1']) | ||
| assert.strictEqual(keys.algorithm, 'sha1') | ||
| }) | ||
|
|
||
| it('should default to base64 encoding', function () { | ||
| var keys = new Keygrip(['key1']) | ||
| assert.strictEqual(keys.encoding, 'base64') | ||
| }) | ||
|
|
||
| it('should accept custom algorithm', function () { | ||
| var keys = new Keygrip(['key1'], 'sha256') | ||
| assert.strictEqual(keys.algorithm, 'sha256') | ||
| }) | ||
|
|
||
| it('should accept custom encoding', function () { | ||
| var keys = new Keygrip(['key1'], 'sha1', 'hex') | ||
| assert.strictEqual(keys.encoding, 'hex') | ||
| }) | ||
| }) | ||
|
|
||
| describe('.sign(data)', function () { | ||
| it('should sign data with first key', function () { | ||
| var keys = new Keygrip(['keyboard cat']) | ||
| var signature = keys.sign('foo=bar') | ||
| assert.strictEqual(signature, 'iW2fuCIzk9Cg_rqLT1CAqrtdWs8') | ||
| }) | ||
|
|
||
| it('should produce url-safe base64', function () { | ||
| var keys = new Keygrip(['test key']) | ||
| var signature = keys.sign('test data') | ||
| assert.ok(!/[\/\+=]/.test(signature), 'signature should not contain /, +, or =') | ||
| }) | ||
|
|
||
| it('should produce different signatures for different data', function () { | ||
| var keys = new Keygrip(['key1']) | ||
| var sig1 = keys.sign('data1') | ||
| var sig2 = keys.sign('data2') | ||
| assert.notStrictEqual(sig1, sig2) | ||
| }) | ||
|
|
||
| it('should produce different signatures for different keys', function () { | ||
| var keys1 = new Keygrip(['key1']) | ||
| var keys2 = new Keygrip(['key2']) | ||
| var sig1 = keys1.sign('data') | ||
| var sig2 = keys2.sign('data') | ||
| assert.notStrictEqual(sig1, sig2) | ||
| }) | ||
|
|
||
| it('should work with sha256 algorithm', function () { | ||
| var keys = new Keygrip(['key1'], 'sha256') | ||
| var signature = keys.sign('test') | ||
| assert.ok(signature.length > 0) | ||
| }) | ||
| }) | ||
|
|
||
| describe('.verify(data, digest)', function () { | ||
| it('should verify valid signature', function () { | ||
| var keys = new Keygrip(['key1']) | ||
| var signature = keys.sign('data') | ||
| assert.ok(keys.verify('data', signature)) | ||
| }) | ||
|
|
||
| it('should reject invalid signature', function () { | ||
| var keys = new Keygrip(['key1']) | ||
| assert.ok(!keys.verify('data', 'invalidsignature')) | ||
| }) | ||
|
|
||
| it('should reject signature from different key', function () { | ||
| var keys1 = new Keygrip(['key1']) | ||
| var keys2 = new Keygrip(['key2']) | ||
| var signature = keys1.sign('data') | ||
| assert.ok(!keys2.verify('data', signature)) | ||
| }) | ||
|
|
||
| it('should reject signature for different data', function () { | ||
| var keys = new Keygrip(['key1']) | ||
| var signature = keys.sign('data1') | ||
| assert.ok(!keys.verify('data2', signature)) | ||
| }) | ||
|
|
||
| it('should verify with any key in keylist', function () { | ||
| var keys = new Keygrip(['key1', 'key2', 'key3']) | ||
| var signature = keys.sign('data') | ||
| assert.ok(keys.verify('data', signature)) | ||
| }) | ||
| }) | ||
|
|
||
| describe('.index(data, digest)', function () { | ||
| it('should return 0 for signature from first key', function () { | ||
| var keys = new Keygrip(['key1', 'key2']) | ||
| var signature = keys.sign('data') | ||
| assert.strictEqual(keys.index('data', signature), 0) | ||
| }) | ||
|
|
||
| it('should return correct index for old key', function () { | ||
| var oldKeys = new Keygrip(['oldkey']) | ||
| var signature = oldKeys.sign('data') | ||
| var newKeys = new Keygrip(['newkey', 'oldkey']) | ||
| assert.strictEqual(newKeys.index('data', signature), 1) | ||
| }) | ||
|
|
||
| it('should return -1 for invalid signature', function () { | ||
| var keys = new Keygrip(['key1']) | ||
| assert.strictEqual(keys.index('data', 'invalidsignature'), -1) | ||
| }) | ||
|
|
||
| it('should return -1 for signature from unknown key', function () { | ||
| var keys1 = new Keygrip(['key1']) | ||
| var keys2 = new Keygrip(['key2']) | ||
| var signature = keys1.sign('data') | ||
| assert.strictEqual(keys2.index('data', signature), -1) | ||
| }) | ||
|
|
||
| it('should support key rotation', function () { | ||
| var keys = new Keygrip(['newest', 'older', 'oldest']) | ||
| var oldSignature = new Keygrip(['oldest']).sign('data') | ||
| assert.strictEqual(keys.index('data', oldSignature), 2) | ||
| }) | ||
| }) | ||
|
|
||
| describe('key rotation', function () { | ||
| it('should verify old signatures after key rotation', function () { | ||
| var oldKeys = new Keygrip(['key1']) | ||
| var signature = oldKeys.sign('session=abc123') | ||
|
|
||
| var newKeys = new Keygrip(['key2', 'key1']) | ||
| assert.ok(newKeys.verify('session=abc123', signature)) | ||
| assert.strictEqual(newKeys.index('session=abc123', signature), 1) | ||
| }) | ||
|
|
||
| it('should sign with newest key', function () { | ||
| var keys = new Keygrip(['newest', 'older', 'oldest']) | ||
| var signature = keys.sign('data') | ||
| assert.strictEqual(keys.index('data', signature), 0) | ||
| }) | ||
| }) | ||
|
|
||
| describe('edge cases', function () { | ||
| it('should handle empty string data', function () { | ||
| var keys = new Keygrip(['key1']) | ||
| var signature = keys.sign('') | ||
| assert.ok(keys.verify('', signature)) | ||
| }) | ||
|
|
||
| it('should handle special characters in data', function () { | ||
| var keys = new Keygrip(['key1']) | ||
| var data = 'foo=bar&baz=qux;path=/;secure' | ||
| var signature = keys.sign(data) | ||
| assert.ok(keys.verify(data, signature)) | ||
| }) | ||
|
|
||
| it('should handle unicode in data', function () { | ||
| var keys = new Keygrip(['key1']) | ||
| var data = 'hello=世界' | ||
| var signature = keys.sign(data) | ||
| assert.ok(keys.verify(data, signature)) | ||
| }) | ||
|
|
||
| it('should handle long keys', function () { | ||
| var keys = new Keygrip(['a'.repeat(1000)]) | ||
| var signature = keys.sign('data') | ||
| assert.ok(keys.verify('data', signature)) | ||
| }) | ||
|
|
||
| it('should handle many keys in rotation', function () { | ||
| var keyList = [] | ||
| for (var i = 0; i < 100; i++) { | ||
| keyList.push('key' + i) | ||
| } | ||
| var keys = new Keygrip(keyList) | ||
| var signature = keys.sign('data') | ||
| assert.strictEqual(keys.index('data', signature), 0) | ||
| }) | ||
|
|
||
| it('should reject signatures with different lengths', function () { | ||
| var keys = new Keygrip(['key1']) | ||
| var signature = keys.sign('data') | ||
| var shortSignature = signature.slice(0, 10) | ||
| var longSignature = signature + 'extra' | ||
| assert.ok(!keys.verify('data', shortSignature)) | ||
| assert.ok(!keys.verify('data', longSignature)) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| function assertServer (done, test) { | ||
| var server = http.createServer(function (req, res) { | ||
| try { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be better to check whether
crypto.timingSafeEqual(...)is existing and call it here rather than inbufferEqual?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The update, as is, preserves expected node compatibility; adapted from keygrip.
Happy to update along with a node compatibility update if desired!