Skip to content

refactor(web): centralize OAuth callback cookie contract (fixes #1362)#1430

Merged
MODSetter merged 1 commit into
MODSetter:devfrom
suryo12:refactor/1362-oauth-typed-contract
May 23, 2026
Merged

refactor(web): centralize OAuth callback cookie contract (fixes #1362)#1430
MODSetter merged 1 commit into
MODSetter:devfrom
suryo12:refactor/1362-oauth-typed-contract

Conversation

@suryo12
Copy link
Copy Markdown
Contributor

@suryo12 suryo12 commented May 23, 2026

Extracts the OAuth callback cookie contract into a single typed module shared by the callback route and the connector dialog hook. Eliminates duplicated cookie name and payload shape across two files.

Description

Adds surfsense_web/contracts/types/oauth.types.ts exporting:

  • OAUTH_RESULT_COOKIE constant
  • oauthCallbackResultSchema Zod schema
  • OAuthCallbackResult type (inferred from the schema)
  • parseOAuthCallbackResult() helper that returns null on invalid JSON or shape mismatch

Updates the two existing files to import from this contract instead of redeclaring locally:

  • surfsense_web/app/dashboard/[search_space_id]/connectors/callback/route.ts — removed local OAUTH_RESULT_COOKIE; the cookie payload is now annotated with OAuthCallbackResult so the writer side is constrained by the schema at compile time.
  • surfsense_web/components/assistant-ui/connector-popup/hooks/use-connector-dialog.ts — removed local OAUTH_RESULT_COOKIE and inline result type; replaced the try { JSON.parse() } catch {} block with parseOAuthCallbackResult(). The helper rejects payloads whose shape doesn't match the schema, removing the silent runtime risk when the cookie is tampered with or its shape drifts.

Motivation and Context

The cookie name and payload shape were declared in two separate files, so typos and schema drift were undetectable at build time. Centralizing the contract removes the duplication and makes the writer/reader contract enforceable by the type system.

FIX #1362

Screenshots

N/A — refactor, no user-facing behavior change.

API Changes

  • This PR includes API changes

Change Type

  • Refactoring

Testing Performed

pnpm exec biome check passes on all three files. pnpm exec tsc --noEmit introduces no new type errors in the touched files. The OAuth flow is already covered end-to-end by the existing tests/connectors/** Playwright journeys; no behavior change expected.

  • Tested locally
  • Manual/QA verification

Checklist

  • Follows project coding standards and conventions
  • Documentation updated as needed
  • Dependencies updated as needed
  • No lint/build errors or new warnings
  • All relevant tests are passing

High-level PR Summary

This PR refactors the OAuth callback cookie handling by extracting the duplicated cookie name and payload structure into a centralized, typed contract module. The new oauth.types.ts contract module exports a Zod schema, TypeScript types, and a safe parsing helper that both the callback route (writer) and connector dialog hook (reader) now share, eliminating schema drift risk and enforcing compile-time type safety across the OAuth flow.

⏱️ Estimated Review Time: 5-15 minutes

💡 Review Order Suggestion
Order File Path
1 surfsense_web/contracts/types/oauth.types.ts
2 surfsense_web/app/dashboard/[search_space_id]/connectors/callback/route.ts
3 surfsense_web/components/assistant-ui/connector-popup/hooks/use-connector-dialog.ts

Need help? Join our Discord

…tter#1362)

Replace the duplicated `OAUTH_RESULT_COOKIE` constant and inline payload
type across the callback route and connector dialog hook with a shared
`contracts/types/oauth.types.ts` module that exports:

- OAUTH_RESULT_COOKIE constant
- oauthCallbackResultSchema Zod schema
- OAuthCallbackResult type (inferred from the schema)
- parseOAuthCallbackResult() helper that returns null on invalid JSON
  or shape mismatch

The route handler now uses the shared type to constrain the cookie
payload at compile time. The consumer hook validates the cookie value
through the helper instead of an unchecked JSON.parse, removing the
silent runtime risk when the cookie is tampered with or its shape
drifts.
Copilot AI review requested due to automatic review settings May 23, 2026 16:29
@vercel
Copy link
Copy Markdown

vercel Bot commented May 23, 2026

@suryo12 is attempting to deploy a commit to the Rohan Verma's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 23, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d3828abf-b8ee-439b-a183-50a0d12882e8

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Centralizes the OAuth callback cookie contract (name, schema, parsing) to make the connector dialog’s OAuth result handling safer and consistent between server callback and client UI.

Changes:

  • Added shared Zod schema + parser for the connector_oauth_result cookie payload.
  • Updated the connector dialog hook to validate cookie payloads via the shared parser instead of raw JSON.parse.
  • Updated the OAuth callback route to reuse the shared cookie constant and strongly type the payload.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
surfsense_web/contracts/types/oauth.types.ts Introduces shared cookie constant, payload schema, and parsing helper.
surfsense_web/components/assistant-ui/connector-popup/hooks/use-connector-dialog.ts Uses shared cookie parsing/constant to handle OAuth callback results more safely.
surfsense_web/app/dashboard/[search_space_id]/connectors/callback/route.ts Uses shared cookie constant/type when constructing the callback payload.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +18 to +26
/**
* Safely decode and validate the OAuth callback cookie value. Returns `null`
* when the value is not valid JSON or does not match the expected shape.
*/
export function parseOAuthCallbackResult(raw: string): OAuthCallbackResult | null {
let parsed: unknown;
try {
parsed = JSON.parse(raw);
} catch {
Comment on lines +2 to 3
import { OAUTH_RESULT_COOKIE, type OAuthCallbackResult } from "@/contracts/types/oauth.types";

@MODSetter MODSetter merged commit d53866d into MODSetter:dev May 23, 2026
5 of 8 checks passed
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.

3 participants