Skip to content

Fix Functions CJS/ESM runtime interop, migrate GitHub secret, and tighten rules/IAM - #508

Merged
jhuleatt merged 1 commit into
masterfrom
fix/functions-runtime-and-hygiene
Nov 19, 2026
Merged

jhuleatt merged 1 commit into
masterfrom
fix/functions-runtime-and-hygiene

Conversation

@jhuleatt

Copy link
Copy Markdown
Contributor

Follow-up maintenance and runtime fixes for Cloud Functions, Cloud Build, and Firestore rules:

  • Fix CJS/ESM runtime interop (functions/src/github.ts, functions/src/content.ts): Handle url-join@5's .default export under CommonJS require(), and update marked@18 initialization (const { marked } = require("marked") + marked.parse(data, { async: false })).
  • Migrate functions.config() to Secret Manager (functions/src/config.ts, functions/src/index.ts, functions/package.json, .gitignore): Replace deprecated functions.config() with defineSecret("GITHUB_TOKEN") on getProject and getAllProjects, initialize Config.github lazily so trigger discovery succeeds without secrets loaded, update local emulator/deploy scripts, and ignore .secret.local.
  • Restrict cron invokers & Firestore read scope (functions/src/index.ts, firestore.rules, static/cloudbuild.yaml): Scope HTTPS cron invokers on getAllProjects and cloudBuild to the project App Engine service account, scope firestore.rules reads explicitly to /configs, /content, and /releases, and align static/cloudbuild.yaml with npm --prefix=frontend run generate.

…hten rules/IAM

- Fix url-join@5 default export interop and marked@18 sync parse API in Functions
- Migrate github.token from deprecated functions.config() to Secret Manager (GITHUB_TOKEN) with lazy Config.github initialization
- Restrict getAllProjects and cloudBuild HTTPS invokers to fir-oss@appspot.gserviceaccount.com
- Scope firestore.rules public reads explicitly to /configs, /content, and /releases
- Update functions scripts/.gitignore and static/cloudbuild.yaml to use frontend generate

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the project's Firebase configuration, dependencies, and deployment scripts, notably migrating to Firebase Secrets for the GitHub token and refining Firestore security rules. However, several critical issues were identified in the review: calling the removed marked.setOptions will crash the application at runtime, using the invoker option in Cloud Functions v1 will cause TypeScript compilation errors, the restricted Firestore rules lack support for staging collections, and calling githubToken.value() outside of an active function context (such as during local testing) will throw an error.

Comment thread functions/src/content.ts
Comment on lines +18 to +19
const { marked } = require("marked");
marked.setOptions({ gfm: true, breaks: false });

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

In marked v12.0.0 and later (including the upgraded marked@18.0.5), marked.setOptions has been completely removed. Calling it will throw a TypeError at runtime, crashing the function on startup. Use marked.use() instead to configure global options.

Suggested change
const { marked } = require("marked");
marked.setOptions({ gfm: true, breaks: false });
const { marked } = require("marked");
marked.use({ gfm: true, breaks: false });

Comment thread functions/src/index.ts
Comment on lines +56 to +60
.runWith({
...RUNTIME_OPTS,
secrets: [githubToken],
invoker: ["fir-oss@appspot.gserviceaccount.com"],
})

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The invoker option is not supported in the RuntimeOptions of Cloud Functions v1 (firebase-functions/v1). Passing it to runWith will cause a TypeScript compilation error because invoker is a v2-only option. To restrict the invoker for a v1 HTTPS function, you must configure the IAM policy (e.g., via Cloud Console, gcloud CLI, or Terraform) rather than in the code, or migrate the function to v2.

  .runWith({
    ...RUNTIME_OPTS,
    secrets: [githubToken],
  })

Comment thread functions/src/index.ts
Comment on lines +84 to +87
.runWith({
...RUNTIME_OPTS,
invoker: ["fir-oss@appspot.gserviceaccount.com"],
})

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

Similar to getAllProjects, the invoker option is not supported in runWith for v1 functions and will cause a compilation error. Since this function does not require any secrets, you can revert to passing RUNTIME_OPTS directly.

  .runWith(RUNTIME_OPTS)

Comment thread firestore.rules
Comment on lines +3 to +5
match /configs/{document=**} { allow read: if true; }
match /content/{document=**} { allow read: if true; }
match /releases/{document=**} { allow read: if true; }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The new rules restrict reads to /configs, /content, and /releases. However, the application also supports a staging environment (Env.STAGING) which uses /configs-staging and /content-staging collections (as defined in shared/util.ts). Without explicit read rules for these staging collections, any staging deployment or frontend client attempting to read staging data will be blocked.

    match /configs/{document=**} { allow read: if true; }
    match /configs-staging/{document=**} { allow read: if true; }
    match /content/{document=**} { allow read: if true; }
    match /content-staging/{document=**} { allow read: if true; }
    match /releases/{document=**} { allow read: if true; }

Comment thread functions/src/config.ts
Comment on lines +72 to 74
if (key === "github.token") {
return process.env.github_token || githubToken.value();
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Calling githubToken.value() outside of an active Cloud Function execution context (for example, when running local unit tests or scripts via ts-node) will throw an error because the secret parameter is not bound. Wrapping this call in a try/catch block ensures that the application can fall back gracefully (e.g., to anonymous access or an empty token) instead of crashing.

    if (key === "github.token") {
      try {
        return process.env.github_token || githubToken.value();
      } catch (e) {
        return "";
      }
    }

@jhuleatt
jhuleatt merged commit 03fdda7 into master Nov 19, 2026
13 checks passed
@jhuleatt
jhuleatt deleted the fix/functions-runtime-and-hygiene branch November 19, 2026 19:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant