Skip to content
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

policy: fix integrity when DEFAULT_ENCODING is set #39750

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
tniessen marked this conversation as resolved.
Show resolved Hide resolved
"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);
}