Skip to content

Open source alternative to Semrush and Ahrefs

Reject Google connects that skip the Search Console or Analytics permission (EVE-408) (#774)

Ben Senescuf1d9a2b080d1
diff --git a/src/client/features/integrations/googleAuthErrorCopy.ts b/src/client/features/integrations/googleAuthErrorCopy.ts
index 6d134ab4..be2f5771 100644
--- a/src/client/features/integrations/googleAuthErrorCopy.ts
+++ b/src/client/features/integrations/googleAuthErrorCopy.ts
@@ -27,6 +27,11 @@ export function googleAuthErrorCopy(
description:
"Google's permission screen was closed or declined. Try again whenever you're ready.",
};
+ case "scope_not_granted":
+ return {
+ title: `${what} is missing a permission`,
+ description: `OpenSEO didn't get access to your ${providerLabel ?? "Google"} data because a box on Google's permission screen wasn't checked. Try again and check every box before you continue.`,
+ };
case "connection_save_failed":
return {
title: `${what} didn't finish`,
diff --git a/src/server/features/google/googleOAuth.test.ts b/src/server/features/google/googleOAuth.test.ts
index 10f308aa..dea74f7a 100644
--- a/src/server/features/google/googleOAuth.test.ts
+++ b/src/server/features/google/googleOAuth.test.ts
@@ -194,6 +194,7 @@ describe("Google OAuth grants", () => {
refresh_token: "refresh-token",
expires_in: 3600,
id_token: idToken,
+ scope: "openid https://www.googleapis.com/auth/analytics.readonly",
}),
);
@@ -318,6 +319,25 @@ describe("Google OAuth grants", () => {
},
);
+ it("returns a consent without the data scope to the connect page without storing a grant", async () => {
+ const state = await authorizationState(oauth.GSC_INTEGRATION);
+ mocks.fetch.mockResolvedValue(
+ tokenResponse({
+ access_token: "access-token",
+ scope: "openid https://www.googleapis.com/auth/userinfo.email",
+ }),
+ );
+
+ const response = await callback(oauth.GSC_INTEGRATION, state, {
+ code: "code-1",
+ });
+
+ expect(response.headers.get("Location")).toBe(
+ "/p/project/settings?tab=integrations&google_link_error=gsc&error=scope_not_granted",
+ );
+ expect(await grants()).toEqual([]);
+ });
+
it("returns a grant save failure to the connect page", async () => {
const state = await authorizationState(oauth.GA4_INTEGRATION);
mocks.fetch.mockResolvedValue(
diff --git a/src/server/features/google/googleOAuth.ts b/src/server/features/google/googleOAuth.ts
index 691227c0..20de215e 100644
--- a/src/server/features/google/googleOAuth.ts
+++ b/src/server/features/google/googleOAuth.ts
@@ -8,12 +8,20 @@ import { resolveUserContextFromHeaders } from "@/middleware/ensure-user/resolve"
import { AppError } from "@/server/lib/errors";
import { responseForAppError } from "@/server/lib/http-errors";
import { getPublicOrigin } from "@/server/mcp/public-origin";
-import { GA4_OAUTH_PROVIDER_ID, GA4_OAUTH_SCOPES } from "@/shared/ga4";
+import {
+ GA4_DATA_SCOPE,
+ GA4_OAUTH_PROVIDER_ID,
+ GA4_OAUTH_SCOPES,
+} from "@/shared/ga4";
import {
GOOGLE_LINK_ERROR_PARAM,
type GoogleLinkProvider,
} from "@/shared/google-link";
-import { GSC_OAUTH_PROVIDER_ID, GSC_OAUTH_SCOPES } from "@/shared/gsc";
+import {
+ GSC_DATA_SCOPE,
+ GSC_OAUTH_PROVIDER_ID,
+ GSC_OAUTH_SCOPES,
+} from "@/shared/gsc";
import {
getGoogleOAuthClientConfig,
hasGoogleOAuthConfig,
@@ -51,6 +59,8 @@ export type GoogleOAuthIntegration = {
displayName: string;
callbackPath: `/${string}`;
scopes: readonly string[];
+ /** The data permission the integration can't work without. */
+ requiredScope: string;
};
export const GSC_INTEGRATION: GoogleOAuthIntegration = {
@@ -59,6 +69,7 @@ export const GSC_INTEGRATION: GoogleOAuthIntegration = {
displayName: "Search Console",
callbackPath: "/api/gsc/oauth/callback",
scopes: GSC_OAUTH_SCOPES,
+ requiredScope: GSC_DATA_SCOPE,
};
export const GA4_INTEGRATION: GoogleOAuthIntegration = {
@@ -67,6 +78,7 @@ export const GA4_INTEGRATION: GoogleOAuthIntegration = {
displayName: "Google Analytics",
callbackPath: "/api/ga4/oauth/callback",
scopes: GA4_OAUTH_SCOPES,
+ requiredScope: GA4_DATA_SCOPE,
};
const googleTokenResponseSchema = z.object({
@@ -279,6 +291,16 @@ async function handleGoogleOAuthCallback(input: {
return fail("oauth_code_verification_failed");
}
if (!tokens) return fail("oauth_code_verification_failed");
+ // Google's consent screen lets the user approve sign-in while leaving the
+ // Search Console or Analytics box unchecked. That grant gets a 403 on every
+ // API call, so send them back to check it rather than saving it. An omitted
+ // scope means everything requested was granted.
+ if (
+ tokens.scope &&
+ !tokens.scope.split(/\s+/).includes(input.integration.requiredScope)
+ ) {
+ return fail("scope_not_granted");
+ }
let googleAccountId: string;
try {
googleAccountId = await getGoogleAccountId(
diff --git a/src/shared/ga4.ts b/src/shared/ga4.ts
index 2044762c..78447ae4 100644
--- a/src/shared/ga4.ts
+++ b/src/shared/ga4.ts
@@ -1,11 +1,14 @@
/** Better Auth provider ID for the dedicated Google Analytics grant. */
export const GA4_OAUTH_PROVIDER_ID = "google-analytics";
+export const GA4_DATA_SCOPE =
+ "https://www.googleapis.com/auth/analytics.readonly";
+
export const GA4_OAUTH_SCOPES = [
"openid",
"email",
"profile",
- "https://www.googleapis.com/auth/analytics.readonly",
+ GA4_DATA_SCOPE,
] as const;
export const GA4_SELF_HOSTED_SETUP_DOCS_URL =
diff --git a/src/shared/gsc.ts b/src/shared/gsc.ts
index c41ef21c..67362b3e 100644
--- a/src/shared/gsc.ts
+++ b/src/shared/gsc.ts
@@ -3,11 +3,14 @@
* button) can reference it without importing the server-only auth config. */
export const GSC_OAUTH_PROVIDER_ID = "google-search-console";
+export const GSC_DATA_SCOPE =
+ "https://www.googleapis.com/auth/webmasters.readonly";
+
export const GSC_OAUTH_SCOPES = [
"openid",
"email",
"profile",
- "https://www.googleapis.com/auth/webmasters.readonly",
+ GSC_DATA_SCOPE,
] as const;
export const GSC_SELF_HOSTED_SETUP_DOCS_URL =