Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Validate deployment and stored broker credentials with the same host, method and path rules

Josh Francef0ed5c3a1c60
diff --git a/src/api/routes/admin-resources.ts b/src/api/routes/admin-resources.ts
index 7d8fadf18..2824e645c 100644
--- a/src/api/routes/admin-resources.ts
+++ b/src/api/routes/admin-resources.ts
@@ -30,6 +30,9 @@ import {
credentialInjectionError,
isValidCredentialSlug,
isValidServiceCredentialEnvKey,
+ isValidBrokerHost,
+ isValidBrokerMethod,
+ isValidBrokerPathPrefix,
type CredentialInjection,
type DecryptedServiceCredential,
type PublicServiceCredential,
@@ -1117,7 +1120,7 @@ export const ADMIN_RESOURCES: readonly AdminResource[] = [
}
const host = typeof b.host === "string" ? b.host.trim().toLowerCase() : "";
if (!name) return { error: "service-credentials requires { slug, name }" };
- if (delivery === "broker" && (!host || /[/:\s]/.test(host))) {
+ if (delivery === "broker" && !isValidBrokerHost(host)) {
return { error: "broker delivery requires { host } (a bare hostname)" };
}
if (delivery === "env" && host) return { error: "a pinned host only applies to broker delivery" };
@@ -1130,7 +1133,7 @@ export const ADMIN_RESOURCES: readonly AdminResource[] = [
.map((method) => method.trim().toUpperCase())
.filter(Boolean)
: undefined;
- if (methods?.some((method) => !/^[A-Z]+$/.test(method)))
+ if (methods?.some((method) => !isValidBrokerMethod(method)))
return { error: "allowed methods must be comma-separated HTTP method names" };
const paths = Array.isArray(b.allowedPathPrefixes)
? b.allowedPathPrefixes
@@ -1138,7 +1141,7 @@ export const ADMIN_RESOURCES: readonly AdminResource[] = [
.map((path) => path.trim())
.filter(Boolean)
: undefined;
- if (paths?.some((path) => !path.startsWith("/") || /[\r\n]/.test(path)))
+ if (paths?.some((path) => !isValidBrokerPathPrefix(path)))
return { error: "each allowed path prefix must be one line starting with /" };
const desired = Array.isArray(b.grantees) ? [...new Set(b.grantees.map(String))] : undefined;
const badGrantee = desired?.find((g) => {
diff --git a/src/credentials/deployment-credentials.ts b/src/credentials/deployment-credentials.ts
index 606335078..7210f7264 100644
--- a/src/credentials/deployment-credentials.ts
+++ b/src/credentials/deployment-credentials.ts
@@ -1,7 +1,12 @@
+import { errMessage } from "../util/errors.ts";
import {
credentialInjectionError,
isComposioHost,
+ isValidBrokerHost,
+ isValidBrokerMethod,
+ isValidBrokerPathPrefix,
isValidCredentialSlug,
+ isValidServiceCredentialEnvKey,
type CredentialInjection,
type DecryptedServiceCredential,
type PublicServiceCredential,
@@ -30,7 +35,6 @@ const FIELDS = new Set([
"allowedPathPrefixes",
"maxRequestsPerDay",
]);
-const METHODS = new Set(["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE"]);
function stringList(value: unknown, label: string, valid: (item: string) => boolean): string[] | undefined {
if (value === undefined) return undefined;
@@ -50,21 +54,21 @@ function parseEntry(raw: unknown, index: number, env: NodeJS.ProcessEnv): Deploy
throw new Error(`BROKER_CREDENTIALS[${index}].slug must be lowercase kebab-case (a-z, 0-9, -)`);
const at = `BROKER_CREDENTIALS "${slug}"`;
const host = entry.host;
- if (typeof host !== "string" || !/^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+$/.test(host))
+ if (typeof host !== "string" || !isValidBrokerHost(host))
throw new Error(`${at}: host must be a bare lowercase hostname`);
if (isComposioHost(host)) throw new Error(`${at}: Composio hosts cannot be brokered`);
const secretEnv = entry.secretEnv;
- if (typeof secretEnv !== "string" || !/^[A-Z][A-Z0-9_]*$/.test(secretEnv) || secretEnv === "BROKER_CREDENTIALS")
+ if (typeof secretEnv !== "string" || !isValidServiceCredentialEnvKey(secretEnv) || secretEnv === "BROKER_CREDENTIALS")
throw new Error(`${at}: secretEnv must name an UPPER_SNAKE_CASE env var`);
const secret = env[secretEnv]?.trim();
if (!secret) throw new Error(`${at}: env var ${secretEnv} is unset or empty`);
const injectionError = credentialInjectionError(entry.injection);
if (injectionError) throw new Error(`${at}: ${injectionError}`);
- const allowedMethods = stringList(entry.allowedMethods, `${at}: allowedMethods`, (m) => METHODS.has(m));
+ const allowedMethods = stringList(entry.allowedMethods, `${at}: allowedMethods`, isValidBrokerMethod);
const allowedPathPrefixes = stringList(
entry.allowedPathPrefixes,
`${at}: allowedPathPrefixes`,
- (p) => p.startsWith("/") && !/[\r\n]/.test(p),
+ isValidBrokerPathPrefix,
);
const cap = entry.maxRequestsPerDay;
if (cap !== undefined && !(typeof cap === "number" && Number.isSafeInteger(cap) && cap >= 1))
@@ -90,8 +94,8 @@ export function deploymentBrokerCredentialsFromEnv(env: NodeJS.ProcessEnv): Depl
let parsed: unknown;
try {
parsed = JSON.parse(raw);
- } catch {
- throw new Error("BROKER_CREDENTIALS must be a JSON array");
+ } catch (error) {
+ throw new Error(`BROKER_CREDENTIALS must be a JSON array (${errMessage(error)})`, { cause: error });
}
if (!Array.isArray(parsed)) throw new Error("BROKER_CREDENTIALS must be a JSON array");
const credentials = parsed.map((entry, index) => parseEntry(entry, index, env));
@@ -116,12 +120,13 @@ function decrypted(c: DeploymentBrokerCredential): DecryptedServiceCredential {
...(c.maxRequestsPerDay !== undefined ? { maxRequestsPerDay: c.maxRequestsPerDay } : {}),
deployments: false,
enabled: true,
+ source: "deployment",
};
}
function projected(c: DeploymentBrokerCredential): PublicServiceCredential {
const { secret: _secret, ...rest } = decrypted(c);
- return { ...rest, hasSecret: true, source: "deployment", updatedAt: 0 };
+ return { ...rest, hasSecret: true, updatedAt: 0 };
}
export function reservedSlugError(slug: string): Error {
diff --git a/src/credentials/keychain.ts b/src/credentials/keychain.ts
index 61bdbe11a..437c7c8cf 100644
--- a/src/credentials/keychain.ts
+++ b/src/credentials/keychain.ts
@@ -209,6 +209,7 @@ export interface DecryptedServiceCredential {
deployments: boolean;
enabled: boolean;
maxRequestsPerDay?: number;
+ source?: "deployment";
}
export function isValidCredentialSlug(slug: string): boolean {
@@ -219,6 +220,18 @@ export function isValidServiceCredentialEnvKey(envKey: string): boolean {
return /^[A-Z][A-Z0-9_]*$/.test(envKey) && !envKey.startsWith("AGENT_");
}
+export function isValidBrokerHost(host: string): boolean {
+ return Boolean(host) && host === host.toLowerCase() && !/[/:\s]/.test(host);
+}
+
+export function isValidBrokerMethod(method: string): boolean {
+ return /^[A-Z]+$/.test(method);
+}
+
+export function isValidBrokerPathPrefix(path: string): boolean {
+ return path.startsWith("/") && !/[\r\n]/.test(path);
+}
+
export interface ServiceCredentialReader {
getServiceCredentialSecret(orgScopeId: ScopeId, slug: string): Promise<DecryptedServiceCredential | null>;
}