Fix uncaught TypeError in "Show on map" operation.#2453
Conversation
|
@GCHQDeveloper581 I noticed the failed PR action. On my system all unit tests succeed. I did just merge the latest master into my branch. |
|
@lzandman - yeah - the test failure was caused by some hard-coded PGP keys, used as test fixtures, expiring and causing those tests to fail - nothing to do with your code. This has now been fixed on master so the merge from there should be all that's required. If you were feeling dead keen you'd add a "sunny day" functional test to tests/browser/02_ops.js . A single test, just to prove that the operation is working is all that's required - detailed testing should be carried out in tests/operations/tests/ShowOnMap.mjs (which you've already addressed). |
GCHQDeveloper581
left a comment
There was a problem hiding this comment.
Looks good!
Thank you for your contribution.
Description
Adding the Show on map operation with an input that resolves to only a single
coordinate value crashes the browser with an uncaught error:
This PR fixes #2420.
How to reproduce
Navigate to:
https://gchq.github.io/CyberChef/#recipe=Show_on_map(13,'Auto','%5C%5Cn')&input=MSwgMjQ
(input
1, 24, add "Show on Map" operation with the Input Delimiter set to\n)Root cause
The input
1, 24is comma-separated, but the recipe pins the Input Delimiterto
\n(newline). That mismatch derails the auto format-detection insrc/core/lib/ConvertCoordinates.mjs:findFormat()is called with the newline delimiter. Since1, 24contains nonewline, the whole string is treated as one coordinate. Splitting it on
whitespace yields two numbers (
1and24), so it is detected asDegrees Decimal Minutes.
\n, gets a single element, setsisPair = false, and interprets1, 24as a single DDM value:1° 24'=1.4°."1.4", not alatitude/longitude pair.
ShowOnMap.present()then emittedL.map(...).setView([1.4], 13)— a one-elementarray — and Leaflet threw
TypeError: Cannot read properties of null (reading 'lat')because the longitude was
undefined.The solution
ShowOnMap.run()now validates that the conversion produced a properlatitude/longitude pair. If it didn't, it throws a friendly
OperationErrorinstead of letting a single value flow through to the map:
Validation lives in
run()rather thanpresent()becausepresent()executesoutside the recipe's error handling, so an error thrown there would crash the bake
instead of surfacing as a message.
This converts an uncaught browser
TypeErrorinto a clear, actionable errormessage telling the user to check the input format and delimiter.
Testing
Added operation tests in
tests/operations/tests/ShowOnMap.mjs:51.5007, -0.1246) still renders the map.1, 24with\ndelimiter) now produces the helpful errormessage instead of crashing.
Also verified in Chrome:
1, 24with\ndelimiter): no more uncaughtTypeError; theoutput now shows "Could not show coordinates '1.4' on the map. Expected a
latitude and longitude pair - check that the input format and delimiter are
correct."
51.5007, -0.1246): map renders, tiles load, and the popup shows51.5007,-0.1246— behaviour unchanged.npx eslintpasses on the changed files.Files changed
src/core/operations/ShowOnMap.mjs— added latitude/longitude pair validation inrun().tests/operations/tests/ShowOnMap.mjs— new test file (regression + happy path).tests/operations/index.mjs— register the new test file.AI Disclosure
Claude Code used to assist in analyzing the bug and creating a fix.