Skip to content

fix(apiv2): fall back to omitting x-goog-user-project on USER_PROJECT_DENIED - #11663

Open
joehan wants to merge 1 commit into
nextfrom
joehan/v16-quota-project-fallback
Open

joehan wants to merge 1 commit into
nextfrom
joehan/v16-quota-project-fallback

Conversation

@joehan

@joehan joehan commented Dec 5, 2026

Copy link
Copy Markdown
Member

Description

Follow-up to #11646.

When apiv2 automatically attaches x-goog-user-project from defaultQuotaProject, callers who lack serviceusage.services.use on that project receive a 403 PERMISSION_DENIED (USER_PROJECT_DENIED) from Service Infrastructure even when they would previously have succeeded using the CLI's default OAuth project quota.

This PR adds a targeted fallback in src/apiv2.ts:

  • Tracks whether a request's x-goog-user-project header came from defaultQuotaProject (leaving explicit GOOGLE_CLOUD_QUOTA_PROJECT env overrides and explicit caller headers untouched).
  • On a 403 whose response indicates USER_PROJECT_DENIED (via structured ErrorInfo.reason === "USER_PROJECT_DENIED" or a message mentioning serviceusage.services.use / USER_PROJECT_DENIED), records the project in a session-scoped deniedQuotaProjects set and immediately retries replayable requests once without x-goog-user-project.
  • Subsequent requests for that project in the same CLI invocation omit x-goog-user-project upfront without incurring another 403 round-trip.
  • Leaves unrelated 403 permission errors untouched.

Scenarios Tested

  • eslint --quiet and prettier --check on src/apiv2.ts and src/apiv2.spec.ts: passed
  • npx mocha src/apiv2.spec.ts --grep "defaultQuotaProject|quota|x-goog-user-project|USER_PROJECT_DENIED|serviceusage|unrelated 403": 15 passing

Sample Commands

N/A

@joehan joehan self-assigned this Dec 5, 2026

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

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.

Code Review

This pull request introduces a mechanism to automatically retry API requests without the x-goog-user-project header when a 403 USER_PROJECT_DENIED or serviceusage.services.use permission error is encountered, and subsequently omits the header for the rest of the session. It also adds comprehensive unit tests to verify this fallback behavior. The review feedback suggests extracting the inline magic strings "USER_PROJECT_DENIED" and "serviceusage.services.use" in isUserProjectDeniedError into top-level constants to align with the repository's style guide.

Comment thread src/apiv2.ts
Comment on lines +136 to 173
/**
* Returns true if an HTTP 403 response indicates the caller lacks
* `serviceusage.services.use` on the `x-goog-user-project` quota project.
*/
export function isUserProjectDeniedError(status: number, body: unknown): boolean {
if (status !== 403 || !body) {
return false;
}
if (typeof body === "string") {
return body.includes("serviceusage.services.use") || body.includes("USER_PROJECT_DENIED");
}
if (typeof body !== "object") {
return false;
}
const record = body as Record<string, unknown>;
const errObj =
record.error && typeof record.error === "object"
? (record.error as Record<string, unknown>)
: record;
if (Array.isArray(errObj.details)) {
for (const detail of errObj.details) {
if (
detail &&
typeof detail === "object" &&
(detail as Record<string, unknown>).reason === "USER_PROJECT_DENIED"
) {
return true;
}
}
}
if (typeof errObj.message === "string") {
return (
errObj.message.includes("serviceusage.services.use") ||
errObj.message.includes("USER_PROJECT_DENIED")
);
}
return false;
}

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

According to the repository style guide (GEMINI.md), static IDs, default timeouts, millisecond offsets, and repeated strings should be defined as top-level constants (UPPER_SNAKE_CASE) instead of inlining magic values inside functions.

Extracting "USER_PROJECT_DENIED" and "serviceusage.services.use" as top-level constants will improve maintainability and prevent duplication.

const USER_PROJECT_DENIED_REASON = "USER_PROJECT_DENIED";
const SERVICE_USAGE_PERMISSION = "serviceusage.services.use";

/**
 * Returns true if an HTTP 403 response indicates the caller lacks
 * `serviceusage.services.use` on the `x-goog-user-project` quota project.
 */
export function isUserProjectDeniedError(status: number, body: unknown): boolean {
  if (status !== 403 || !body) {
    return false;
  }
  if (typeof body === "string") {
    return body.includes(SERVICE_USAGE_PERMISSION) || body.includes(USER_PROJECT_DENIED_REASON);
  }
  if (typeof body !== "object") {
    return false;
  }
  const record = body as Record<string, unknown>;
  const errObj =
    record.error && typeof record.error === "object"
      ? (record.error as Record<string, unknown>)
      : record;
  if (Array.isArray(errObj.details)) {
    for (const detail of errObj.details) {
      if (
        detail &&
        typeof detail === "object" &&
        (detail as Record<string, unknown>).reason === USER_PROJECT_DENIED_REASON
      ) {
        return true;
      }
    }
  }
  if (typeof errObj.message === "string") {
    return (
      errObj.message.includes(SERVICE_USAGE_PERMISSION) ||
      errObj.message.includes(USER_PROJECT_DENIED_REASON)
    );
  }
  return false;
}
References
  1. Define static IDs, default timeouts, millisecond offsets, and repeated strings as top-level constants (UPPER_SNAKE_CASE) instead of inlining magic values inside functions. (link)

This branch has not been deployed

No deployments
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