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
8 changes: 8 additions & 0 deletions apps/commandboard-api/src/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ describe("CommandBoard API contracts", () => {
expect(opmlBody).toContain("<opml");
});

it("rejects malformed encoded feed discovery paths as client errors", async () => {
const response = await fetch(`${baseUrl}/rss/discover/%E0%A4%A.xml`);
const body = await response.json() as { error: string };

expect(response.status).toBe(400);
expect(body).toEqual({ error: "Invalid path encoding" });
});

it("exposes sh1pt project and action contracts", async () => {
const projectsResponse = await fetch(`${baseUrl}/api/plugins/sh1pt/projects`);
const projectsBody = await projectsResponse.json() as { projects: Array<{ id: string; board: string; status: string; actions: number }> };
Expand Down
24 changes: 20 additions & 4 deletions apps/commandboard-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ async function route(request: IncomingMessage, response: ServerResponse) {

const formattedDiscover = matchFormattedDiscoverPath(url.pathname);
if (request.method === "GET" && formattedDiscover) {
if (formattedDiscover.format === "invalid-encoding") {
json(response, 400, { error: "Invalid path encoding" });
return;
}

const result = await discoverFeeds({
q: formattedDiscover.keyword,
type: "all",
Expand Down Expand Up @@ -312,23 +317,34 @@ function listParam(value: string | null) {
function matchFormattedDiscoverPath(pathname: string) {
const rss = /^\/rss\/discover\/(.+)\.xml$/.exec(pathname);
if (rss) {
return { format: "rss" as const, keyword: decodeURIComponent(rss[1]) };
return formattedDiscoverMatch("rss", rss[1]);
}
const opml = /^\/opml\/discover\/(.+)\.xml$/.exec(pathname) ?? /^\/rss\/discover\/(.+)\.opml$/.exec(pathname);
if (opml) {
return { format: "opml" as const, keyword: decodeURIComponent(opml[1]) };
return formattedDiscoverMatch("opml", opml[1]);
}
const atom = /^\/atom\/discover\/(.+)\.xml$/.exec(pathname);
if (atom) {
return { format: "atom" as const, keyword: decodeURIComponent(atom[1]) };
return formattedDiscoverMatch("atom", atom[1]);
}
const jsonFeed = /^\/json-feed\/discover\/(.+)\.json$/.exec(pathname);
if (jsonFeed) {
return { format: "json-feed" as const, keyword: decodeURIComponent(jsonFeed[1]) };
return formattedDiscoverMatch("json-feed", jsonFeed[1]);
}
return undefined;
}

function formattedDiscoverMatch(format: "rss" | "opml" | "atom" | "json-feed", encodedKeyword: string) {
try {
return { format, keyword: decodeURIComponent(encodedKeyword) };
} catch (error) {
if (error instanceof URIError) {
return { format: "invalid-encoding" as const };
}
throw error;
}
}

export function startCommandBoardServer(port = Number(process.env.PORT ?? 4010)) {
const server = createCommandBoardServer();
server.listen(port, () => {
Expand Down
Loading