@fluixi/api 1.0.0-alpha.107 → 1.0.0-alpha.109

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,106 @@
1
+ import {
2
+ rateLimit
3
+ } from "./chunk-CGGDPGK5.mjs";
4
+ import {
5
+ appendVary,
6
+ withHeaders
7
+ } from "./chunk-MI6ZXL3J.mjs";
8
+ import {
9
+ ApiError,
10
+ errorResponse
11
+ } from "./chunk-FIHRBQUV.mjs";
12
+
13
+ // src/security.ts
14
+ var allows = (origins, origin) => typeof origins === "function" ? origins(origin) : origins.includes(origin);
15
+ var DEFAULT_SECURITY_HEADERS = {
16
+ "content-security-policy": "default-src 'none'; frame-ancestors 'none'",
17
+ "cross-origin-opener-policy": "same-origin",
18
+ "cross-origin-resource-policy": "same-origin",
19
+ "referrer-policy": "no-referrer",
20
+ "strict-transport-security": "max-age=31536000; includeSubDomains",
21
+ "x-content-type-options": "nosniff",
22
+ "x-frame-options": "DENY",
23
+ "x-permitted-cross-domain-policies": "none",
24
+ "cache-control": "no-store"
25
+ };
26
+ function securityHeaders(options = {}) {
27
+ const merged = { ...DEFAULT_SECURITY_HEADERS, ...options };
28
+ const entries = Object.entries(merged).filter((e) => e[1] !== false);
29
+ return async (request, next) => withHeaders(await next(), (headers) => {
30
+ for (const [name, value] of entries) if (!headers.has(name)) headers.set(name, value);
31
+ });
32
+ }
33
+ var METHODS = ["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE"];
34
+ function cors(options) {
35
+ const { origins, credentials = true, methods = METHODS, allowHeaders, exposeHeaders, maxAge = 600 } = options;
36
+ return async (request, next) => {
37
+ const origin = request.headers.get("origin");
38
+ if (!origin || origin === new URL(request.url).origin) return next();
39
+ const allowed = allows(origins, origin);
40
+ const preflight = request.method === "OPTIONS" && request.headers.has("access-control-request-method");
41
+ if (preflight) {
42
+ if (!allowed) return refusal(403, "origin_not_allowed", "This origin may not call the API.");
43
+ const headers = new Headers({
44
+ "access-control-allow-origin": origin,
45
+ "access-control-allow-methods": methods.join(", "),
46
+ "access-control-max-age": String(maxAge),
47
+ vary: "Origin, Access-Control-Request-Headers"
48
+ });
49
+ const asked = request.headers.get("access-control-request-headers");
50
+ const allowedHeaders = allowHeaders ? allowHeaders.join(", ") : asked;
51
+ if (allowedHeaders) headers.set("access-control-allow-headers", allowedHeaders);
52
+ if (credentials) headers.set("access-control-allow-credentials", "true");
53
+ return new Response(null, { status: 204, headers });
54
+ }
55
+ const response = await next();
56
+ return withHeaders(response, (headers) => {
57
+ appendVary(headers, "Origin");
58
+ if (!allowed) return;
59
+ headers.set("access-control-allow-origin", origin);
60
+ if (credentials) headers.set("access-control-allow-credentials", "true");
61
+ if (exposeHeaders?.length) headers.set("access-control-expose-headers", exposeHeaders.join(", "));
62
+ });
63
+ };
64
+ }
65
+ var SAFE = /* @__PURE__ */ new Set(["GET", "HEAD", "OPTIONS"]);
66
+ function originCheck(options) {
67
+ return (request, next) => {
68
+ if (SAFE.has(request.method) || !request.headers.has("cookie")) return next();
69
+ const origin = request.headers.get("origin");
70
+ const site = request.headers.get("sec-fetch-site");
71
+ const self = new URL(request.url).origin;
72
+ if (site === "same-origin" || site === "none") return next();
73
+ if (origin && (origin === self || allows(options.origins, origin))) return next();
74
+ if (!site && !origin) return next();
75
+ return refusal(403, "cross_origin_refused", "Requests from this origin are refused.");
76
+ };
77
+ }
78
+ function apiSecurity(options = {}) {
79
+ const origins = options.origins ?? [];
80
+ const prefix = (options.prefix ?? "/api").replace(/\/$/, "");
81
+ const chain = [];
82
+ if (options.headers !== false) chain.push(securityHeaders(options.headers));
83
+ if (options.cors !== false) chain.push(cors({ ...options.cors, origins }));
84
+ if (options.originCheck !== false) chain.push(originCheck({ origins }));
85
+ for (const limit of options.rateLimits ?? []) chain.push(rateLimit(limit));
86
+ return (request, next) => {
87
+ const { pathname } = new URL(request.url);
88
+ if (pathname !== prefix && !pathname.startsWith(prefix + "/")) return next();
89
+ const run = (i) => {
90
+ const middleware = chain[i];
91
+ return middleware ? Promise.resolve(middleware(request, () => run(i + 1))) : next();
92
+ };
93
+ return run(0);
94
+ };
95
+ }
96
+ function refusal(status, code, message) {
97
+ return errorResponse(new ApiError(status, code, message));
98
+ }
99
+
100
+ export {
101
+ DEFAULT_SECURITY_HEADERS,
102
+ securityHeaders,
103
+ cors,
104
+ originCheck,
105
+ apiSecurity
106
+ };