Skip to content

Records of a subject whose id contains a slash, hash, question mark or percent sign cannot be opened #1718

Open

Description

@joshunrau

A subject whose id contains /, #, ? or % cannot have any of its records opened, and its "View Current Subject" link goes nowhere. Two places paste the raw id into a URL path. Such ids are legal: a custom identifier only has to avoid $ (and match the group's optional regex), the upload's SUBJECT_ID_REGEX is /^[^$\s]+$/, and the group-name prefix only has spaces replaced. So 2024/001, P#12, or a group named Psychosis/PEPP all produce such ids.

  • subjectQueryOptions requests /v1/subjects/${params.id}. For PEPP$2024/001 that is /v1/subjects/PEPP$2024/001, which matches no API route (404). # truncates the path at the fragment, and % makes Express fail to decode the parameter. The record detail page's loader awaits this query, so every record of that subject opens to the error page.
  • useNavItems builds the session link as `/datahub/subjects/${currentSession?.subjectId}/table` and hands it to navigate({ to }), so the router resolves the wrong path.

useSubjectDemographicsLookup already gets this right with encodeURIComponent(params.id).

Where

apps/web/src/hooks/useSubjectQuery.ts:12:

const response = await axios.get(`/v1/subjects/${params.id}`, { params });

apps/web/src/hooks/useNavItems.ts:282:

url: `/datahub/subjects/${currentSession?.subjectId}/table`

(The subject lookup in apps/web/src/routes/_app/datahub/subjects/index.tsx:317 has the same unencoded `/v1/subjects/${id}`.)

Reproduce

  1. Start a session with the custom identifier 2024/001 and complete any instrument.
  2. In the sidebar, click View Current Subject.
  3. Go to Data Hub → Subjects, open the subject, and click its record in the table.

Actual: step 2 lands on the not-found page. Step 3 requests GET /v1/subjects/<Group>$2024/001, gets a 404, and shows the error page.
Expected: both open the subject and the record.

Tests

  • apps/web/src/hooks/__tests__/useSubjectQuery.test.ts: it('should encode the subject id in the request path, so an id containing a slash or hash reaches the subject endpoint').
  • apps/web/src/hooks/__tests__/useNavItems.test.ts: it('should link to the current subject through its encoded id').
  • testing/src/specs/subject-detail.spec.ts: start a session with a custom id containing /, complete an instrument, and open the record from the subject table.

Suggested fix

Use encodeURIComponent(params.id) in subjectQueryOptions, and drop the stray { params }, which also sends the id as ?id=. In useNavItems, navigate with route params (to: '/datahub/subjects/$subjectId/table', params: { subjectId }) rather than an interpolated string, which means carrying params on NavItem. Otherwise encode the segment.

Activity

  1. added
    Bug
    Priority: Medium
    Difficulty: Low
    Area: Web
    Good First Issue
    on Oct 7, 2026
  2. joshunrau commented on Oct 7, 2026

    @joshunrau
    CollaboratorAuthor

    Two more call sites break the same way. In the Data Hub subjects table, the row click handlers interpolate the raw id into a relative path:

    apps/web/src/routes/_app/datahub/subjects/index.tsx:675 and :678:

    void navigate({ to: `./${subject.id}/table` });

    The fix should cover these as well: navigate with typed route params (to: '/datahub/subjects/$subjectId/table', params: { subjectId: subject.id }).

  3. joshunrau commented on Oct 26, 2026

    @joshunrau
    CollaboratorAuthor

    Still reproduces on main (d86740e). None of the files involved has changed since the issue was filed.

    Two more call sites

    • apps/web/src/routes/_app/datahub/subjects/index.tsx:325: when the lookup at :317 succeeds, it navigates to `./${response.data.id}/table`, so encoding the request alone is not enough.
    • apps/web/src/routes/_app/datahub/subjects/$subjectId/route.tsx:18: the Table, Graph and Assignments tabs are built from the decoded params.subjectId, so for 2024/001 they point at a path no route matches.

    The router also reads a raw id as template syntax. router-core parses a segment that starts with $ as a param, so in a group named $Lab (a legal name, see #1725) the row click at :675 goes to /datahub/subjects/undefined/table.

    Tab highlighting is already wrong

    A typed param is encoded with encodeURIComponent, and router-core only applies decodeURI to location.pathname (decodePath), so %24 and %2F stay encoded. Custom-ID subject ids always contain $, and their record page is opened through the typed ./$recordId navigate ($subjectId/table/index.tsx:96, :101). Its URL holds %24 while TabLink.tsx:12 runs startsWith against a raw $, so no tab is highlighted there. #1807's instrument-hub links pass typed params too, so once it lands the table page loses its highlight as well.

    Plan

    1. encodeURIComponent the id at useSubjectQuery.ts:12 (dropping { params }) and at subjects/index.tsx:317.
    2. Navigate with { params: { subjectId }, to: '/datahub/subjects/$subjectId/table' } at subjects/index.tsx:325, :675 and :678.
    3. useNavItems.ts:282 and route.tsx:18 produce strings that are compared with location.pathname (Sidebar.tsx:58-59, Navbar.tsx:95-96, TabLink.tsx:12). Build them with useRouter().buildLocation({ params: { subjectId }, to }).pathname, with to: '/datahub/subjects/$subjectId' for the tab base. That is exactly what location.pathname holds after the navigation, and passing it back as a plain to string reaches the same subject. NavItem.url stays a string, and NavButton, TabLink and the walkthrough selectors (WalkthroughProvider.tsx:327, :344, :378, :397, which use the id 123) need no change. Putting params on NavItem would mean rewriting those checks too. Plain encodeURIComponent routes correctly but breaks highlighting for non-ASCII ids: for a group named Équipe it gives %C3%89quipe%24001, while location.pathname holds Équipe%24001.

    In step 3, there is no subjectId to pass when no session is active. Do not fall back to /datahub/subjects for the disabled item: disabled && location.pathname !== url would enable it on that page.

    Tests

    These assert the current behaviour and have to change: useSubjectQuery.test.ts:39 (the { params } argument), datahub/subjects/__tests__/index.test.tsx:184, :191 and :428, and $subjectId/__tests__/route.test.tsx:64-84. The route test also mocks the pathname with a raw $ (:14, :43, :73), which the router no longer produces once every link passes typed params. That test and useNavItems.test.ts render without a router, so they need useRouter mocked.

    For e2e, the regex /\/datahub\/subjects\/.+\/table$/ at datahub.spec.ts:168 also matches a broken / path, so a / variant of the lookup test must assert that subject-table is visible. getUrlWithParams (testing/src/pages/__root.page.ts:19-27) substitutes params without encoding, so drive the new subject-detail.spec.ts test through the sidebar and the table rather than goto. One custom id containing /, #, ? and % covers every case.

    Related

    • feat: add the instrument hub and record provenance #1807 edits the same four web files (TabLink.tsx, $subjectId/route.tsx, useNavItems.ts, datahub/subjects/index.tsx) but none of these lines, so either can land first. Only the line numbers move.
    • The API runs on Fastify (libnest's AppFactory), not Express. find-my-way rejects a malformed % with a 400 and decodes :id once. DELETE /v1/subjects/:id (apps/api/src/subjects/subjects.controller.ts:19) then runs decodeURIComponent a second time, so an id containing % throws and A%41 resolves to the subject AA. No web code calls that endpoint. It needs its own API issue.

    Triage note written with Claude Code. Code references point at main @ d86740e.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area: WebBugDifficulty: LowGood First IssuePriority: Medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions