{"record":{"id":"e0c9cdd7ad5aac48","repo":"JuliusBrussee/caveman","slug":"githubapp-sign-app-jwt-w","errorCode":null,"errorMessage":"githubapp: sign app jwt: %w","messagePattern":"githubapp: sign app jwt: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shared/platform/githubapp/githubapp.go","lineNumber":133,"sourceCode":"\theader := map[string]string{\"alg\": \"RS256\", \"typ\": \"JWT\"}\n\tclaims := map[string]any{\n\t\t\"iat\": now.Add(-60 * time.Second).Unix(),\n\t\t\"exp\": now.Add(10 * time.Minute).Unix(),\n\t\t\"iss\": a.appID,\n\t}\n\thb, err := json.Marshal(header)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tcb, err := json.Marshal(claims)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tsigningInput := base64.RawURLEncoding.EncodeToString(hb) + \".\" + base64.RawURLEncoding.EncodeToString(cb)\n\tdigest := sha256.Sum256([]byte(signingInput))\n\tsig, err := rsa.SignPKCS1v15(rand.Reader, a.privateKey, crypto.SHA256, digest[:])\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"githubapp: sign app jwt: %w\", err)\n\t}\n\treturn signingInput + \".\" + base64.RawURLEncoding.EncodeToString(sig), nil\n}\n\n// InstallationToken is the JIT, least-agency credential. It is scoped to the\n// single repo named and to the supplied permissions only, and is NEVER\n// persisted. The caller revokes it via RevokeToken at job end.\ntype InstallationToken struct {\n\tToken     string    `json:\"token\"`\n\tExpiresAt time.Time `json:\"expires_at\"`\n}\n\n// Installation is the App-authenticated identity GitHub assigns to an install.\n// Callers use it to reject callback-supplied ids that do not belong to this App.\ntype Installation struct {\n\tID      int64 `json:\"id\"`\n\tAccount struct {\n\t\tLogin string `json:\"login\"`","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/githubapp/githubapp.go#L115-L151","documentation":"AppJWT signs the GitHub App JWT with RS256 (rsa.SignPKCS1v15 over a SHA-256 digest of the signing input) and wraps any signing failure in this error. Signing failure means the App's private key could not produce a signature — almost always a malformed or unsupported key rather than a runtime condition.","triggerScenarios":"Calling AppJWT (directly or via GetInstallation, MintInstallationToken, GetRepoInstallation) when rsa.SignPKCS1v15 errors — typically because a.privateKey is not a valid RSA key (e.g. an EC key parsed into the wrong field) or rand.Reader fails.","commonSituations":"The configured PEM contained an EC or Ed25519 key instead of an RSA private key; the key was corrupted during base64/secret round-tripping; exotic crypto builds lacking PKCS#1 v1.5 support.","solutions":["Confirm the PEM is an RSA private key (openssl rsa -in key.pem -check); re-export from GitHub App settings if unsure","Verify parseRSAPrivateKey handles both PKCS#1 and PKCS#8 PEM blocks and that the secret wasn't mangled (line endings, HTML-escaped characters)","Check the error wrapped in %w for the exact crypto reason and fix accordingly","If rand.Reader failed, investigate OS entropy availability (rare; mostly containers with restricted /dev/urandom)"],"exampleFix":"// before: EC key passed where RSA required\nkeyPEM := loadECKey()\napp, _ := githubapp.New(githubapp.Config{AppID: id, PrivateKeyPEM: keyPEM})\n// after: ensure RSA key\nif !strings.Contains(string(keyPEM), \"RSA PRIVATE KEY\") && !isPKCS8RSA(keyPEM) {\n    return fmt.Errorf(\"github app key must be RSA\")\n}","handlingStrategy":"try-catch","validationCode":"if blk, _ := pem.Decode(keyPEM); blk == nil || !strings.Contains(blk.Type, \"PRIVATE KEY\") {\n    return errors.New(\"key must be a PEM RSA private key\")\n}\nif _, err := x509.ParsePKCS1PrivateKey(blk.Bytes); err != nil {\n    if _, err2 := x509.ParsePKCS8PrivateKey(blk.Bytes); err2 != nil {\n        return errors.New(\"key is not a valid RSA private key\")\n    }\n}","typeGuard":null,"tryCatchPattern":"jwt, err := app.AppJWT(ctx)\nif err != nil && strings.Contains(err.Error(), \"sign app jwt\") {\n    // key unusable for RS256: reload/re-export the RSA key, alert ops\n    return ErrAppKeyUnusable\n}","preventionTips":["Verify key type with openssl at deploy time (must be RSA)","Round-trip secrets as raw bytes, never through JSON/HTML escaping","Add a startup self-test that mints and parses one AppJWT"],"tags":["go","github-app","jwt","rsa-signing"],"backgroundTag":"jwt-token-expired","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"}