Notes

Cloud Run Can Verify Firebase App Check Tokens Without the Admin SDK

Cloud Run, App Check

When deploying standalone containers on Cloud Run, you can verify incoming Firebase App Check JWTs inside Express/Fastify middleware by checking the token against Google's public JWKS (https://firebaseappcheck.googleapis.com/v1/jwks). Pin RS256 and the JWT type, and check that the issuer is https://firebaseappcheck.googleapis.com/ followed by your project number, not /v1, which rejects every genuine token.

This rejects requests without a valid token with HTTP 401 Unauthorized before your Node.js application ever instantiates a Gemini API request. The check still runs inside your billed Cloud Run instance, and App Check attests the app, not the user: Firebase says it "prevents some, but not all, abuse vectors", and a token can be replayed until it expires. Keep user authentication and per-user rate limits behind it.

appCheckMiddleware.ts
import type { FastifyReply, FastifyRequest } from "fastify";
import { createRemoteJWKSet, jwtVerify } from "jose";

const PROJECT_NUMBER = process.env.GCP_PROJECT_NUMBER;
if (!PROJECT_NUMBER) throw new Error("GCP_PROJECT_NUMBER is not set");
const JWKS = createRemoteJWKSet(new URL("https://firebaseappcheck.googleapis.com/v1/jwks"));

export async function verifyAppCheck(req: FastifyRequest, reply: FastifyReply) {
  const token = req.headers["x-firebase-appcheck"];
  if (typeof token !== "string") {
    return reply.status(401).send({ error: "Missing App Check token." });
  }
  try {
    await jwtVerify(token, JWKS, {
      algorithms: ["RS256"],
      typ: "JWT",
      issuer: `https://firebaseappcheck.googleapis.com/${PROJECT_NUMBER}`,
      audience: `projects/${PROJECT_NUMBER}`,
    });
  } catch {
    return reply.status(401).send({ error: "Invalid App Check verification." });
  }
}

All 7 notes