Skip to content

Commit 7ff2139

Browse files
tniessendanielleadams
authored andcommitted
policy: fix integrity when DEFAULT_ENCODING is set
PR-URL: #39750 Reviewed-By: Bradley Farias <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent a5ded4a commit 7ff2139

File tree

6 files changed

+60
-2
lines changed

6 files changed

+60
-2
lines changed

lib/internal/policy/manifest.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,8 +501,10 @@ class Manifest {
501501
value: expected
502502
} = integrityEntries[i];
503503
const hash = createHash(algorithm);
504-
HashUpdate(hash, content);
505-
const digest = HashDigest(hash);
504+
// TODO(tniessen): the content should not be passed as a string in the
505+
// first place, see https://github.com/nodejs/node/issues/39707
506+
HashUpdate(hash, content, 'utf8');
507+
const digest = HashDigest(hash, 'buffer');
506508
if (digest.length === expected.length &&
507509
timingSafeEqual(digest, expected)) {
508510
return true;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.js text eol=lf
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
// No code.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'use strict';
2+
3+
require('crypto').DEFAULT_ENCODING = process.env.DEFAULT_ENCODING;
4+
require('./dep.js');
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"resources": {
3+
"./parent.js": {
4+
"integrity": "sha384-j4pMdq83q5Bq9+idcHuGKzi89FrYm1PhZYrEw3irbNob6g4i3vKBjfYiRNYwmoGr",
5+
"dependencies": {
6+
"crypto": true,
7+
"./dep.js": true
8+
}
9+
},
10+
"./dep.js": {
11+
"integrity": "sha384-VU7GIrTix/HFLhUb4yqsV4n1xXqjPcWw6kLvjuKXtR1+9nmufJu5vZLajBs8brIW"
12+
}
13+
}
14+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
if (!common.hasCrypto)
5+
common.skip('missing crypto');
6+
common.requireNoPackageJSONAbove();
7+
8+
const fixtures = require('../common/fixtures');
9+
10+
const assert = require('assert');
11+
const { spawnSync } = require('child_process');
12+
13+
const encodings = ['buffer', 'utf8', 'utf16le', 'latin1', 'base64', 'hex'];
14+
15+
for (const encoding of encodings) {
16+
const dep = fixtures.path('policy', 'crypto-default-encoding', 'parent.js');
17+
const depPolicy = fixtures.path(
18+
'policy',
19+
'crypto-default-encoding',
20+
'policy.json');
21+
const { status } = spawnSync(
22+
process.execPath,
23+
[
24+
'--experimental-policy', depPolicy, dep,
25+
],
26+
{
27+
env: {
28+
...process.env,
29+
DEFAULT_ENCODING: encoding
30+
}
31+
}
32+
);
33+
assert.strictEqual(status, 0);
34+
}

0 commit comments

Comments
 (0)