Skip to content

Commit 56a345d

Browse files
committed
✨ add script to update known hashes
1 parent 175ab21 commit 56a345d

File tree

5 files changed

+40
-16
lines changed

5 files changed

+40
-16
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@
3434
},
3535
"scripts": {
3636
"lint": "eslint --plugin markdown --ext js,md .",
37-
"test": "mocha --reporter spec --bail --check-leaks test/test.js",
37+
"test": "mocha --reporter spec --check-leaks test/test.js",
3838
"test-ci": "nyc --reporter=lcov --reporter=text npm test",
3939
"test-cov": "nyc --reporter=html --reporter=text npm test",
40+
"update-upstream-hashes": "node scripts/update-upstream-hashes.js",
4041
"upstream": "mocha --reporter spec --check-leaks test/upstream.js",
4142
"version": "node scripts/version-history.js && git add HISTORY.md"
4243
}

scripts/update-upstream-hashes.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const fs = require('fs')
2+
const path = require('path')
3+
const { getFunctionHash, httpServerResponsePrototype: res } = require('../scripts/upstream-common')
4+
5+
const updatedHashes = {
6+
knownAppendHeaderHash: getFunctionHash(res.appendHeader),
7+
knownRemoveHeaderHash: getFunctionHash(res.removeHeader),
8+
knownSetHeaderHash: getFunctionHash(res.setHeader),
9+
knownWriteHeadHash: getFunctionHash(res.writeHead)
10+
}
11+
12+
const filename = 'known-upstream-hashes.json'
13+
14+
const filePath = path.join(__dirname, `../test/${filename}`)
15+
fs.writeFileSync(filePath, JSON.stringify(updatedHashes, null, 2) + '\n', 'utf8')
16+
17+
console.log(`✅ updated '${filename}' with current method hashes.`)

scripts/upstream-common.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const crypto = require('crypto')
2+
const http = require('http')
3+
4+
function getFunctionHash (fn) {
5+
const src = fn.toString().replace(/\s+/g, '') // normalize whitespace
6+
return crypto.createHash('sha256').update(src).digest('hex')
7+
}
8+
9+
module.exports = {
10+
getFunctionHash,
11+
httpServerResponsePrototype: http.ServerResponse.prototype
12+
}

test/known-upstream-hashes.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"knownAppendHeaderHash": "0deb9f70c3bba63993321cca9281fb4607e2567bed1436b8574c5b86698125a8",
3+
"knownRemoveHeaderHash": "3ad5ccb0a858beb6268f281492bd8d42c9815f5316cc3c4f7f735e142fcd29d9",
4+
"knownSetHeaderHash": "2d4f95e92586d28bfd4d3137a8eaacb82b255967d8c26413015c6b56daf0afe7",
5+
"knownWriteHeadHash": "281e0d02084a69893b8c3b8692e3c7c4de2ce22a626217fcf597fa6ddf6955a9"
6+
}

test/upstream.js

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
11
// NOTE: this is a temporary solution to tell us if future changes to the monkey-patched methods
22
// could impact this package. Recognizing this is not an ideal solution, we plan to address this when
33
// we can drop the monkey-patching entirely.
4-
const crypto = require('crypto')
54
const assert = require('assert')
6-
const http = require('http')
7-
const Socket = require('net').Socket
5+
const knownHashes = require('./known-upstream-hashes.json')
6+
const { getFunctionHash, httpServerResponsePrototype: res } = require('../scripts/upstream-common')
87

9-
const req = new http.IncomingMessage(new Socket())
10-
const res = new http.ServerResponse(req)
11-
12-
function getFunctionHash (fn) {
13-
const src = fn.toString().replace(/\s+/g, '') // normalize whitespace
14-
return crypto.createHash('sha256').update(src).digest('hex')
15-
}
16-
17-
const knownWriteHeadHash = '281e0d02084a69893b8c3b8692e3c7c4de2ce22a626217fcf597fa6ddf6955a9'
18-
const knownSetHeaderHash = '2d4f95e92586d28bfd4d3137a8eaacb82b255967d8c26413015c6b56daf0afe7'
19-
const knownAppendHeaderHash = '0deb9f70c3bba63993321cca9281fb4607e2567bed1436b8574c5b86698125a8'
20-
const knownRemoveHeaderHash = '3ad5ccb0a858beb6268f281492bd8d42c9815f5316cc3c4f7f735e142fcd29d9'
8+
const { knownAppendHeaderHash, knownRemoveHeaderHash, knownSetHeaderHash, knownWriteHeadHash } = knownHashes
219

2210
describe('function verification', function () {
2311
it('should match the known function hash of writeHead', function () {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy