Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkgs/pkarr-worker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ not a traffic relay. Storage is one SQLite-backed Durable Object per
public key (named by the key's z-base-32 form), which gives strongly
consistent newest-wins semantics per key. Records expire after
`RETENTION_SECONDS` (default 7 days) without a refresh, via the object's
alarm. Per-IP rate limits guard all three routes. Clients verify
alarm. Configured values must be positive whole base-10 seconds (digits
only); missing, malformed, non-positive, or unsupported values fall back
to seven days. Per-IP rate limits guard all three routes. Clients verify
signatures themselves; the Worker also verifies before storing, but it
is trusted for availability, not integrity.

Expand Down
6 changes: 4 additions & 2 deletions pkgs/pkarr-worker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// verifies before storing, but is trusted for availability, not integrity.

import { DurableObject } from "cloudflare:workers";
import { retentionMilliseconds } from "./retention";
import {
MAX_RELAY_PAYLOAD,
buildDohResponse,
Expand Down Expand Up @@ -64,8 +65,9 @@ export class PkarrStore extends DurableObject<Env> {
payload,
Number(timestampMicros),
);
const retentionMs = Number(this.env.RETENTION_SECONDS || "604800") * 1000;
await this.ctx.storage.setAlarm(Date.now() + retentionMs);
const now = Date.now();
const retentionMs = retentionMilliseconds(this.env.RETENTION_SECONDS, now);
await this.ctx.storage.setAlarm(now + retentionMs);
return "stored";
}

Expand Down
22 changes: 22 additions & 0 deletions pkgs/pkarr-worker/src/retention.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const DEFAULT_RETENTION_SECONDS = 604800;
const MAX_ALARM_TIMESTAMP = 8_640_000_000_000_000;

/** RETENTION_SECONDS is positive whole decimal seconds; invalid or unsupported values use seven days. */
export function retentionMilliseconds(value: string | undefined, now: number): number {
const defaultMilliseconds = DEFAULT_RETENTION_SECONDS * 1000;
if (value === undefined || !/^\d+$/.test(value)) return defaultMilliseconds;

const seconds = Number(value);
const milliseconds = seconds * 1000;
const alarmTimestamp = now + milliseconds;
if (
!Number.isSafeInteger(seconds) ||
seconds <= 0 ||
!Number.isSafeInteger(milliseconds) ||
!Number.isSafeInteger(alarmTimestamp) ||
alarmTimestamp > MAX_ALARM_TIMESTAMP
) {
return defaultMilliseconds;
}
return milliseconds;
}
92 changes: 90 additions & 2 deletions pkgs/pkarr-worker/test/worker.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { SELF } from "cloudflare:test";
import { describe, expect, it } from "vitest";
import { env as testBindings, runInDurableObject, SELF } from "cloudflare:test";
import { describe, expect, it, vi } from "vitest";
import worker, { type Env } from "../src/index";
import { parseDnsMessage, signableBytes, z32Decode, z32Encode } from "../src/pkarr";
import { retentionMilliseconds } from "../src/retention";
import { buildRelayPayload, dnsPacketWithTxt, dnsQuery, keypairFromSeed } from "./helpers";

const BASE = "https://pkarr.test";
Expand Down Expand Up @@ -28,6 +30,39 @@ async function get(key: string): Promise<Response> {
return SELF.fetch(`${BASE}/pkarr/${key}`);
}

function mockStore(retentionSeconds: string | undefined) {
let payload: Uint8Array | null = null;
const alarms: number[] = [];
const store = {
async putPacket(value: Uint8Array) {
payload = value;
const now = Date.now();
alarms.push(now + retentionMilliseconds(retentionSeconds, now));
return "stored" as const;
},
async getPacket() {
return payload;
},
};
return { store, alarms };
}

function testEnv(store: ReturnType<typeof mockStore>["store"], retentionSeconds: string | undefined): Env {
const allow = { limit: async () => ({ success: true }) };
return {
ORIGIN,
RETENTION_SECONDS: retentionSeconds as string,
DNS_TTL: "30",
PUT_LIMITER: allow,
GET_LIMITER: allow,
DOH_LIMITER: allow,
PKARR_STORE: {
idFromName: (name: string) => name,
get: () => store,
} as unknown as Env["PKARR_STORE"],
} as unknown as Env;
}

describe("z32 codec", () => {
it("matches hand-computed vectors", () => {
expect(z32Encode(new Uint8Array([0x00]))).toBe("yy");
Expand Down Expand Up @@ -57,6 +92,52 @@ describe("signable bytes", () => {
});
});

describe("RETENTION_SECONDS", () => {
it.each([
{ label: "missing", setting: undefined, expectedMilliseconds: 604_800_000 },
{ label: "zero", setting: "0", expectedMilliseconds: 604_800_000 },
{ label: "negative", setting: "-1", expectedMilliseconds: 604_800_000 },
{ label: "unit suffix", setting: "7d", expectedMilliseconds: 604_800_000 },
{ label: "whitespace", setting: " ", expectedMilliseconds: 604_800_000 },
{ label: "scientific notation", setting: "1e9", expectedMilliseconds: 604_800_000 },
{ label: "fractional seconds", setting: "3600.5", expectedMilliseconds: 604_800_000 },
{ label: "plus sign", setting: "+3600", expectedMilliseconds: 604_800_000 },
{ label: "out-of-range value", setting: "8640000000000", expectedMilliseconds: 604_800_000 },
{ label: "seven-day default", setting: "604800", expectedMilliseconds: 604_800_000 },
{ label: "valid decimal seconds", setting: "3600", expectedMilliseconds: 3_600_000 },
])("$label still supports PUT then GET and schedules the expected retention", async ({
setting,
expectedMilliseconds,
}) => {
const now = 1_800_000_000_000;
const nowSpy = vi.spyOn(Date, "now").mockReturnValue(now);
try {
const { store, alarms } = mockStore(setting);
const env = testEnv(store, setting);
const kp = keypair(25);
const key = z32(kp.publicKey);
const payload = buildRelayPayload(kp, TS, dnsPacketWithTxt(`_iroh.${key}`, "retention-test"));

const putResponse = await worker.fetch(
new Request(`${BASE}/pkarr/${key}`, {
method: "PUT",
headers: { "content-type": "application/x-pkarr-signed-packet" },
body: payload,
}),
env,
);
expect(putResponse.status).toBe(204);

const getResponse = await worker.fetch(new Request(`${BASE}/pkarr/${key}`), env);
expect(getResponse.status).toBe(200);
expect(new Uint8Array(await getResponse.arrayBuffer())).toEqual(payload);
expect(alarms).toEqual([now + expectedMilliseconds]);
} finally {
nowSpy.mockRestore();
}
});
});

describe("pkarr HTTP relay", () => {
it("publishes and resolves a signed packet verbatim", async () => {
const kp = keypair(11);
Expand All @@ -68,6 +149,13 @@ describe("pkarr HTTP relay", () => {
expect(res.status).toBe(200);
expect(res.headers.get("content-type")).toBe("application/x-pkarr-signed-packet");
expect(new Uint8Array(await res.arrayBuffer())).toEqual(payload);

const bindings = testBindings as unknown as Env;
const stub = bindings.PKARR_STORE.get(bindings.PKARR_STORE.idFromName(key));
const alarmAt = await runInDurableObject(stub, (_instance, state) => state.storage.getAlarm());
expect(alarmAt).not.toBeNull();
expect(alarmAt! - Date.now()).toBeGreaterThan(604_790_000);
expect(alarmAt! - Date.now()).toBeLessThanOrEqual(604_800_000);
});

it("returns 404 for an unknown key", async () => {
Expand Down