-
Notifications
You must be signed in to change notification settings - Fork 29.9k
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
src,crypto: fix 0-length output crash in webcrypto #38913
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -249,16 +249,17 @@ class CipherJob final : public CryptoJob<CipherTraits> { | |
v8::Local<v8::Value>* result) override { | ||
Environment* env = AsyncWrap::env(); | ||
CryptoErrorStore* errors = CryptoJob<CipherTraits>::errors(); | ||
if (out_.size() > 0) { | ||
|
||
if (errors->Empty()) | ||
errors->Capture(); | ||
|
||
if (out_.size() > 0 || errors->Empty()) { | ||
CHECK(errors->Empty()); | ||
*err = v8::Undefined(env->isolate()); | ||
*result = out_.ToArrayBuffer(env); | ||
return v8::Just(!result->IsEmpty()); | ||
} | ||
|
||
if (errors->Empty()) | ||
errors->Capture(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. /ping @jasnell, why here should |
||
CHECK(!errors->Empty()); | ||
*result = v8::Undefined(env->isolate()); | ||
return v8::Just(errors->ToException(env).ToLocal(err)); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
|
||
if (!common.hasCrypto) | ||
common.skip('missing crypto'); | ||
|
||
const assert = require('assert'); | ||
const crypto = require('crypto').webcrypto; | ||
|
||
(async () => { | ||
const k = await crypto.subtle.importKey( | ||
'raw', | ||
new Uint8Array(32), | ||
{ name: 'AES-GCM' }, | ||
false, | ||
[ 'encrypt', 'decrypt' ]); | ||
assert(k instanceof crypto.CryptoKey); | ||
|
||
const e = await crypto.subtle.encrypt({ | ||
name: 'AES-GCM', | ||
iv: new Uint8Array(12), | ||
}, k, new Uint8Array(0)); | ||
assert(e instanceof ArrayBuffer); | ||
assert.deepStrictEqual( | ||
Buffer.from(e), | ||
Buffer.from([ | ||
0x53, 0x0f, 0x8a, 0xfb, 0xc7, 0x45, 0x36, 0xb9, | ||
0xa9, 0x63, 0xb4, 0xf1, 0xc4, 0xcb, 0x73, 0x8b ])); | ||
|
||
const v = await crypto.subtle.decrypt({ | ||
name: 'AES-GCM', | ||
iv: new Uint8Array(12), | ||
}, k, e); | ||
assert(v instanceof ArrayBuffer); | ||
assert.strictEqual(v.byteLength, 0); | ||
})().then(common.mustCall()).catch((e) => { | ||
assert.ifError(e); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few lines below, there's this:
So, either those lines are not needed, or there is no guarantee that
errors->IsEmpty()
will returnfalse
only if no error occurred.If
errors->IsEmpty()
is true if and only if the operation succeeded, thenout_.size() > 0
is not a required condition.If
errors->IsEmpty()
is nevertrue
when the operation failed, then the lines below should be removed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I move
if (errors->Empty()) errors->Capture();
beforeif (out_.size() > 0)
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think @jasnell might be the best person to ask, but that might be enough to solve the problem. It would make the sanity check
CHECK(!errors->Empty())
below useless.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've moved it above.
/cc @jasnell