-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpublic-routes.test.ts
More file actions
154 lines (135 loc) · 5.05 KB
/
Copy pathpublic-routes.test.ts
File metadata and controls
154 lines (135 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { describe, expect, it, vi } from "vitest";
import { PluginRouteError } from "emdash";
import type { RouteContext } from "emdash";
import { createPlugin } from "../src/index.js";
interface AnonymousRouteResponse {
success: boolean;
data?: unknown;
error?: { code: string; message: string };
status?: number;
}
interface PublicRouteRuntime {
getPluginRouteMeta(pluginId: string, path: string): { public: boolean } | null;
handlePluginApiRoute(
pluginId: string,
method: string,
path: string,
request: Request,
): Promise<AnonymousRouteResponse>;
}
type PublicRouteHandler = (
pluginId: string,
method: string,
path: string,
request: Request,
) => Promise<AnonymousRouteResponse>;
type CreatePublicRouteHandler = (runtime: PublicRouteRuntime) => PublicRouteHandler;
function isPublicRouteModule(value: unknown): value is {
createPublicPluginApiRouteHandler: CreatePublicRouteHandler;
} {
return (
typeof value === "object" &&
value !== null &&
"createPublicPluginApiRouteHandler" in value &&
typeof value.createPublicPluginApiRouteHandler === "function"
);
}
const publicRouteModulePath = "../node_modules/emdash/src/astro/public-plugin-api-routes.js";
const publicRouteModule: unknown = await import(/* @vite-ignore */ publicRouteModulePath);
if (!isPublicRouteModule(publicRouteModule)) {
throw new Error("EmDash public plugin route handler is unavailable");
}
const { createPublicPluginApiRouteHandler } = publicRouteModule;
function createAnonymousDispatcher() {
const plugin = createPlugin();
const store = new Map<string, unknown>();
const kv = {
get: vi.fn(async (key: string) => store.get(key)),
set: vi.fn(async (key: string, value: unknown) => {
store.set(key, value);
}),
delete: vi.fn(async (key: string) => store.delete(key)),
list: vi.fn(async (prefix?: string) =>
Array.from(store.entries())
.filter(([key]) => !prefix || key.startsWith(prefix))
.map(([key, value]) => ({ key, value })),
),
};
const dispatch = vi.fn(async (_pluginId: string, _method: string, path: string, request: Request) => {
const route = plugin.routes[path.replace(/^\//, "")];
if (!route) {
return {
success: false,
error: { code: "NOT_FOUND", message: "Plugin route not found" },
};
}
try {
const data = await route.handler({ request, kv } as unknown as RouteContext);
return { success: true, data };
} catch (error) {
if (error instanceof PluginRouteError) {
return {
success: false,
error: { code: error.code, message: error.message },
status: error.status,
};
}
throw error;
}
});
const handler = createPublicPluginApiRouteHandler({
getPluginRouteMeta(pluginId, path) {
const route = pluginId === plugin.id ? plugin.routes[path.replace(/^\//, "")] : undefined;
return route ? { public: route.public === true } : null;
},
handlePluginApiRoute: dispatch,
});
return { dispatch, handler, kv, plugin };
}
const publishedFileRoutes = ["indexnow/key", "llms/txt"] as const;
const nonGetMethods = ["POST", "PUT", "PATCH", "DELETE"] as const;
describe("published-file route access", () => {
it.each(publishedFileRoutes)("allows anonymous GET access to %s", async (path) => {
const { dispatch, handler } = createAnonymousDispatcher();
const request = new Request(`https://example.com/${path}`);
await expect(handler("seo", "GET", path, request)).resolves.toMatchObject({
success: true,
});
expect(dispatch).toHaveBeenCalledOnce();
});
it.each(
publishedFileRoutes.flatMap((path) => nonGetMethods.map((method) => [method, path] as const)),
)("rejects anonymous %s access to %s before route side effects", async (method, path) => {
const { dispatch, handler, kv } = createAnonymousDispatcher();
const request = new Request(`https://example.com/${path}`, { method });
await expect(handler("seo", method, path, request)).resolves.toMatchObject({
success: false,
error: { code: "METHOD_NOT_ALLOWED" },
status: 405,
});
expect(dispatch).toHaveBeenCalledOnce();
expect(kv.get).not.toHaveBeenCalled();
expect(kv.set).not.toHaveBeenCalled();
expect(kv.list).not.toHaveBeenCalled();
});
it.each([
["GET", "settings"],
["POST", "settings/save"],
])("keeps %s %s protected", async (method, path) => {
const { dispatch, handler } = createAnonymousDispatcher();
const request = new Request(`https://example.com/${path}`);
await expect(handler("seo", method, path, request)).resolves.toMatchObject({
success: false,
error: { code: "NOT_FOUND" },
});
expect(dispatch).not.toHaveBeenCalled();
});
it("does not broaden public access beyond published read routes", () => {
const { plugin } = createAnonymousDispatcher();
const publicRoutes = Object.entries(plugin.routes)
.filter(([, route]) => route.public === true)
.map(([path]) => path)
.sort();
expect(publicRoutes).toEqual(["indexnow/key", "llms/txt", "schema/map"]);
});
});