Skip to content

fix: make the endpoint slug codec injective (#123) - #136

Merged
jrosskopf merged 2 commits into
mainfrom
fix/gh-123-slug-codec
Sep 22, 2026
Merged

jrosskopf merged 2 commits into
mainfrom
fix/gh-123-slug-codec

Conversation

@jrosskopf

@jrosskopf jrosskopf commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Closes #123.

The defect

An endpoint is addressed as /api/v1/_config/endpoints/{slug}. The codec producing that slug was not reversible, so the address did not identify the endpoint:

"/a-b" -> "a-b" -> "/a/b"
"/a/b" -> "a-b" -> "/a/b"

Both shared one config-service URL, and a GET → edit → PUT round trip silently rewrote /order-items to /order/items. Hyphens in REST paths are entirely ordinary, so this needed no unusual config. / and "" both encoded to "empty" as well.

The new encoding

Chosen to keep the common case readable:

'/' → '-' /sap/functions → -sap-functions
'-' → %2D a literal hyphen is escaped…
'%' → %25 …which is what makes a - in a slug unambiguously a slash
"" → empty unambiguous: every non-empty path starts with /, so it encodes to a leading -

Verified exhaustively over every path up to length 5 drawn from /-%2Dab — 19,608 paths, zero collisions, zero round-trip failures.

Two earlier attempts are recorded in the source comment, because both failed on the same point: a one-character escape for / cannot coexist with a two-character escape for the literal, since the shorter is a prefix of the longer. //x and _x both produced __x. I only caught it by fuzzing rather than by inspection.

Breaking

Slugs change: …/endpoints/customers-slash → …/endpoints/-customers-. Anything holding a slug URL must re-resolve it. This is the clean-break option you chose, so there is no legacy form to carry.

On the changed tests

Twelve assertions changed. They pinned "customers-slash" and pathToSlug("/") == "empty" — that is, the defect, including the root/empty collision itself. What they test is unchanged: round trips, lookup by slug, uniqueness. A note at the top of the file explains why they moved, since the standing rule is to keep the test surface stable while the tests are correct.

Four new cases cover what the old codec could not: a hyphen is not a slash, root and empty are distinct, everything round-trips including /a%b and /a--b, and distinct paths get distinct slugs.

751 unit, 362 standalone integration, all green.

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.

An endpoint is addressed in the config service as
/api/v1/_config/endpoints/{slug}. The codec that produced that slug was not
reversible, so the address did not identify the endpoint.

pathToSlug mapped every internal '/' to '-', every non-alphanumeric to '-',
collapsed runs of '-' and stripped the edges. slugToPath then mapped every
'-' back to '/'. So:

  "/a-b" -> "a-b" -> "/a/b"
  "/a/b" -> "a-b" -> "/a/b"

Both endpoints shared one config-service URL, and a GET -> edit -> PUT round
trip silently REWROTE "/order-items" to "/order/items". Hyphens in REST
paths are entirely ordinary, so this was reachable with a plain config.
"/" and "" both encoded to "empty" as well.

New encoding, chosen to keep the common case readable:

  '/'  -> '-'        /sap/functions  ->  -sap-functions
  '-'  -> "%2D"      a literal hyphen is escaped, which is what makes a '-'
  '%'  -> "%25"      in a slug unambiguously mean '/'
  ""   -> "empty"    unambiguous: every non-empty path starts with '/' and
                     therefore encodes to a leading '-'

Verified exhaustively over every path up to length 5 drawn from "/-%2Dab":
19,608 paths, zero collisions, zero round-trip failures.

Two earlier attempts are recorded in the source comment because both failed
on the same point and the next person should not repeat them: a
one-character escape for '/' cannot coexist with a two-character escape for
the literal, because the shorter is a prefix of the longer. "//x" and "_x"
both produced "__x".

BREAKING: slugs change. /api/v1/_config/endpoints/customers-slash is now
/api/v1/_config/endpoints/-customers-. Anything holding a slug URL must
re-resolve it. This is the clean-break option rather than accepting both
forms, so there is no legacy path to carry.

Twelve assertions in config_service_slug_test.cpp changed with the
encoding. They were pinning the defect - "customers-slash", and
pathToSlug("/") == "empty", which is the root/empty collision - not the
contract. What they test is unchanged: round trips, lookup by slug,
uniqueness across endpoints. A note at the top of the file says so.

Four new test cases cover what the old codec could not do: a hyphen is not
a slash, root and empty are distinct, everything round-trips including
"/a%b" and "/a--b", and distinct paths get distinct slugs.

751 unit tests, 362 standalone integration tests.
The integration suite failed on the slug branch in a way the unit tests
could not see, because both failures are in callers that address endpoints
by slug or path over HTTP.

1. slugToPath must tolerate a path-style identifier.

   The config service also accepts a percent-encoded url-path in the {slug}
   position - the tavern suite addresses an endpoint as
   "northwind%2Fproducts%2F" - which arrives url-decoded as
   "northwind/products/" and contains nothing this codec encodes.

   The old decoder prepended the leading slash unconditionally, because it
   stripped one when encoding. The new one encodes the leading '/' as '-',
   so a slug from pathToSlug already decodes with it - and the
   path-style identifier lost its leading slash and matched no endpoint.

   The slash is now added only when absent. That cannot collide: every slug
   pathToSlug produces for a non-empty path starts with '-' and therefore
   already decodes to a leading '/'.

2. Four hardcoded slugs in the DuckLake tests.

   test-cached-slash -> -test-cached-, and the same for append, merge and
   invalid. Verified against the encoder rather than hand-derived.

Worth recording: the unit suite was green through both of these. The slug
codec has two classes of caller - one that round-trips through it, which
the unit tests cover, and one that hardcodes the output, which only the
integration suite exercises. A green unit run said nothing about the
second.

751 unit tests, 362 standalone integration tests; the tavern suite that
caught this runs on CI.
@jrosskopf
jrosskopf merged commit f04ceec into main Sep 22, 2026
22 checks passed
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.

pathToSlug is not injective: an endpoint whose URL contains a hyphen is addressed as a different endpoint

1 participant