Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
+75
@@ -0,0 +1,75 @@
|
||||
import type { PluginCapabilityName } from "./capabilities.js";
|
||||
|
||||
export function isCapabilityCallWithinScope(args: {
|
||||
capability: PluginCapabilityName;
|
||||
callArgs: readonly unknown[];
|
||||
scope: unknown;
|
||||
}): boolean {
|
||||
switch (args.capability) {
|
||||
case "http.outbound":
|
||||
return isHttpOutboundCallWithinScope(args.callArgs, args.scope);
|
||||
case "secret.read":
|
||||
return isSecretReadCallWithinScope(args.callArgs, args.scope);
|
||||
case "db.read":
|
||||
case "db.write":
|
||||
return isDatabaseCallWithinScope(args.callArgs, args.scope);
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function isHttpOutboundCallWithinScope(callArgs: readonly unknown[], scope: unknown): boolean {
|
||||
const request = readObject(callArgs[0]);
|
||||
const scopeObject = readObject(scope);
|
||||
const allowedHosts = normalizeAllowedHosts(scopeObject?.allowedHosts);
|
||||
if (!request || allowedHosts.size === 0 || callArgs.length !== 1) return false;
|
||||
const url = typeof request.url === "string" ? parseScopedUrl(request.url) : null;
|
||||
if (!url) return false;
|
||||
return allowedHosts.has(url.hostname.toLowerCase());
|
||||
}
|
||||
|
||||
function isSecretReadCallWithinScope(callArgs: readonly unknown[], scope: unknown): boolean {
|
||||
const request = readObject(callArgs[0]);
|
||||
if (!request || callArgs.length !== 1 || typeof request.key !== "string") return false;
|
||||
const scopeObject = readObject(scope);
|
||||
if (typeof scopeObject?.keyPattern !== "string") return false;
|
||||
return globMatches(scopeObject.keyPattern, request.key);
|
||||
}
|
||||
|
||||
function isDatabaseCallWithinScope(callArgs: readonly unknown[], scope: unknown): boolean {
|
||||
const request = readObject(callArgs[0]);
|
||||
if (!request || callArgs.length !== 1 || typeof request.table !== "string") return false;
|
||||
const scopeObject = readObject(scope);
|
||||
if (typeof scopeObject?.tablePrefix !== "string") return false;
|
||||
if ("sql" in request || "query" in request || "raw" in request) return false;
|
||||
return request.table.startsWith(scopeObject.tablePrefix);
|
||||
}
|
||||
|
||||
function readObject(value: unknown): Record<string, unknown> | null {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
||||
return value as Record<string, unknown>;
|
||||
}
|
||||
|
||||
function normalizeAllowedHosts(hosts: unknown): ReadonlySet<string> {
|
||||
if (!Array.isArray(hosts)) return new Set();
|
||||
return new Set(hosts.filter((host): host is string => typeof host === "string").map((host) => host.toLowerCase()));
|
||||
}
|
||||
|
||||
function parseScopedUrl(value: string): URL | null {
|
||||
try {
|
||||
const url = new URL(value);
|
||||
if (url.protocol !== "https:") return null;
|
||||
if (url.username || url.password || url.port) return null;
|
||||
return url;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function globMatches(pattern: string, value: string): boolean {
|
||||
const source = pattern
|
||||
.split("*")
|
||||
.map((part) => part.replace(/[\\^$+?.()|[\]{}]/gu, "\\$&"))
|
||||
.join(".*");
|
||||
return new RegExp(`^${source}$`, "u").test(value);
|
||||
}
|
||||
Reference in new issue
Block a user