Skip to content

Commit 4a20750

Browse files
Read a channel cursor with a malformed time as the first page, not a 500 (#640)
decodeChannelCursor only checked that recency was a string, and the page query casts it with ::timestamptz, so a hand-edited cursor answered 500. A recency that is not the UTC timestamp the encoder writes now reads as the first page, like every other malformed cursor. Co-authored-by: David McKay <davidmckayv@users.noreply.github.com>
1 parent 0504711 commit 4a20750

3 files changed

Lines changed: 64 additions & 2 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ Newest first. `Unreleased` is what is on `main` and not yet tagged.
88

99
## Unreleased
1010

11+
### A channel cursor with a malformed time reads as the first page, not a 500
12+
13+
`GET /api/channels` only checked that a cursor's time was a string before casting it with
14+
`::timestamptz`, so a hand-edited or corrupted cursor such as `{"recency": "not-a-date"}` answered
15+
500. A time that is not the UTC timestamp the list writes now reads as the first page, the way every
16+
other malformed cursor already did.
17+
1118
### A playground component's Published switch publishes its source too
1219

1320
The Published switch on an admin component page called the generic publication endpoint for every

‎server/src/channels/routes.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ const MAX_CHANNEL_PAGE = 200;
108108
*/
109109
type ChannelCursor = { pinned: boolean; recency: string; id: string };
110110

111-
function encodeChannelCursor(cursor: ChannelCursor): string {
111+
/** The shape the encoder writes: UTC, to the millisecond (older cursors) or the microsecond. */
112+
const CURSOR_RECENCY = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,6})?Z$/;
113+
114+
export function encodeChannelCursor(cursor: ChannelCursor): string {
112115
return Buffer.from(JSON.stringify(cursor), "utf8").toString("base64url");
113116
}
114117

@@ -117,8 +120,11 @@ function encodeChannelCursor(cursor: ChannelCursor): string {
117120
*
118121
* A cursor minted before `pinned` existed is malformed by this definition, and deliberately: it
119122
* describes a position in an ordering this query no longer has.
123+
*
124+
* `recency` has to be the timestamp the encoder writes, not merely a string: it is cast with
125+
* `::timestamptz` in the page query, so any other string used to reach PostgreSQL and answer 500.
120126
*/
121-
function decodeChannelCursor(
127+
export function decodeChannelCursor(
122128
value: string | undefined,
123129
): ChannelCursor | undefined {
124130
if (!value) return undefined;
@@ -128,6 +134,8 @@ function decodeChannelCursor(
128134
) as ChannelCursor;
129135
return typeof parsed?.id === "string" &&
130136
typeof parsed?.recency === "string" &&
137+
CURSOR_RECENCY.test(parsed.recency) &&
138+
!Number.isNaN(Date.parse(parsed.recency)) &&
131139
typeof parsed?.pinned === "boolean"
132140
? parsed
133141
: undefined;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { describe, expect, test } from "bun:test";
2+
import {
3+
decodeChannelCursor,
4+
encodeChannelCursor,
5+
} from "../src/channels/routes";
6+
7+
function encode(value: unknown): string {
8+
return Buffer.from(JSON.stringify(value), "utf8").toString("base64url");
9+
}
10+
11+
describe("decodeChannelCursor", () => {
12+
test("round-trips a cursor to the microsecond", () => {
13+
const cursor = {
14+
pinned: true,
15+
recency: "2026-09-01T00:00:00.123456Z",
16+
id: "channel-1",
17+
};
18+
expect(decodeChannelCursor(encodeChannelCursor(cursor))).toEqual(cursor);
19+
});
20+
21+
test("still reads a cursor minted to the millisecond", () => {
22+
const cursor = {
23+
pinned: false,
24+
recency: "2026-09-01T00:00:00.123Z",
25+
id: "channel-1",
26+
};
27+
expect(decodeChannelCursor(encode(cursor))).toEqual(cursor);
28+
});
29+
30+
test.each([
31+
"not-a-date",
32+
"",
33+
"2026-13-45T00:00:00Z",
34+
"1",
35+
"2026-09-01 00:00:00",
36+
"2026-09-01T00:00:00+02:00",
37+
])("reads a recency of %p as the first page", (recency) => {
38+
expect(
39+
decodeChannelCursor(encode({ pinned: false, recency, id: "channel-1" })),
40+
).toBeUndefined();
41+
});
42+
43+
test("reads garbage and a missing cursor as the first page", () => {
44+
expect(decodeChannelCursor("%%%")).toBeUndefined();
45+
expect(decodeChannelCursor(undefined)).toBeUndefined();
46+
});
47+
});

0 commit comments

Comments
 (0)