Fix uncaught TypeError in To BCD with empty encoding scheme#2584
Open
itxaiohanglover wants to merge 1 commit into
Open
Fix uncaught TypeError in To BCD with empty encoding scheme#2584itxaiohanglover wants to merge 1 commit into
itxaiohanglover wants to merge 1 commit into
Conversation
62b326e to
6fdf102
Compare
Validate the encoding scheme argument in To BCD and From BCD before it is used. When the scheme is empty or unrecognised (e.g. loaded from a recipe URL), ENCODING_LOOKUP returns undefined, and accessing properties on it threw an uncaught TypeError. Throw a friendly OperationError instead. Adds regression tests covering both operations with an empty scheme.
6fdf102 to
c657852
Compare
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
The To BCD operation throws an uncaught
TypeErrorwhen its Schemeargument is empty or unrecognised. This can be triggered via a recipe URL where
the first argument is an empty string, e.g.:
https://gchq.github.io/CyberChef/#recipe=To_BCD('',true,false,'Nibbles')&input=NDM
Root cause
In
src/core/operations/ToBCD.mjs, the encoding lookup is performed with:args[0]is the Scheme option. When it is an empty string (or any value notpresent in
ENCODING_LOOKUP),encodingisundefined. The code then doesencoding[n]while mapping over the input digits, which throws the uncaughtTypeErrorbecause a property is being accessed onundefined.From BCD(src/core/operations/FromBCD.mjs) shares the same pattern: it callsencoding.indexOf(n), which would throwTypeError: encoding.indexOf is not a functionfor the same reason.The solution
Both operations now validate the encoding scheme immediately after the lookup and
throw a friendly
OperationErrorinstead of letting an uncaughtTypeErrorcrash the bake:
This mirrors the existing validation style already used in
To BCD(e.g. theinput.isNaN()and fractional-value guards) and the approach taken by otherrecent fixes that convert uncaught
TypeErrors into actionable errors. As withthose existing
OperationErrors, the message is surfaced to the user as theoperation output rather than crashing the bake.
Existing Issue
Fixes #2488
Screenshots
No visual/UI changes — this only replaces a crash with a handled error message.
AI disclosure
This code was written with the assistance of an AI coding assistant (Qoder). The
logic, fix, and tests have been reviewed and verified against the existing
codebase conventions and the
Recipe.executeerror-handling flow.Test Coverage
Added regression tests to
tests/operations/tests/BCD.mjscovering bothoperations with an empty scheme:
To BCD: invalid (empty) encoding scheme— input43, expects outputInvalid encoding scheme.From BCD: invalid (empty) encoding scheme— input0100 0011, expectsoutput
Invalid encoding scheme.Because an
OperationErroris caught byRecipe.executeand its message isreturned as the operation output, these tests assert the message directly
(
expectedOutput: "Invalid encoding scheme"). Without the fix, both casesproduce an uncaught
TypeErrorinstead of this message, so the tests failwithout the fix and pass with it.