Skip to content

Patch that fixes emulated v1 callable functions that are in a monorepo project #1345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- Fixes an emulator issue where snapshot.ref could not point to multiple databases (#1339).
- Fixes emulated v1 callable functions that use a monorepo setup (#1345).
29 changes: 29 additions & 0 deletions src/common/providers/https.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ import { TaskContext } from "./tasks";

const JWT_REGEX = /^[a-zA-Z0-9\-_=]+?\.[a-zA-Z0-9\-_=]+?\.([a-zA-Z0-9\-_=]+)?$/;

const CALLABLE_AUTH_HEADER = "x-callable-context-auth";
const ORIGINAL_AUTH_HEADER = "x-original-auth";

/** An express request with the wire format representation of the request body. */
export interface Request extends express.Request {
/** The wire format representation of the request body. */
Expand Down Expand Up @@ -661,6 +664,32 @@ function wrapOnCallHandler<Req = any, Res = any>(
}

const context: CallableContext = { rawRequest: req };

// TODO(colerogers): yank this when we release a breaking change of the CLI that removes
// our monkey-patching code referenced below and increases the minimum supported SDK version.
//
// Note: This code is needed to fix v1 callable functions in the emulator with a monorepo setup.
// The original monkey-patched code lived in the functionsEmulatorRuntime
// (link: https://github.com/firebase/firebase-tools/blob/accea7abda3cc9fa6bb91368e4895faf95281c60/src/emulator/functionsEmulatorRuntime.ts#L480)
// and was not compatible with how monorepos separate out packages (see https://github.com/firebase/firebase-tools/issues/5210).
if (isDebugFeatureEnabled("skipTokenVerification") && handler.length === 2) {
const authContext = context.rawRequest.header(CALLABLE_AUTH_HEADER);
if (authContext) {
logger.debug("Callable functions auth override", {
key: CALLABLE_AUTH_HEADER,
value: authContext,
});
context.auth = JSON.parse(decodeURIComponent(authContext));
delete context.rawRequest.headers[CALLABLE_AUTH_HEADER];
}

const originalAuth = context.rawRequest.header(ORIGINAL_AUTH_HEADER);
if (originalAuth) {
context.rawRequest.headers["authorization"] = originalAuth;
delete context.rawRequest.headers[ORIGINAL_AUTH_HEADER];
}
}

const tokenStatus = await checkTokens(req, context);
if (tokenStatus.auth === "INVALID") {
throw new HttpsError("unauthenticated", "Unauthenticated");
Expand Down