policy: fix integrity when DEFAULT_ENCODING is set

PR-URL: https://github.com/nodejs/node/pull/39750
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Tobias Nießen authored and Michaël Zasso committed 2021-09-04 15:14:53 +02:00
1 parent e660892f1a
commit 0ff520cf02
6 files changed
+60 -2

No files matched your search

+4 -2
View File
@@ -501,8 +501,10 @@ class Manifest {
value: expected
} = integrityEntries[i];
const hash = createHash(algorithm);
HashUpdate(hash, content);
const digest = HashDigest(hash);
// TODO(tniessen): the content should not be passed as a string in the
// first place, see https://github.com/nodejs/node/issues/39707
HashUpdate(hash, content, 'utf8');
const digest = HashDigest(hash, 'buffer');
if (digest.length === expected.length &&
timingSafeEqual(digest, expected)) {
return true;
@@ -0,0 +1 @@
*.js text eol=lf
+3
View File
@@ -0,0 +1,3 @@
'use strict';
// No code.
@@ -0,0 +1,4 @@
'use strict';
require('crypto').DEFAULT_ENCODING = process.env.DEFAULT_ENCODING;
require('./dep.js');
@@ -0,0 +1,14 @@
{
"resources": {
"./parent.js": {
"integrity": "sha384-j4pMdq83q5Bq9+idcHuGKzi89FrYm1PhZYrEw3irbNob6g4i3vKBjfYiRNYwmoGr",
"dependencies": {
"crypto": true,
"./dep.js": true
}
},
"./dep.js": {
"integrity": "sha384-VU7GIrTix/HFLhUb4yqsV4n1xXqjPcWw6kLvjuKXtR1+9nmufJu5vZLajBs8brIW"
}
}
}
@@ -0,0 +1,34 @@
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
common.requireNoPackageJSONAbove();
const fixtures = require('../common/fixtures');
const assert = require('assert');
const { spawnSync } = require('child_process');
const encodings = ['buffer', 'utf8', 'utf16le', 'latin1', 'base64', 'hex'];
for (const encoding of encodings) {
const dep = fixtures.path('policy', 'crypto-default-encoding', 'parent.js');
const depPolicy = fixtures.path(
'policy',
'crypto-default-encoding',
'policy.json');
const { status } = spawnSync(
process.execPath,
[
'--experimental-policy', depPolicy, dep,
],
{
env: {
...process.env,
DEFAULT_ENCODING: encoding
}
}
);
assert.strictEqual(status, 0);
}