{"record":{"id":"4f226d5c1744202f","repo":"JuliusBrussee/caveman","slug":"githubapp-exactly-one-repository-is-required","errorCode":null,"errorMessage":"githubapp: exactly one repository is required","messagePattern":"githubapp: exactly one repository is required","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shared/platform/githubapp/githubapp.go","lineNumber":192,"sourceCode":"\tif err := json.Unmarshal(raw, &out); err != nil {\n\t\treturn out, fmt.Errorf(\"githubapp: decode installation: %w\", err)\n\t}\n\tif out.ID != installationID || out.Account.ID <= 0 || strings.TrimSpace(out.Account.Login) == \"\" {\n\t\treturn Installation{}, fmt.Errorf(\"githubapp: installation response identity mismatch\")\n\t}\n\treturn out, nil\n}\n\n// MintInstallationToken POSTs /app/installations/{id}/access_tokens narrowed to\n// `repos` and `perms`, returning a ~1h token. Defaults (perms nil) are\n// contents:write + pull_requests:write — enough to push a branch and open a draft\n// PR, never to merge.\nfunc (a *App) MintInstallationToken(ctx context.Context, installationID int64, repos []string, perms map[string]string) (InstallationToken, error) {\n\tif installationID <= 0 {\n\t\treturn InstallationToken{}, fmt.Errorf(\"githubapp: installation id must be positive\")\n\t}\n\tif len(repos) != 1 || strings.TrimSpace(repos[0]) == \"\" {\n\t\treturn InstallationToken{}, fmt.Errorf(\"githubapp: exactly one repository is required\")\n\t}\n\trepository := strings.TrimSpace(repos[0])\n\tif perms == nil {\n\t\tperms = map[string]string{\"contents\": \"write\", \"pull_requests\": \"write\"}\n\t}\n\tallowedPermissions := map[string]bool{\"contents\": true, \"pull_requests\": true}\n\tscopedPermissions := make(map[string]string, len(perms))\n\tfor name, level := range perms {\n\t\tif !allowedPermissions[name] || (level != \"read\" && level != \"write\") {\n\t\t\treturn InstallationToken{}, fmt.Errorf(\"githubapp: permission %q=%q exceeds the least-agency allowlist\", name, level)\n\t\t}\n\t\tscopedPermissions[name] = level\n\t}\n\tjwt, err := a.AppJWT()\n\tif err != nil {\n\t\treturn InstallationToken{}, err\n\t}\n\tbody := map[string]any{\"permissions\": scopedPermissions, \"repositories\": []string{repository}}","sourceCodeStart":174,"sourceCodeEnd":210,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/githubapp/githubapp.go#L174-L210","documentation":"The library enforces least-agency token scoping: each minted token must target exactly one repository with a non-empty name. Broad multi-repo or empty-scope tokens are rejected before any network request. This is a deliberate design constraint so a leaked token can only affect a single repo.","triggerScenarios":"App.MintInstallationToken called with repos nil, an empty slice, more than one entry, or a single entry that is empty/whitespace (e.g. [\"  \"]).","commonSituations":"Passing an empty repos slice when the target repo is only known later; batch jobs looping over many repos but calling once with all of them; repo name built from an empty config value.","solutions":["Call MintInstallationToken once per repository with a single-element slice","Validate the repo string (owner/name, non-blank) before calling","If the repo is resolved dynamically, fail fast when it is empty instead of passing a blank name"],"exampleFix":"// before\nrepos := []string{\"org/a\", \"org/b\"}\ntok, err := app.MintInstallationToken(ctx, id, repos, nil)\n// after\nfor _, r := range repos {\n\ttok, err := app.MintInstallationToken(ctx, id, []string{r}, nil)\n\t// use token, then RevokeToken\n}","handlingStrategy":"validation","validationCode":"func okRepos(repos []string) bool {\n\treturn len(repos) == 1 && strings.TrimSpace(repos[0]) != \"\"\n}\nif !okRepos(repos) { return fmt.Errorf(\"exactly one non-empty repo required\") }","typeGuard":null,"tryCatchPattern":"tok, err := app.MintInstallationToken(ctx, id, []string{repo}, nil)\nif err != nil {\n\tif strings.Contains(err.Error(), \"exactly one repository\") {\n\t\t// fix caller to loop one repo at a time\n\t}\n\treturn err\n}","preventionTips":["Mint one token per repository; never batch repos into one call","Validate the owner/name string is non-blank before calling","Resolve repo names from config at startup and fail on empties"],"tags":["github-app","input-validation","least-privilege"],"backgroundTag":"missing-required-argument","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}