Skip to content
Closed
74 changes: 74 additions & 0 deletions apps/server/src/provider/AntigravityAuth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,73 @@ it.layer(NodeServices.layer)("AntigravityAuth", (it) => {
}),
);

it.effect("shows Antigravity's onboarding rejection instead of the generic failure", () =>
Effect.gen(function* () {
const harness = yield* makeHarness();
yield* harness.auth.controller.start(owner);
yield* phase(harness.auth, "waiting");
const reason =
"Onboarding failed: User is ineligible for free-tier. Reason: Your current account is not eligible for Antigravity. Try signing in with another personal Google account.";
yield* Deferred.fail(
harness.authenticated,
new AcpErrors.AcpRequestError({
code: -32603,
errorMessage: reason,
method: "authenticate",
}),
);
const failed = yield* phase(harness.auth, "failed");
assert.equal(failed.message, reason);
const other = yield* phase(harness.auth, "failed", otherOwner);
assert.equal(other.message, "Sign-in failed in another client.");
yield* Deferred.await(harness.closed);
}),
);

it.effect("keeps the generic failure when an authenticate error could leak a URL", () =>
Effect.gen(function* () {
const harness = yield* makeHarness();
yield* harness.auth.controller.start(owner);
yield* phase(harness.auth, "waiting");
yield* Deferred.fail(
harness.authenticated,
new AcpErrors.AcpRequestError({
code: -32603,
errorMessage: `Onboarding failed ${callbackUrl}`,
method: "authenticate",
}),
);
const failed = yield* phase(harness.auth, "failed");
assert.equal(failed.message, "Google sign-in failed. Start sign-in again.");
yield* Deferred.await(harness.closed);
}),
);

it.effect.each([
["a percent-encoded callback", "callback=http%3A%2F%2F127.0.0.1%3A51234%2F%3Fcode%3Dtest-code"],
["an email address", "Account alice@example.com is not eligible"],
["a key=value fragment", "reason=invalid"],
["only digits and punctuation", "555-123-4567"],
["a scheme without slashes", "Onboarding failed: See https:example.com for help."],
["a bare hostname", "Onboarding failed. Visit support.google.com for help."],
["a hyphenated credential", "Invalid token sk-123456789012345678901234567890"],
] as const)(
"keeps the generic failure when an authenticate error contains %s",
([, errorMessage]) =>
Effect.gen(function* () {
const harness = yield* makeHarness();
yield* harness.auth.controller.start(owner);
yield* phase(harness.auth, "waiting");
yield* Deferred.fail(
harness.authenticated,
new AcpErrors.AcpRequestError({ code: -32603, errorMessage, method: "authenticate" }),
);
const failed = yield* phase(harness.auth, "failed");
assert.equal(failed.message, "Google sign-in failed. Start sign-in again.");
yield* Deferred.await(harness.closed);
}),
);

it.effect("does not call callback HTTP success a successful Google sign-in", () =>
Effect.gen(function* () {
const harness = yield* makeHarness();
Expand Down Expand Up @@ -297,9 +364,16 @@ it.layer(NodeServices.layer)("AntigravityAuth", (it) => {
.complete(owner, { flowId: state.flowId!, callbackUrl: invalidUrl })
.pipe(Effect.exit);
assert.isTrue(Exit.isFailure(result));
assert.equal(
(yield* phase(harness.auth, "waiting")).inputError,
"This redirect URL does not belong to the current sign-in.",
);
assert.isNull((yield* phase(harness.auth, "waiting", otherOwner)).inputError);
}
assert.equal(harness.forwarded(), 0);
assert.equal((yield* phase(harness.auth, "waiting")).authorizationUrl, authorizationUrl);
yield* harness.auth.controller.complete(owner, { flowId: state.flowId!, callbackUrl });
assert.isNull((yield* phase(harness.auth, "verifying")).inputError);
yield* harness.auth.controller.cancel(owner, state.flowId!);
}),
);
Expand Down
49 changes: 48 additions & 1 deletion apps/server/src/provider/AntigravityAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,42 @@ function visibleSnapshot(snapshot: AuthSnapshot, ownerSessionId: string): Provid
authorizationUrl: null,
interaction: null,
expiresAt: null,
...(busy ? { message: "Sign-in is in progress in another client." } : {}),
inputError: null,
...(busy
? { message: "Sign-in is in progress in another client." }
: snapshot.state.phase === "failed"
? { message: "Sign-in failed in another client." }
: {}),
};
}

const MAX_PROVIDER_REASON_LENGTH = 300;

/**
* Antigravity rejections such as "account not eligible" carry an actionable,
* human-readable reason. Show it, but only when it is short and cannot leak a
* URL, email, token, or path into the UI.
*/
function providerReason(message: string): string | undefined {
const text = message
.replace(/\p{Cc}+/gu, " ")
.replace(/\s+/g, " ")
.trim();
if (text.length === 0 || text.length > MAX_PROVIDER_REASON_LENGTH) return undefined;
if (!/\p{L}/u.test(text)) return undefined;
// Allowlist plain prose. This excludes URLs (encoded or not), paths, emails,
// and key=value fragments without enumerating each leak shape.
if (!/^[\p{L}\p{N} .,:;'"()!?-]+$/u.test(text)) return undefined;
Comment thread
kaovilai marked this conversation as resolved.
Resolved
// Scheme-like ("https:example.com") and hostname-like ("support.google.com")
// runs pass the character allowlist but still name a location. Prose puts
// a space after "." and ":".
if (/[\p{L}\p{N}][.:][\p{L}\p{N}]/u.test(text)) return undefined;
// Credential-shaped runs: a long unbroken token, or a long digit run, even
// when hyphens split it ("sk-1234...").
if (/\S{20,}/u.test(text) || /\p{N}{8,}/u.test(text)) return undefined;
return text;
}

function safeAuthFailure(cause: Cause.Cause<unknown>, usesBrowser: boolean): string {
const error = Cause.findErrorOption(cause);
if (Option.isSome(error)) {
Expand All @@ -119,6 +151,10 @@ function safeAuthFailure(cause: Cause.Cause<unknown>, usesBrowser: boolean): str
if (!usesBrowser && error.value.code === -32602) {
return "Antigravity rejected the configured credentials. Check the provider settings.";
}
if (error.value.method === "authenticate") {
const reason = providerReason(error.value.errorMessage);
if (reason) return reason;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
}
}
return usesBrowser
Expand Down Expand Up @@ -226,6 +262,7 @@ export const makeAntigravityAuth = Effect.fn("makeAntigravityAuth")(function* <
phase: Exit.isSuccess(result) ? "succeeded" : "failed",
authorizationUrl: null,
expiresAt: null,
inputError: null,
message: Exit.isSuccess(result)
? usesBrowser
? "Signed in with Google."
Expand Down Expand Up @@ -276,6 +313,7 @@ export const makeAntigravityAuth = Effect.fn("makeAntigravityAuth")(function* <
...flow.state,
phase: "verifying",
authorizationUrl: null,
inputError: null,
message: "Checking Antigravity access and models.",
});
}),
Expand Down Expand Up @@ -306,6 +344,7 @@ export const makeAntigravityAuth = Effect.fn("makeAntigravityAuth")(function* <
phase,
authorizationUrl: null,
expiresAt: null,
inputError: null,
message,
});
return true;
Expand Down Expand Up @@ -398,12 +437,19 @@ export const makeAntigravityAuth = Effect.fn("makeAntigravityAuth")(function* <
options.instanceId,
flow.pending,
input.callbackUrl,
).pipe(
// The flow keeps waiting for a correct paste, so show why this one
// was refused without ending it. The detail is a fixed message.
Effect.tapError((error) =>
publishFlow(flow, { ...flow.state, inputError: error.detail }),
),
);
flow.callbackSent = true;
yield* publishFlow(flow, {
...flow.state,
phase: "verifying",
authorizationUrl: null,
inputError: null,
message: "Waiting for Google to finish sign-in.",
});
const forwarding = yield* (
Expand Down Expand Up @@ -452,6 +498,7 @@ export const makeAntigravityAuth = Effect.fn("makeAntigravityAuth")(function* <
phase: "cancelled",
authorizationUrl: null,
expiresAt: null,
inputError: null,
message: "Google sign-in was cancelled by sign-out.",
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ export function ProviderAuthenticationSection({
: needsExternalSetup
? "No in-app sign-in advertised. Follow the provider's docs to finish setup."
: `Sign in on ${environmentLabel}.`;
const statusMessage = auth?.phase === "failed" ? auth.message : null;
const statusMessage =
auth?.phase === "failed" ? auth.message : auth?.phase === "waiting" ? auth.inputError : null;
const disabled = readOnly || pending || query.error !== null || isDiscovering;
const draftId = `${auth?.flowId ?? ""}:${interaction?.id ?? ""}`;
const values = draft.id === draftId ? draft.values : {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,18 @@ describe("Antigravity setup", () => {
expect(occurrences("Signed in.")).toBe(1);
});

it("shows why a pasted redirect URL was refused while the flow keeps waiting", async () => {
const refused = "This redirect URL does not belong to the current sign-in.";
setup.auth = authState({ message: "Open the Google sign-in link.", inputError: refused });
await renderSetup();
expect(occurrences(refused)).toBe(1);
expect(occurrences("Open the Google sign-in link.")).toBe(0);

setup.auth = authState({ message: "Open the Google sign-in link.", inputError: null });
await publish();
expect(occurrences(refused)).toBe(0);
});

it("offers sign-in again when credentials expire after a completed auth flow", async () => {
setup.auth = authState({
phase: "succeeded",
Expand Down
3 changes: 3 additions & 0 deletions packages/contracts/src/providerSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ export const ProviderAuthState = Schema.Struct({
authorizationUrl: Schema.NullOr(Schema.String),
expiresAt: Schema.NullOr(IsoDateTime),
message: Schema.NullOr(Schema.String),
// Why the last pasted redirect URL was rejected. The flow is still waiting,
// so this is not a failure and clears once a paste is accepted.
inputError: Schema.optionalKey(Schema.NullOr(Schema.String)),
// Newer servers may add method types, interactions, or owners; older
// clients drop what they cannot decode instead of rejecting the state.
methods: Schema.optionalKey(
Expand Down
Loading