Skip to content

feat(https): add enforceAuth option to callable functions#1870

Open
kyungseopk1m wants to merge 1 commit intofirebase:masterfrom
kyungseopk1m:fix/enforce-auth-callable-option
Open

feat(https): add enforceAuth option to callable functions#1870
kyungseopk1m wants to merge 1 commit intofirebase:masterfrom
kyungseopk1m:fix/enforce-auth-callable-option

Conversation

@kyungseopk1m
Copy link
Copy Markdown

Description

Fixes #1557

Adds an enforceAuth option to onCall callable functions, mirroring
the existing enforceAppCheck pattern.

Problem: onCall functions unconditionally reject requests with
invalid or expired auth tokens with a 401 error. There is no way to opt
out for public endpoints that don't require authentication.

Solution: Add enforceAuth?: boolean option. When false, requests
with invalid auth tokens log a warning and set context.auth to
undefined instead of throwing. Defaults to true to preserve
existing behavior.

Code sample

// Public endpoint — allow requests with invalid/expired tokens
export const publicFn = onCall({ enforceAuth: false }, (req) => {
  // req.auth is undefined if token was invalid or missing
  return { user: req.auth?.uid ?? "anonymous" };
});

// Default behavior (enforceAuth: true) — unchanged
export const privateFn = onCall((req) => {
  return { user: req.auth.uid };
});

Add enforceAuth option to onCall functions, mirroring the existing
enforceAppCheck pattern. When set to false, requests with invalid
auth tokens log a warning instead of throwing a 401 error.

Defaults to true to preserve existing behavior.

Fixes firebase#1557
Copy link
Copy Markdown
Contributor

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

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 introduces the enforceAuth option for Firebase HTTPS callable functions in both v1 and v2 SDKs. This feature allows developers to control whether invalid authentication tokens should automatically trigger a 401 Unauthorized response (the default behavior) or be allowed through with a warning, in which case context.auth is expected to be undefined. The changes include updates to configuration interfaces, the request handling logic in wrapOnCallHandler, and new test cases. One piece of feedback suggests explicitly setting context.auth = undefined when an invalid token is permitted to ensure consistency with the documentation and avoid potential issues with mock data in the emulator.

Comment on lines +826 to +830
} else {
logger.warn(
"Allowing request with invalid auth token because enforcement is disabled"
);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

To ensure that context.auth is strictly undefined when an invalid token is provided and enforcement is disabled (as stated in the documentation), it should be explicitly cleared. This is particularly important because the emulator hook (lines 803-812) might have already populated context.auth with mock data, which should not be used if an actual (but invalid) token was sent in the request.

        } else {
          logger.warn(
            "Allowing request with invalid auth token because enforcement is disabled"
          );
          context.auth = undefined;
        }

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

context.auth is already undefined in the INVALID path since checkAuthToken only sets it on success. This matches the existing enforceAppCheck behavior which also does not explicitly reset context.app.

@cabljac cabljac requested a review from IzaakGough April 16, 2026 14:52
@cabljac
Copy link
Copy Markdown
Contributor

cabljac commented Apr 16, 2026

Hi @kyungseopk1m thanks for your contribution!!

This as a feature is under some internal discussion right now, I'll keep you updated here on the outcome.

@kyungseopk1m kyungseopk1m force-pushed the fix/enforce-auth-callable-option branch 2 times, most recently from 27b147f to fe714b7 Compare April 17, 2026 11:33
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.

Don't verify auth token on public endpoints

2 participants