fix: deceptive link hosts render as text, not links or cards

- urlGuard: honestHttpUrl answers an http(s) URL only when its written
  host is the host the browser opens: no percent-escape, userinfo or
  backslash in the authority, no ideographic, fullwidth or halfwidth dot,
  fullwidth letter, invisible character or trailing dot in the host, and
  exactly two slashes after the scheme; IDN hosts in Unicode pass
- safeUrl allows an http(s) target only through honestHttpUrl, so
  renderChatText leaves a deceptive URL as text, bare or labelled
- linkify and messageUrls parse through it, so no card shows a host the
  text does not
- tests: urlGuard.test.ts and linkEmbeds cases; the SPA's copied tests
  are unchanged
This commit is contained in:
Jakub Zych
2026-11-02 18:35:52 +01:00
parent b6d3389e46
commit a798bb1acb
6 changed files with 168 additions and 11 deletions

View File

@@ -181,6 +181,8 @@ export default defineNuxtPlugin(() => {
`linkify(escapedHtml)` (same module) turns the URLs of already-escaped text into links, and `embedRendererFor(url)` answers the registered component of a URL.
Links and cards are made only for honest URLs: `honestHttpUrl(raw)` (`app/lib/urlGuard.ts`) answers the parsed URL when the host as written is the host the browser opens, else null. A URL with a percent-escape, userinfo (`@`) or a backslash in its authority, an ideographic, fullwidth or halfwidth dot, fullwidth letters, an invisible character or a trailing dot in its host, or a scheme not followed by exactly two slashes stays text: no anchor in the message (`safeUrl`, used by `renderChatText`) and no card (`messageUrls`, `linkify`). A card therefore never shows a host the text does not. Honest URLs, IDN hosts written in Unicode, ports, queries and fragments link and embed as before. The guard covers rendering only; refusing banned links is the server's job.
### Slash commands
Typing `/` at the start of the composer opens the commands of `GET {botsApiBase}/commands`. Picking one, or sending any text that starts with `/`, calls `POST {botsApiBase}/dispatch` with `{input}` instead of posting a message. The answer shows under the conversation as a row only the sender sees, dismissable; an unknown command shows the refusal.
@@ -197,7 +199,7 @@ Typing `/` at the start of the composer opens the commands of `GET {botsApiBase}
| `ChatReactionBar`, `ChatStagingChips`, `ChatDeleteDialog`, `ChatMemberPicker`, `ChatMentionPopup`, `ChatCommandPicker`, `ChatLinkEmbed` | Parts of the above |
| `ChatAvatar`, `ChatIcon`, `ChatNotices` | Initials avatar, stroke icons, short notices |
Composables: `useChat()` (the store, one per app, state in `useState` keys prefixed `chat:`), `useChatApi()` (the HTTP client bound to runtime config and the hook), `useChatNotice()`. The pure modules in `app/lib/` are imported explicitly, never auto-imported: `client.ts` (`createChatClient`, `ChatApiError`, `userIdFromToken`), `realtime.ts`, `linkEmbeds.ts`, `chatEvents.ts`, `chatFormat.ts`, `chatText.ts`, `chatUnread.ts`, `format.ts`, `attachments.ts`, `slots.ts` (`dmActionsTarget`, the DM-only rule of the conversation-actions slot).
Composables: `useChat()` (the store, one per app, state in `useState` keys prefixed `chat:`), `useChatApi()` (the HTTP client bound to runtime config and the hook), `useChatNotice()`. The pure modules in `app/lib/` are imported explicitly, never auto-imported: `client.ts` (`createChatClient`, `ChatApiError`, `userIdFromToken`), `realtime.ts`, `linkEmbeds.ts`, `urlGuard.ts` (`honestHttpUrl`), `chatEvents.ts`, `chatFormat.ts`, `chatText.ts`, `chatUnread.ts`, `format.ts`, `attachments.ts`, `slots.ts` (`dmActionsTarget`, the DM-only rule of the conversation-actions slot).
## Tokens
@@ -229,7 +231,8 @@ pnpm test # vitest in Node: app/**/*.test.ts and tests/**/*.test.
```
- `app/lib/chatEvents.test.ts`, `chatFormat.test.ts`, `chatText.test.ts`, `chatUnread.test.ts` and `realtime.test.ts` are the Golem15 SPA's tests of the modules ported here, with only their import paths changed (`vitest.config.ts` maps the SPA's `@/api/http` to `tests/support/http.ts`).
- `app/lib/linkEmbeds.test.ts`: `linkify` (escaped input, http(s) only, `target` and `rel`), `messageUrls` and the renderer registry.
- `app/lib/linkEmbeds.test.ts`: `linkify` (escaped input, http(s) only, `target` and `rel`, deceptive hosts left as text), `messageUrls` (deceptive hosts skipped) and the renderer registry.
- `app/lib/urlGuard.test.ts`: `honestHttpUrl` on deceptive URLs (percent-escapes, fullwidth letters, ideographic and other dots, userinfo, backslashes, trailing dots, invisible characters, malformed scheme separators) and honest ones (ports, queries, fragments, IDN hosts in Unicode and punycode), and `renderChatText`, `safeUrl`, `messageUrls` and `linkify` on both.
- `tests/client.test.ts`: every endpoint's URL, method and body, the 401 refresh-and-retry, `unauthorized`, the 422 mapping and the multipart upload.
- `tests/store.test.ts`: `useChat` with stubbed Nuxt composables, a fake client and a fake realtime client: the bootstrap (and the anonymous, non-member and no-host cases), lazy channel joins, DMs, live messages and unread counts, threads, pins, reactions, slash commands, polling without realtime, `onUserEvent` and the DM-only `conversation-actions` rule.