-
Notifications
You must be signed in to change notification settings - Fork 3
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
Handling panics in encrypt/decrypt #78
Open
olegbespalov
wants to merge
2
commits into
main
Choose a base branch
from
chore/handling-panics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,40 @@ | ||
// this example demonstrates how to handle panic in the decrypt function | ||
// we intentionally cut transmittedData from 12 to the end, so it will panic | ||
// the panic will be caught | ||
import { crypto } from "k6/experimental/webcrypto"; | ||
|
||
export default async function () { | ||
const keyData = new Uint8Array([ | ||
7, 152, 164, 45, 255, 169, 164, 66, 164, 163, 20, 197, 194, 223, 48, 213, | ||
93, 115, 173, 86, 215, 81, 128, 188, 45, 237, 156, 92, 163, 197, 248, 114, | ||
]); | ||
|
||
const transmittedData = new Uint8Array([ | ||
167, 9, 89, 202, 97, 13, 137, 77, 223, 24, 226, 161, 225, 228, 121, 248, | ||
181, 4, 25, 202, 215, 230, 193, 94, 143, 77, 187, 231, 84, 3, 198, 75, 22, | ||
211, 83, 101, 241, 159, 117, 124, 155, 229, 244, 173, 58, 149, 57, 18, | ||
]); | ||
|
||
const iv = new Uint8Array(transmittedData.slice(0, 16)); | ||
|
||
// we intentionally cut incorrectly transmittedData from 12 to the end | ||
const encryptedData = new Uint8Array(transmittedData.slice(12)); | ||
|
||
const importedKey = await crypto.subtle.importKey( | ||
"raw", | ||
keyData, | ||
{ name: "AES-CBC", length: "256" }, | ||
true, | ||
["encrypt", "decrypt"] | ||
); | ||
|
||
// if we pass such transmittedData to decrypt function, it will panic | ||
await crypto.subtle.decrypt( | ||
{ | ||
name: "AES-CBC", | ||
iv: iv, | ||
}, | ||
importedKey, | ||
encryptedData | ||
); | ||
} |
Oops, something went wrong.
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.
Given that the issue in the concrete case is about the input not being what is expected, I do wonder if it is not better to check the input instead of using
recover
.I guess we can still have this code, but I am not a fan of "exceptional workflow".
AFAIK the reason this is a panic in the
crypto
module is because there is no reason for someone to provide it incorrect data.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.
In general, I totally agree with you, and we should catch this on input/validation. My issue is that if we don't catch such input rule, e.g. that algorithm was part of the web API test suite and still, k6 ended up with hard interruption which isn't great :sad: