Skip to content

Fix uncaught TypeError in "Show on map" operation.#2453

Merged
GCHQDeveloper581 merged 8 commits into
gchq:masterfrom
lzandman:leon/fix-showonmap-typeerror
Jun 20, 2026
Merged

Fix uncaught TypeError in "Show on map" operation.#2453
GCHQDeveloper581 merged 8 commits into
gchq:masterfrom
lzandman:leon/fix-showonmap-typeerror

Conversation

@lzandman

@lzandman lzandman commented May 25, 2026

Copy link
Copy Markdown
Contributor

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:

Uncaught TypeError: Cannot read properties of null (reading 'lat')

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, 24 is comma-separated, but the recipe pins the Input Delimiter
to \n (newline). That mismatch derails the auto format-detection in
src/core/lib/ConvertCoordinates.mjs:

  1. findFormat() is called with the newline delimiter. Since 1, 24 contains no
    newline, the whole string is treated as one coordinate. Splitting it on
    whitespace yields two numbers (1 and 24), so it is detected as
    Degrees Decimal Minutes.
  2. The main conversion then splits on \n, gets a single element, sets
    isPair = false, and interprets 1, 24 as a single DDM value:
    1° 24' = 1.4°.
  3. The conversion output is therefore the single value "1.4", not a
    latitude/longitude pair.

ShowOnMap.present() then emitted L.map(...).setView([1.4], 13) — a one-element
array — 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 proper
latitude/longitude pair. If it didn't, it throws a friendly OperationError
instead of letting a single value flow through to the map:

const coords = latLong.split(",").map(v => v.trim());
if (coords.length !== 2 || coords.some(v => v === "" || isNaN(Number(v)))) {
    throw new OperationError(`Could not show coordinates '${latLong}' on the map. Expected a latitude and longitude pair - check that the input format and delimiter are correct.`);
}

Validation lives in run() rather than present() because present() executes
outside 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 TypeError into a clear, actionable error
message telling the user to check the input format and delimiter.

Testing

Added operation tests in tests/operations/tests/ShowOnMap.mjs:

  • A valid coordinate pair (51.5007, -0.1246) still renders the map.
  • The regression case (1, 24 with \n delimiter) now produces the helpful error
    message instead of crashing.

Also verified in Chrome:

  • Bad case (1, 24 with \n delimiter): no more uncaught TypeError; the
    output 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."
  • Good case (51.5007, -0.1246): map renders, tiles load, and the popup shows
    51.5007,-0.1246 — behaviour unchanged.
  • npx eslint passes on the changed files.

Files changed

  • src/core/operations/ShowOnMap.mjs — added latitude/longitude pair validation in
    run().
  • 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.

@lzandman

Copy link
Copy Markdown
Contributor Author

@GCHQDeveloper581 I noticed the failed PR action. On my system all unit tests succeed. I did just merge the latest master into my branch.

@GCHQDeveloper581

Copy link
Copy Markdown
Contributor

@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 GCHQDeveloper581 enabled auto-merge (squash) June 20, 2026 10:59

@GCHQDeveloper581 GCHQDeveloper581 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

Thank you for your contribution.

@GCHQDeveloper581 GCHQDeveloper581 merged commit a0a369a into gchq:master Jun 20, 2026
2 checks passed
@lzandman lzandman deleted the leon/fix-showonmap-typeerror branch June 23, 2026 21:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(Show On Map, Input Delimiter): TypeError can't access property lat, t null

2 participants