Skip to content

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

Open
lzandman wants to merge 3 commits into
gchq:masterfrom
lzandman:leon/fix-showonmap-typeerror
Open

Fix uncaught TypeError in "Show on map" operation.#2453
lzandman wants to merge 3 commits into
gchq:masterfrom
lzandman:leon/fix-showonmap-typeerror

Conversation

@lzandman
Copy link
Copy Markdown
Contributor

@lzandman lzandman commented May 25, 2026

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.

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

1 participant