⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.
TypeScript honoissue-managementissue-trackerjira-alternativekanbanlinear-alternativemcpproject-managementreactself-hostedtypescript
fix(github): accept \n-escaped and base64-encoded GITHUB_PRIVATE_KEY
Andrej63c060bc74a3
diff --git a/apps/api/src/plugins/github/utils/github-app.ts b/apps/api/src/plugins/github/utils/github-app.tsindex ee86b734b..c8862a3af 100644--- a/apps/api/src/plugins/github/utils/github-app.ts+++ b/apps/api/src/plugins/github/utils/github-app.ts@@ -5,22 +5,45 @@ config();let githubAppInstance: App | null = null;+// Resolve the GitHub App private key from env in whichever form the host+// can carry. Orchestrators like Portainer's form-based stack editor strip+// real newlines from env values, so users end up with either an empty key+// or a single line containing literal "\n" sequences. Support three shapes:+//+// 1. GITHUB_PRIVATE_KEY = real multi-line PEM (the canonical form)+// 2. GITHUB_PRIVATE_KEY = single line with literal `\n` separators+// 3. GITHUB_PRIVATE_KEY_BASE64 = base64-encoded PEM (no newline issues)+//+// Returns "" when nothing is set; the caller's existence check handles that.+export function resolveGithubPrivateKey(): string {+ const base64 = process.env.GITHUB_PRIVATE_KEY_BASE64;+ if (base64 && base64.trim() !== "") {+ return Buffer.from(base64.trim(), "base64").toString("utf8");+ }+ const raw = process.env.GITHUB_PRIVATE_KEY ?? "";+ if (raw.includes("\\n") && !raw.includes("\n")) {+ return raw.replace(/\\n/g, "\n");+ }+ return raw;+}+export function getGithubApp(): App | null {if (githubAppInstance) {return githubAppInstance;}+ const privateKey = resolveGithubPrivateKey();if (!process.env.GITHUB_WEBHOOK_SECRET ||!process.env.GITHUB_APP_ID ||- !process.env.GITHUB_PRIVATE_KEY+ !privateKey) {return null;}githubAppInstance = new App({appId: process.env.GITHUB_APP_ID ?? "",- privateKey: process.env.GITHUB_PRIVATE_KEY ?? "",+ privateKey,webhooks: {secret: process.env.GITHUB_WEBHOOK_SECRET ?? "",},diff --git a/apps/docs/core/installation/environment-variables.mdx b/apps/docs/core/installation/environment-variables.mdxindex cad771f08..4e0d39993 100644--- a/apps/docs/core/installation/environment-variables.mdx+++ b/apps/docs/core/installation/environment-variables.mdx@@ -103,7 +103,8 @@ Name | Description |--- | --- || `GITHUB_APP_ID` | The GitHub App ID.| `GITHUB_WEBHOOK_SECRET` | Secret used to verify GitHub webhook payloads.-| `GITHUB_PRIVATE_KEY` | PEM private key for the GitHub App (JWT authentication).+| `GITHUB_PRIVATE_KEY` | PEM private key for the GitHub App (JWT authentication). Accepts a real multi-line PEM, or a single line where every newline is replaced with the literal characters `\n` (useful when the host's env editor strips newlines, e.g. Portainer's form-based stack editor).+| `GITHUB_PRIVATE_KEY_BASE64` | (Optional) Alternative to `GITHUB_PRIVATE_KEY`: a base64-encoded PEM. Takes precedence when both are set. Convenient when your orchestrator can't carry newlines at all.| `GITHUB_APP_NAME` | (Optional) GitHub App slug, used for installation links in the UI.### Google SSOdiff --git a/tests/api/plugins/github/utils/resolve-private-key.test.ts b/tests/api/plugins/github/utils/resolve-private-key.test.tsnew file mode 100644index 000000000..813d52079--- /dev/null+++ b/tests/api/plugins/github/utils/resolve-private-key.test.ts@@ -0,0 +1,73 @@+import { afterEach, beforeEach, describe, expect, it } from "vitest";+import { resolveGithubPrivateKey } from "../../../../../apps/api/src/plugins/github/utils/github-app";++const PEM = [+ "-----BEGIN RSA PRIVATE KEY-----",+ "MIIEpAIBAAKCAQEA",+ "abc",+ "-----END RSA PRIVATE KEY-----",+].join("\n");++describe("resolveGithubPrivateKey", () => {+ const originalKey = process.env.GITHUB_PRIVATE_KEY;+ const originalB64 = process.env.GITHUB_PRIVATE_KEY_BASE64;++ beforeEach(() => {+ delete process.env.GITHUB_PRIVATE_KEY;+ delete process.env.GITHUB_PRIVATE_KEY_BASE64;+ });++ afterEach(() => {+ if (originalKey === undefined) {+ delete process.env.GITHUB_PRIVATE_KEY;+ } else {+ process.env.GITHUB_PRIVATE_KEY = originalKey;+ }+ if (originalB64 === undefined) {+ delete process.env.GITHUB_PRIVATE_KEY_BASE64;+ } else {+ process.env.GITHUB_PRIVATE_KEY_BASE64 = originalB64;+ }+ });++ it("returns an empty string when nothing is set", () => {+ expect(resolveGithubPrivateKey()).toBe("");+ });++ it("returns the raw value when GITHUB_PRIVATE_KEY is real multi-line PEM", () => {+ process.env.GITHUB_PRIVATE_KEY = PEM;+ expect(resolveGithubPrivateKey()).toBe(PEM);+ });++ it("unescapes literal \\n separators when the env contains no real newlines", () => {+ // Simulates the Portainer form-field case: real newlines stripped,+ // user replaced them with literal "\n".+ process.env.GITHUB_PRIVATE_KEY = PEM.replace(/\n/g, "\\n");+ expect(resolveGithubPrivateKey()).toBe(PEM);+ });++ it("does not touch values that already have real newlines", () => {+ // Mixed content (real newlines + literal \n inside data) must not be+ // mangled. Real newlines win; \\n stays as-is.+ const mixed = "line1\nliteral\\nstays\nline3";+ process.env.GITHUB_PRIVATE_KEY = mixed;+ expect(resolveGithubPrivateKey()).toBe(mixed);+ });++ it("decodes GITHUB_PRIVATE_KEY_BASE64 when set", () => {+ process.env.GITHUB_PRIVATE_KEY_BASE64 = Buffer.from(PEM).toString("base64");+ expect(resolveGithubPrivateKey()).toBe(PEM);+ });++ it("prefers GITHUB_PRIVATE_KEY_BASE64 over GITHUB_PRIVATE_KEY when both are set", () => {+ process.env.GITHUB_PRIVATE_KEY = "stale-value";+ process.env.GITHUB_PRIVATE_KEY_BASE64 = Buffer.from(PEM).toString("base64");+ expect(resolveGithubPrivateKey()).toBe(PEM);+ });++ it("falls back to GITHUB_PRIVATE_KEY when the base64 env is empty/whitespace", () => {+ process.env.GITHUB_PRIVATE_KEY_BASE64 = " ";+ process.env.GITHUB_PRIVATE_KEY = PEM;+ expect(resolveGithubPrivateKey()).toBe(PEM);+ });+});