Skip to content

Commit

Permalink
policy: fix integrity when DEFAULT_ENCODING is set
Browse files Browse the repository at this point in the history
PR-URL: #39750
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
tniessen authored and targos committed Sep 4, 2021
1 parent e660892 commit 0ff520c
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/internal/policy/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.js text eol=lf
3 changes: 3 additions & 0 deletions test/fixtures/policy/crypto-default-encoding/dep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

// No code.
4 changes: 4 additions & 0 deletions test/fixtures/policy/crypto-default-encoding/parent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict';

require('crypto').DEFAULT_ENCODING = process.env.DEFAULT_ENCODING;
require('./dep.js');
14 changes: 14 additions & 0 deletions test/fixtures/policy/crypto-default-encoding/policy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"resources": {
"./parent.js": {
"integrity": "sha384-j4pMdq83q5Bq9+idcHuGKzi89FrYm1PhZYrEw3irbNob6g4i3vKBjfYiRNYwmoGr",
"dependencies": {
"crypto": true,
"./dep.js": true
}
},
"./dep.js": {
"integrity": "sha384-VU7GIrTix/HFLhUb4yqsV4n1xXqjPcWw6kLvjuKXtR1+9nmufJu5vZLajBs8brIW"
}
}
}
34 changes: 34 additions & 0 deletions test/parallel/test-policy-crypto-default-encoding.js
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 0ff520c

Please sign in to comment.