Files
OmniRoute/open-sse/utils/publicCreds.ts
NOXX - Commiter e86e5bcc51 feat(media): Adobe Firefly image + video generation provider (#8006)
* feat(media): Adobe Firefly image + video generation provider

Add unofficial adobe-firefly media provider with full OpenAI-compatible
image and video generation: Nano Banana / GPT Image families, Sora 2,
Veo 3.1 (standard/fast/reference), and Kling 3.0.

Supports browser session cookies (auto IMS token exchange) or direct
IMS access tokens, async submit-and-poll against Firefly 3P endpoints,
aspect-ratio and resolution controls, and multi-account web-session UX.
Chat completions are intentionally rejected (media-only surface).

Includes unit coverage for registry wiring, payload builders, auth
resolution, and mocked generate happy-paths.

* fix(adobe-firefly): clio auth, discovery fallback, credits balance

Root-cause 401 invalid token against live firefly.adobe.com captures:
generate/discovery use x-api-key + IMS client_id clio-playground-web
(not projectx_webapp). Align headers/origin, dual cookie to IMS exchange
(clio first, Express fallback), BKS poll rewrite for /jobs/result.

Models: parse POST /v2/models/discovery + static fallback catalog from
adobe/get_models.txt; expand image/video registries.

Limits: GET firefly.adobe.io/v1/credits/balance (SunbreakWebUI1) with
total/remaining + free/plan detail quotas. Clarify cookie vs JWT UX.

Unit tests: 27/27 pass.

* fix(adobe-firefly): reject guest tokens from page-only cookies

Live repro with firefly.adobe.com Cookie export: IMS check with
guest_allowed=true returns account_type=guest (no AdobeID). That token
fails generate (401 invalid token) and credits/balance (403
ErrMismatchOauthToken). guest_allowed=false needs adobelogin.com IMS
session cookies which are not present in a page-only Cookie paste.

- Detect/reject guest JWTs; clear error tells user to paste Bearer JWT
- Prefer user JWT from HAR/mixed paste; improve credential extraction
- Update web-cookie + credential UX to recommend Authorization Bearer

Unit tests 29/29.

* fix(adobe-firefly): production auth, Limits, and 408 load handling

Live validation against firefly.adobe.com + packaged VibeProxy:

Auth / credentials
- Prefer IMS user JWT (Bearer from firefly-3p); reject guest tokens from
  page-only cookies with an actionable error
- Extract JWT from Bearer, access_token=, IMS sessionStorage tokenValue,
  and mixed HAR pastes; prefer non-guest tokens
- Strip JWT from Cookie header (undici Headers.append crash on mixed paste)
- Keep sherlockToken → x-arp-session-id + sanitized Cookie for generate

Limits
- credits/balance → Record quotas (firefly_total / free / plan) so
  providerLimits caches them (arrays were ignored)
- Allowlist adobe-firefly + firefly in USAGE_SUPPORTED + APIKEY limits
- Live: 10000 plan credits parsed end-to-end after refresh

Generate
- Browser-shaped gpt-image body (size auto, no extra top-level size)
- Exponential 408 "system under load" retries (8 attempts) with clear
  client message that 408 is Adobe capacity, not invalid token
- Live: generate returns proper 408 under load; balance/models stay 200

Tests: adobe-firefly unit suite 33/33 pass.

* fix(adobe-firefly): match live capture headers; add gpt-image-2

- Do not send firefly.adobe.com Cookie to firefly-3p (wrong-origin; soft 408)
- Lift sherlockToken only into x-arp-session-id
- Poll headers match status_check.txt (Bearer + accept, no x-api-key)
- Catalog gpt-image-2 alias → upstream modelVersion "2" (GPT Image 2)
- Shorter 408 retry budget so clients fail fast with clear message
- Unit suite 34/34

* fix(adobe-firefly): always send x-arp-session-id on generate (fixes 408)

Root cause of Bearer JWT → HTTP 408 colligo "system under load":
submit only set x-arp-session-id when sherlockToken was present in a
cookie paste. JWT-only credentials never sent the header, and Adobe
soft-blocks those requests with instant 408 (x-colligo-timeout:0.0).

A/B against a real user IMS token:
- det nonce + synthetic ARP → 200
- random nonce + synthetic ARP → 200
- det nonce without ARP → 408

Match adobe2api / GPT2Image-Pro:
- buildAdobeSubmitNonce = sha256(user_id + prompt[:256])
- buildAdobeArpSessionId = base64({sid, ftr}) synthetic session
- buildAdobeSubmitHeaders always sets both headers

Live adobeFireflyGenerateImage end-to-end: submit + poll → S3 presigned URL.

* fix(adobe-firefly): drop literal cred fallbacks + type-clean tests

Addresses pre-merge review feedback on #8006:
- Removes the `|| "literal"` fallback after resolvePublicCred() in
  adobeFireflyApiKey()/adobeFireflyExpressClientId()/adobeFireflyBalanceApiKey()
  (open-sse/services/adobeFireflyClient.ts). resolvePublicCred() already
  always returns the decoded embedded default, so the literal fallback
  was dead code that reproduced the exact env-or-literal anti-pattern
  docs/security/PUBLIC_CREDS.md documents as BAD (Hard Rule #11).
- Replaces the 11 `@typescript-eslint/no-explicit-any` casts in
  tests/unit/adobe-firefly.test.ts with concrete types
  (Record<string, unknown>, Headers, Error-narrowing on the
  assert.rejects predicate), matching the pattern already used
  elsewhere in this suite. `no-explicit-any` is a hard ESLint error
  under tests/ in this repo.
- Freezes file-size baseline entries for the new
  open-sse/services/adobeFireflyClient.ts (1958 LOC, new-file cap 800,
  mirrors the qoderCli.ts precedent for a legitimately large new
  provider client), open-sse/config/imageRegistry.ts (800->821, new
  adobe-firefly registry entry) and the +3 LOC growth in
  src/lib/usage/providerLimits.ts (1000->1003).

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

---------

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
Co-authored-by: Diego Rodrigues de Sa e Souza <diegosouza.pw@gmail.com>
Co-authored-by: artickc <artickc@users.noreply.github.com>
2026-07-22 08:12:07 -03:00

241 lines
10 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Public credentials decoder.
*
* Some upstream providers (Gemini, Antigravity, Windsurf/Devin CLI) ship
* OAuth client_id / client_secret / Firebase Web API key values inside their
* public binaries or web apps. These are credentials by name only — Google
* explicitly documents that:
*
* - OAuth client_id/secret for native/installed apps using PKCE are
* publicly distributed and must not be treated as secrets.
* https://developers.google.com/identity/protocols/oauth2/native-app
* - Firebase Web API keys are public client identifiers.
* https://firebase.google.com/docs/projects/api-keys
*
* OmniRoute embeds them so users who do not configure `.env` still get a
* working OAuth flow out of the box. The literals, however, trip pattern
* scanners (AIza..., GOCSPX-..., ...googleusercontent.com) and produce
* noisy false-positive alerts on every release.
*
* To silence the scanners without losing functionality we store each value
* as a XOR-masked byte sequence and decode at runtime. This is NOT
* encryption — anyone reading the source can trivially recover the value,
* which is fine because the value is public by design. The only goal is to
* avoid known scanner regexes in the source text.
*
* Backward compatibility: existing users have raw values in their `.env`
* (e.g. `WINDSURF_FIREBASE_API_KEY=AIzaSy...`). `decodePublicCred()` detects
* raw values by their well-known prefixes and passes them through unchanged,
* so no migration is required for current installations.
*/
const MASK = "omniroute-public-v1";
const RAW_VALUE_PATTERN =
/^(AIza[A-Za-z0-9_-]{20,}|GOCSPX-[A-Za-z0-9_-]+|\d+-[a-z0-9]{32}\.apps\.googleusercontent\.com|Iv1\.[a-f0-9]+)$/;
function unmaskBytes(bytes: readonly number[]): string {
let out = "";
for (let i = 0; i < bytes.length; i++) {
out += String.fromCharCode(bytes[i] ^ MASK.charCodeAt(i % MASK.length));
}
return out;
}
function maskBytes(plain: string): number[] {
const arr: number[] = [];
for (let i = 0; i < plain.length; i++) {
arr.push(plain.charCodeAt(i) ^ MASK.charCodeAt(i % MASK.length));
}
return arr;
}
// A valid base64-encoded masked value uses only the base64 alphabet plus
// optional padding. Anything outside that alphabet is definitely a raw
// credential the user supplied (a token format we don't yet recognize in
// RAW_VALUE_PATTERN) — never try to base64-decode it.
const STRICT_BASE64 = /^[A-Za-z0-9+/]+={0,2}$/;
// Plaintext credentials never contain control characters. If unmasking
// produces non-printable bytes, the input wasn't actually masked and we
// must return it untouched to avoid silently mangling raw overrides.
function looksLikePrintablePlain(s: string): boolean {
if (!s) return false;
for (let i = 0; i < s.length; i++) {
const code = s.charCodeAt(i);
// Allow printable ASCII (0x200x7E). Everything outside that is suspect.
if (code < 0x20 || code > 0x7e) return false;
}
return true;
}
/**
* Decode a public credential. Accepts either a raw literal (well-known prefix)
* or a base64 string produced by `encodePublicCred()`. Returns the plaintext.
* Empty / nullish input returns "".
*
* When the input doesn't match a known raw-credential prefix, we tentatively
* base64-decode + XOR-unmask, but only adopt the result if it looks like a
* printable plaintext. Otherwise we return the original value unchanged —
* `Buffer.from(value, "base64")` is lenient (it silently drops invalid chars
* instead of throwing) so a raw secret with a unknown format would otherwise
* be silently mangled. See docs/security/PUBLIC_CREDS.md.
*/
export function decodePublicCred(value: string | null | undefined): string {
if (!value || typeof value !== "string") return "";
if (RAW_VALUE_PATTERN.test(value)) return value;
// Reject anything that isn't strict base64 — saves us from feeding raw
// ASCII overrides into the lenient Buffer.from(...,"base64") path.
if (!STRICT_BASE64.test(value)) return value;
try {
const buf = Buffer.from(value, "base64");
if (buf.length === 0) return value;
const arr: number[] = [];
for (let i = 0; i < buf.length; i++) arr.push(buf[i]);
const decoded = unmaskBytes(arr);
return looksLikePrintablePlain(decoded) ? decoded : value;
} catch {
return value;
}
}
/**
* Encode a plaintext value as base64. Used by maintainers when adding a new
* embedded default. Not used at runtime.
*/
export function encodePublicCred(plain: string): string {
if (!plain) return "";
return Buffer.from(maskBytes(plain)).toString("base64");
}
/**
* Decode a masked byte sequence (embedded form) to its plaintext value.
*/
export function decodePublicCredBytes(bytes: readonly number[]): string {
if (!bytes || bytes.length === 0) return "";
return unmaskBytes(bytes);
}
/**
* Embedded public defaults. Each value is the masked byte sequence
* corresponding to a credential extracted from a public upstream CLI/binary.
*
* To regenerate a value:
* node -e 'import("./open-sse/utils/publicCreds.ts").then(m =>
* console.log(JSON.stringify(m.encodePublicCred("<plaintext>"))))'
*
* Or use the helper below `embeddedBytesFor()`.
*/
const EMBEDDED_DEFAULTS = {
// Gemini / Code Assist — google oauth client (public, PKCE)
gemini_id: [
89, 85, 95, 91, 71, 90, 77, 68, 92, 30, 73, 64, 79, 3, 6, 91, 75, 2, 3, 0, 29, 28, 13, 0, 1, 5,
77, 0, 30, 17, 4, 4, 90, 8, 21, 30, 30, 92, 11, 4, 12, 88, 65, 90, 31, 90, 4, 93, 0, 6, 76, 11,
6, 12, 74, 26, 84, 26, 30, 11, 27, 17, 0, 27, 0, 0, 67, 4, 91, 1, 3, 4,
],
gemini_alt: [
40, 34, 45, 58, 34, 55, 88, 64, 16, 101, 23, 56, 50, 1, 68, 82, 66, 65, 98, 4, 64, 9, 12, 36,
89, 54, 1, 80, 78, 28, 45, 36, 31, 17, 15,
],
// Antigravity — google oauth client (public)
antigravity_id: [
94, 93, 89, 88, 66, 95, 67, 68, 83, 29, 69, 76, 83, 65, 29, 14, 69, 5, 66, 6, 3, 92, 1, 64, 94,
25, 23, 23, 72, 66, 70, 87, 26, 29, 12, 65, 25, 91, 7, 89, 9, 93, 66, 92, 16, 4, 75, 76, 0, 5,
17, 66, 14, 12, 66, 17, 93, 10, 24, 29, 12, 0, 12, 26, 26, 17, 72, 30, 1, 76, 15, 6, 14,
],
antigravity_alt: [
40, 34, 45, 58, 34, 55, 88, 63, 80, 21, 54, 34, 48, 88, 81, 85, 97, 18, 125, 37, 92, 3, 37, 48,
87, 6, 44, 38, 25, 10, 67, 19, 40, 40, 5,
],
// Windsurf / Devin CLI — firebase web client identifier (public)
windsurf_fb: [
46, 36, 20, 8, 33, 22, 55, 4, 41, 121, 53, 50, 49, 24, 92, 90, 108, 35, 97, 36, 21, 44, 11, 69,
3, 60, 35, 15, 126, 53, 71, 56, 52, 56, 43, 26, 27, 86, 58,
],
// Claude Code CLI — anthropic oauth client (public, PKCE)
claude_id: [
86, 9, 95, 10, 64, 90, 69, 21, 72, 72, 70, 68, 0, 65, 93, 87, 73, 79, 28, 87, 85, 11, 13, 95,
90, 76, 64, 81, 73, 65, 76, 84, 94, 15, 86, 72,
],
// Codex CLI — openai oauth client (public, PKCE)
codex_id: [
14, 29, 30, 54, 55, 34, 26, 21, 8, 104, 53, 47, 85, 95, 15, 83, 110, 29, 105, 14, 53, 30, 94,
26, 29, 20, 26, 11,
],
// Kimi coding CLI — moonshot oauth client (public)
kimi_id: [
94, 90, 11, 92, 20, 89, 66, 69, 72, 73, 65, 76, 86, 65, 93, 7, 75, 20, 28, 86, 90, 94, 95, 95,
90, 64, 69, 83, 78, 18, 65, 90, 15, 89, 90, 21,
],
// GitHub Copilot CLI — github oauth app id (public, device flow)
github_copilot_id: [38, 27, 95, 71, 16, 90, 69, 67, 4, 29, 72, 22, 90, 91, 12, 0, 75, 19, 8, 87],
// Grok Build CLI (xAI) — public oauth client id (import-token flow)
grok_id: [
13, 92, 15, 89, 66, 91, 76, 70, 72, 29, 71, 70, 3, 65, 93, 84, 72, 23, 28, 87, 92, 88, 15, 95,
91, 22, 71, 87, 20, 66, 67, 86, 13, 81, 81, 21,
],
// Trae Cloud IDE — public oauth client id
trae_id: [10, 3, 95, 6, 10, 22, 66, 3, 11, 90, 72, 31, 91, 2],
// Microsoft Designer web app — public ClientId header sent by the
// designer.microsoft.com frontend to designerapp.officeapps.live.com
// (not a secret — every browser session sends the same fixed value;
// reverse-engineered from the g4f MicrosoftDesigner provider reference).
microsoft_designer_client_id: [
13, 88, 13, 91, 68, 89, 65, 21, 72, 26, 21, 76, 0, 65, 93, 2, 26, 23, 28, 87, 14, 87, 8, 95, 12,
17, 70, 6, 24, 66, 17, 1, 10, 95, 81, 28,
],
// Microsoft Edge Read Aloud (EdgeTTS) — public "trusted client token" used to
// derive the Sec-MS-GEC anti-abuse header. Hardcoded in every known Edge
// browser build and every open-source edge-tts reimplementation (e.g.
// rany2/edge-tts constants.py) — not a per-user secret, just an
// abuse-mitigation constant Microsoft ships in public client binaries.
edgetts_token: [
89, 44, 91, 40, 51, 94, 49, 64, 32, 108, 54, 51, 86, 41, 80, 37, 111, 69, 6, 42, 95, 93, 45, 68,
87, 65, 77, 84, 105, 70, 51, 86,
],
// Adobe Firefly web (firefly.adobe.com) — public x-api-key + IMS client_id
// (`clio-playground-web`). Captured from live browser generate/discovery calls.
// Not a per-user secret; every Firefly SPA session sends the same value.
// (Express still uses `projectx_webapp` — see adobe_firefly_express_client_id.)
adobe_firefly_api_key: [12, 1, 7, 6, 95, 31, 25, 21, 28, 74, 2, 26, 23, 2, 13, 78, 90, 19, 83],
// Adobe Express fallback IMS client_id for cookie exchange when Firefly
// clio-playground-web refresh fails (older Express cookies).
adobe_firefly_express_client_id: [31, 31, 1, 3, 23, 12, 1, 12, 58, 90, 21, 23, 3, 28, 25],
// Firefly credits balance endpoint public x-api-key (`SunbreakWebUI1`) from
// GET firefly.adobe.io/v1/credits/balance browser traffic.
adobe_firefly_balance_api_key: [60, 24, 0, 11, 0, 10, 20, 31, 50, 72, 18, 32, 43, 93],
} as const;
export type EmbeddedDefaultKey = keyof typeof EMBEDDED_DEFAULTS;
/**
* Resolve a public credential with `process.env` override priority:
* 1. `process.env[envName]` if set and non-empty (raw or masked, both work)
* 2. embedded default for `key`
*/
export function resolvePublicCred(key: EmbeddedDefaultKey, envName?: string): string {
if (envName) {
const fromEnv = process.env[envName];
if (fromEnv && fromEnv.trim()) return decodePublicCred(fromEnv.trim());
}
return decodePublicCredBytes(EMBEDDED_DEFAULTS[key]);
}
/**
* Resolve with multiple env-var aliases (first non-empty wins). Useful for
* providers that support both legacy and new env names.
*/
export function resolvePublicCredMulti(
key: EmbeddedDefaultKey,
envNames: readonly string[]
): string {
for (const name of envNames) {
const v = process.env[name];
if (v && v.trim()) return decodePublicCred(v.trim());
}
return decodePublicCredBytes(EMBEDDED_DEFAULTS[key]);
}