Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

6.9 KB
Raw Blame
import { z } from "zod";

import { AGENT_VAULT } from "@app/lib/api-docs";

import {
  AGENT_VAULT_HEADER_NAME_MESSAGE,
  AGENT_VAULT_HEADER_NAME_RE,
  AGENT_VAULT_NO_CONTROL_CHARS_MESSAGE,
  AGENT_VAULT_NO_CONTROL_CHARS_RE
} from "./agent-vault-credential-schemas";
import { AgentVaultSubstitutionSurface } from "./agent-vault-enums";
import {
  AGENT_VAULT_INLINE_SERVICE_VARIABLE_MESSAGE,
  AGENT_VAULT_MALFORMED_REFERENCE_MESSAGE,
  AGENT_VAULT_TOO_MANY_REFERENCES_MESSAGE,
  hasMalformedVariableReference,
  hasTooManyVariableReferences,
  hasVariableReference
} from "./agent-vault-variable-fns";

export const AGENT_VAULT_MAX_CUSTOM_HEADERS = 20;
export const AGENT_VAULT_MAX_SUBSTITUTIONS = 20;

// Set by the proxy or stripped as hop-by-hop, so naming one is silently dropped or corrupts the request.
const RESERVED_HEADER_NAMES = new Set([
  "host",
  "content-length",
  "transfer-encoding",
  "connection",
  "proxy-connection",
  "upgrade",
  "te",
  "trailer",
  "keep-alive",
  "proxy-authenticate",
  "proxy-authorization"
]);

export const AGENT_VAULT_RESERVED_HEADER_MESSAGE = "This header is set by the proxy and can't be overridden.";

// Shared with the bearer credential's own header name: both end up on the request the same way, so both
// have the same reserved names.
export const agentVaultHeaderNameSchema = z
  .string()
  .trim()
  .min(1)
  .max(128)
  .regex(AGENT_VAULT_HEADER_NAME_RE, AGENT_VAULT_HEADER_NAME_MESSAGE)
  .refine((name) => !RESERVED_HEADER_NAMES.has(name.toLowerCase()), AGENT_VAULT_RESERVED_HEADER_MESSAGE);

const hasNoDoubleBraces = (text: string) => !text.includes("{{") && !text.includes("}}");

// Shared with the bearer credential's own prefix. A prefix is sent as typed, so double braces in one would read
// as a variable reference the proxy never fills in.
export const agentVaultHeaderPrefixSchema = z
  .string()
  .trim()
  .max(64)
  .regex(AGENT_VAULT_NO_CONTROL_CHARS_RE, AGENT_VAULT_NO_CONTROL_CHARS_MESSAGE)
  .refine(hasNoDoubleBraces, "A prefix can't contain {{ or }}. Put a variable reference in the value instead.");

// For every field a variable can be used in, so a mistyped reference fails the save, not the request.
export const acceptsVariableReferences = (schema: z.ZodString) =>
  schema
    .refine((value) => !hasMalformedVariableReference(value), AGENT_VAULT_MALFORMED_REFERENCE_MESSAGE)
    .refine((value) => !hasTooManyVariableReferences(value), AGENT_VAULT_TOO_MANY_REFERENCES_MESSAGE);

const secretValueSchema = acceptsVariableReferences(
  z.string().min(1).max(8192).regex(AGENT_VAULT_NO_CONTROL_CHARS_RE, AGENT_VAULT_NO_CONTROL_CHARS_MESSAGE)
);

// No minimum length: a short placeholder over-matches, but that is the author's own doing. No double braces,
// which would read as a variable reference to anyone looking at the service.
const placeholderSchema = z
  .string()
  .trim()
  .min(1)
  .max(255)
  .regex(AGENT_VAULT_NO_CONTROL_CHARS_RE, AGENT_VAULT_NO_CONTROL_CHARS_MESSAGE)
  .refine(
    hasNoDoubleBraces,
    "A placeholder can't contain {{ or }}. Double braces are reserved for variable references."
  );

const surfacesSchema = z
  .array(z.nativeEnum(AgentVaultSubstitutionSurface))
  .min(1, "Pick at least one place to look for the placeholder.")
  .max(Object.keys(AgentVaultSubstitutionSurface).length)
  .transform((surfaces) => [...new Set(surfaces)]);

// Rows resolve by `id` and otherwise by name or placeholder, so a hand-written call can edit one without
// fetching the service first. `value` is optional on update because omitting it keeps what is sealed.
export const AgentVaultCustomHeaderInputSchema = z.object({
  name: agentVaultHeaderNameSchema.describe(AGENT_VAULT.SERVICE.customHeaderName),
  prefix: agentVaultHeaderPrefixSchema.optional().describe(AGENT_VAULT.SERVICE.customHeaderPrefix),
  value: secretValueSchema.describe(AGENT_VAULT.SERVICE.customHeaderValue)
});

export const AgentVaultCustomHeaderUpdateSchema = z.object({
  id: z.string().uuid().optional().describe(AGENT_VAULT.SERVICE.customHeaderId),
  name: agentVaultHeaderNameSchema.describe(AGENT_VAULT.SERVICE.customHeaderName),
  prefix: agentVaultHeaderPrefixSchema.optional().describe(AGENT_VAULT.SERVICE.updateCustomHeaderPrefix),
  value: secretValueSchema.optional().describe(AGENT_VAULT.SERVICE.updateCustomHeaderValue)
});

export const AgentVaultSubstitutionInputSchema = z.object({
  placeholder: placeholderSchema.describe(AGENT_VAULT.SERVICE.placeholder),
  surfaces: surfacesSchema.describe(AGENT_VAULT.SERVICE.surfaces),
  value: secretValueSchema.describe(AGENT_VAULT.SERVICE.substitutionValue)
});

export const AgentVaultSubstitutionUpdateSchema = z.object({
  id: z.string().uuid().optional().describe(AGENT_VAULT.SERVICE.substitutionId),
  placeholder: placeholderSchema.describe(AGENT_VAULT.SERVICE.placeholder),
  surfaces: surfacesSchema.describe(AGENT_VAULT.SERVICE.surfaces),
  value: secretValueSchema.optional().describe(AGENT_VAULT.SERVICE.updateSubstitutionValue)
});

// For every value of a service sent inline with a session. Whoever starts a session names the hosts its inline
// services reach, so a variable reference would let them send any variable's value to a host of their choosing.
export const acceptsNoVariableReferences = (schema: z.ZodString) =>
  schema.refine((value) => !hasVariableReference(value), AGENT_VAULT_INLINE_SERVICE_VARIABLE_MESSAGE);

const inlineSecretValueSchema = acceptsNoVariableReferences(
  z.string().min(1).max(8192).regex(AGENT_VAULT_NO_CONTROL_CHARS_RE, AGENT_VAULT_NO_CONTROL_CHARS_MESSAGE)
);

export const AgentVaultInlineCustomHeaderInputSchema = AgentVaultCustomHeaderInputSchema.extend({
  value: inlineSecretValueSchema.describe(AGENT_VAULT.SESSION.inlineCustomHeaderValue)
});

export const AgentVaultInlineSubstitutionInputSchema = AgentVaultSubstitutionInputSchema.extend({
  value: inlineSecretValueSchema.describe(AGENT_VAULT.SESSION.inlineSubstitutionValue)
});

type TNamed = { name: string };
type TPlaceheld = { placeholder: string };

// Header names collide case-insensitively, placeholders exactly, matching how each is used. A duplicate
// would make "which stored row did the caller mean" unanswerable.
export const addDuplicateCustomHeaderNameIssues = (headers: TNamed[], ctx: z.RefinementCtx) => {
  const seen = new Set<string>();
  headers.forEach((header, index) => {
    const key = header.name.toLowerCase();
    if (seen.has(key)) {
      ctx.addIssue({
        code: z.ZodIssueCode.custom,
        message: `The custom header "${header.name}" is listed twice.`,
        path: [index, "name"]
      });
    }
    seen.add(key);
  });
};

export const addDuplicatePlaceholderIssues = (substitutions: TPlaceheld[], ctx: z.RefinementCtx) => {
  const seen = new Set<string>();
  substitutions.forEach((substitution, index) => {
    if (seen.has(substitution.placeholder)) {
      ctx.addIssue({
        code: z.ZodIssueCode.custom,
        message: `The placeholder "${substitution.placeholder}" is listed twice.`,
        path: [index, "placeholder"]
      });
    }
    seen.add(substitution.placeholder);
  });
};