Skip to content

Black Friday

Annual Pro is $90 for the first year, normally $120. Or start monthly at $6 for two months, then $12.

Ends Dec 4, 11:59 PM PT.

Create an account
← Public packages

@kentcdodds/github

GitHub REST, GraphQL, and pull request helpers for Kody agents.

FollowFork

Files

secret-resolution.ts

36 lines · 1.5 KB · TypeScript
/**
 * Kody fills secret placeholders in outgoing fetch URLs and bodies unless the
 * request sets `x-kody-secret-resolution`. The gateway strips that header
 * before the request reaches GitHub.
 *
 * GitHub auth here comes from the saved OAuth connection, so request bodies
 * never need placeholder filling. File-content writes default to
 * `headers-only` so fixture text that quotes the placeholder syntax is sent as
 * written instead of being refused (base64) or filled with a secret.
 */
export const SECRET_RESOLUTION_HEADER = "x-kody-secret-resolution"

const FILE_CONTENT_WRITES: Array<{ method: string; pattern: RegExp }> = [
  { method: "POST", pattern: /^\/repos\/[^/]+\/[^/]+\/git\/blobs\/?$/ },
  { method: "POST", pattern: /^\/repos\/[^/]+\/[^/]+\/git\/trees\/?$/ },
  { method: "PUT", pattern: /^\/repos\/[^/]+\/[^/]+\/contents\/.+$/ },
]

/**
 * Returns `"headers-only"` for GitHub calls that upload file content
 * (POST git/blobs, POST git/trees, PUT contents/{path}), otherwise undefined.
 */
export function defaultSecretResolution(method: string, path: string): "headers-only" | undefined {
  const upper = method.toUpperCase()
  let pathname = path
  try {
    pathname = new URL(path, "https://api.github.com").pathname
  } catch {
    pathname = path.split("?")[0] ?? path
  }
  if (!pathname.startsWith("/")) pathname = "/" + pathname
  for (const write of FILE_CONTENT_WRITES) {
    if (write.method === upper && write.pattern.test(pathname)) return "headers-only"
  }
  return undefined
}