Repository navigation
Fix Functions CJS/ESM runtime interop, migrate GitHub secret, and tighten rules/IAM - #508
Conversation
…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
There was a problem hiding this comment.
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.
| const { marked } = require("marked"); | ||
| marked.setOptions({ gfm: true, breaks: false }); |
There was a problem hiding this comment.
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.
| const { marked } = require("marked"); | |
| marked.setOptions({ gfm: true, breaks: false }); | |
| const { marked } = require("marked"); | |
| marked.use({ gfm: true, breaks: false }); |
| .runWith({ | ||
| ...RUNTIME_OPTS, | ||
| secrets: [githubToken], | ||
| invoker: ["fir-oss@appspot.gserviceaccount.com"], | ||
| }) |
There was a problem hiding this comment.
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],
})| .runWith({ | ||
| ...RUNTIME_OPTS, | ||
| invoker: ["fir-oss@appspot.gserviceaccount.com"], | ||
| }) |
| match /configs/{document=**} { allow read: if true; } | ||
| match /content/{document=**} { allow read: if true; } | ||
| match /releases/{document=**} { allow read: if true; } |
There was a problem hiding this comment.
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; }
| if (key === "github.token") { | ||
| return process.env.github_token || githubToken.value(); | ||
| } |
There was a problem hiding this comment.
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 "";
}
}
Follow-up maintenance and runtime fixes for Cloud Functions, Cloud Build, and Firestore rules:
functions/src/github.ts,functions/src/content.ts): Handleurl-join@5's.defaultexport under CommonJSrequire(), and updatemarked@18initialization (const { marked } = require("marked")+marked.parse(data, { async: false })).functions.config()to Secret Manager (functions/src/config.ts,functions/src/index.ts,functions/package.json,.gitignore): Replace deprecatedfunctions.config()withdefineSecret("GITHUB_TOKEN")ongetProjectandgetAllProjects, initializeConfig.githublazily so trigger discovery succeeds without secrets loaded, update local emulator/deploy scripts, and ignore.secret.local.functions/src/index.ts,firestore.rules,static/cloudbuild.yaml): Scope HTTPS cron invokers ongetAllProjectsandcloudBuildto the project App Engine service account, scopefirestore.rulesreads explicitly to/configs,/content, and/releases, and alignstatic/cloudbuild.yamlwithnpm --prefix=frontend run generate.