Skip to content

feat(embed): clickjacking-safe /embed/[slug] campaign widget & iframe snippet (Refs #335) - #342

Merged
JOY (JOY) merged 5 commits into
devfrom
antigravity/give-335-embed-widget
Nov 23, 2026
Merged

JOY (JOY) merged 5 commits into
devfrom
antigravity/give-335-embed-widget

Conversation

@JOY

@JOY JOY (JOY) commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Refs #335

Summary

Implements the clickjacking-safe, read-only embeddable campaign widget (/embed/[slug]) and copy-paste HTML-escaped <iframe> snippet UI inside the campaign share flow.

Changes

  1. Scoped Framing Headers (apps/web/next.config.ts) & Proxy Isolation (apps/web/src/proxy.ts):

    • Switched global header rule from /:path* to /((?!embed(?:/|$)).*), preserving X-Frame-Options: DENY on all non-embed routes (/, /campaigns/*, /me, /admin, /donations/*, /invites/*).
    • Added /embed/:path* header rule with X-Content-Type-Options: nosniff, Content-Security-Policy: frame-ancestors *, Referrer-Policy: strict-origin-when-cross-origin, and no X-Frame-Options.
    • Excluded embed(?:/|$) in proxy.ts config.matcher so cross-origin <iframe> loads never trigger OAuth token refresh or Set-Cookie headers, and explicitly return 404 with X-Frame-Options: DENY + frame-ancestors 'none' for mixed-case /Embed/* paths.
  2. Chrome-Free Standalone /embed/[slug] (apps/web/src/app/embed/**, LayoutShell.tsx, layout.tsx, globals.css):

    • Added client LayoutShell (usePathname()) in RootLayout so /embed and /embed/* omit SiteHeader, SiteFooter, and RingBoxScroll on both SSR and client without altering existing route paths.
    • Added apps/web/src/app/embed/[slug]/page.tsx (force-dynamic, robots: { index: false, follow: false }), strictly anonymous dbAs() only (zero getSession(), cookies, <form>, or VietQR/transfer details), enforcing SLUG_PATTERN (<= 80) and isPublicCampaignVisible().
    • Allowlisted query parameters (theme: light | dark | auto, compact: 1 | 0, ref: sanitizeAmbassadorRef) in apps/web/src/lib/embed.ts + explicit light/dark semantic token overrides in globals.css (:root:has([data-embed-theme=...])).
    • Pre-launch (approved with prelaunch_public and future starts_at) never queries getMoneyTotals and never renders raised/goal amounts or progress bars, showing "Sắp ra mắt" + scheduled date and "Xem chi tiết" CTA.
    • Added /embed root (app/embed/page.tsx), nested /embed/[slug]/[...rest] catch-all, and compact themed EmbedNotFoundCard (app/embed/not-found.tsx).
  3. Copy-Paste Embed Snippet UI (apps/web/src/components/EmbedWidgetDisclosure.tsx, apps/web/src/app/campaigns/[slug]/page.tsx):

    • Added collapsible "Nhúng vào website / báo chí" disclosure on publicly visible campaigns (!preview && isPublicCampaignVisible(campaign)), with 280px/180px size toggle, light/dark/auto theme selector, HTML-escaped <iframe> snippet (border-radius:6px per DESIGN.md), optional valid ?ref= forwarding, and 1-click copy with share_click analytics.

Verification

  • pnpm format:check: passed
  • pnpm --filter @give/web exec vitest run --maxWorkers=2 --fileParallelism=false: 218 files, 2932 tests passed
  • pnpm --filter @give/web typecheck: passed
  • pnpm --filter @give/web lint: passed
  • pnpm --filter @give/web build: passed (ƒ /embed, ƒ /embed/[slug], ƒ /embed/[slug]/[...rest], ƒ Proxy (Middleware))

📌 TL;DR

This PR introduces a secure, read-only embeddable campaign widget (/embed/[slug]) that allows third-party sites to display campaign progress via iframe. It implements strict security boundaries (no session/auth, no forms, CSP frame-ancestors) and provides a UI component for users to generate and copy the embed code.

🎯 Type of Change

  • 🚀 New feature
  • 🐛 Bugfix
  • 🧹 Refactor
  • ⚡ Performance
  • 📚 Documentation
  • ⚙️ CI / Configuration

🔍 Changes Walkthrough

File Summary of Changes
apps/web/next.config.ts Updated security headers to allow framing for /embed/* routes via CSP frame-ancestors * while maintaining X-Frame-Options: DENY for all other routes.
apps/web/src/app/layout.tsx Refactored root layout to use a new LayoutShell component, enabling conditional rendering of site chrome (header/footer) based on the route.
apps/web/src/components/layout/LayoutShell.tsx New component that detects /embed routes (case-insensitive) and omits SiteHeader, SiteFooter, and RingBoxScroll to provide a chrome-free iframe experience.
apps/web/src/app/embed/[slug]/page.tsx New server component for the embed widget. Enforces strict read-only access (no getSession, no dbService), handles pre-launch teasers (hiding financial data), and renders a compact campaign summary with a CTA link.
apps/web/src/app/embed/[slug]/page.test.ts New comprehensive tests verifying security invariants (no auth calls, no forms), XSS protection in slugs, and correct rendering for live, pre-launch, and expired campaigns.
apps/web/src/app/embed/page.tsx New root page for /embed that triggers notFound().
apps/web/src/app/embed/not-found.tsx New custom 404 page for the embed route, rendering a compact EmbedNotFoundCard.
apps/web/src/app/embed/[slug]/[...rest]/page.tsx New catch-all page for invalid nested embed paths, triggering notFound().
apps/web/src/components/EmbedWidgetDisclosure.tsx New client component displayed on campaign pages. Allows users to toggle theme/size, generates the iframe HTML snippet, and handles clipboard copying with analytics tracking.
apps/web/src/components/EmbedNotFoundCard.tsx New client component for the embed 404 state, respecting the ?theme query parameter.
apps/web/src/app/campaigns/[slug]/page.tsx Integrated EmbedWidgetDisclosure into the campaign sidebar for both live and pre-launch public campaigns.
apps/web/src/app/globals.css Added CSS variables for explicit light/dark theme overrides triggered by [data-embed-theme] attributes.
apps/web/src/lib/embed.ts New (implied by imports) utility library containing parseEmbedOptions, buildEmbedIframeCode, and theme constants.
apps/web/src/app/layout.test.ts New tests verifying that site chrome is correctly omitted on /embed routes.
apps/web/src/components/layout/LayoutShell.test.ts New tests for LayoutShell route detection and rendering logic.
apps/web/src/components/EmbedWidgetDisclosure.test.ts New tests for the embed code generator, clipboard interaction, and analytics tracking.
apps/web/src/app/embed/not-found.test.ts New tests for the embed 404 page and nested route handling.

📊 Architectural Flow

sequenceDiagram
    participant User as User (Campaign Page)
    participant Disclosure as EmbedWidgetDisclosure
    participant ThirdParty as Third-Party Site
    participant EmbedRoute as /embed/[slug]
    participant Supabase as Supabase (Anon)

    Note over User, Disclosure: 1. Generate Embed Code
    User->>Disclosure: Click "Nhúng vào website"
    Disclosure->>Disclosure: Build iframe snippet (theme, size, ref)
    User->>Disclosure: Click "Copy"
    Disclosure->>User: Copy to clipboard

    Note over ThirdParty, Supabase: 2. Render Embed
    ThirdParty->>EmbedRoute: Load iframe (/embed/slug?theme=dark)
    EmbedRoute->>EmbedRoute: Check route (no session/auth)
    EmbedRoute->>Supabase: Fetch public campaign data (anon key)
    Supabase-->>EmbedRoute: Return campaign + totals
    EmbedRoute->>EmbedRoute: Check visibility (status, prelaunch)
    alt Public & Live
        EmbedRoute-->>ThirdParty: Render widget (progress, CTA)
    else Pre-launch or Hidden
        EmbedRoute-->>ThirdParty: Render teaser or 404
    end
Loading

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Code review skipped — your organization has no extra usage available to pay for this review.

If your organization's extra usage balance is empty, an organization admin can add extra usage credits at claude.ai/admin-settings/usage. If its monthly spend limit was reached, an admin can raise it on the same page. If neither applies, contact Anthropic support.

Once extra usage is available, someone with write access to this repository can comment @claude review on this pull request to trigger a review.

@vercel

vercel Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
give Ready Ready Preview Nov 23, 2026 1:14pm UTC

Request Review

@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

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

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 18d34684-da55-48af-a711-e3b15542d9fc

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
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@JOY
JOY (JOY) force-pushed the antigravity/give-335-embed-widget branch from a2b4572 to a36646d Compare November 23, 2026 13:07
dos[bot]
dos Bot approved these changes Nov 23, 2026

@dos dos Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⏱️ Code Review completed (21 eligible files · 78,087 chars · 3 model call(s) · 3 PR unit(s))

Whole diff with bounded context at base d69a6ba / head a36646d.

✅ No verified defects

1 possible issue(s) could not be grounded in or verified against the snapshot source, so they are not published.

Context investigation was bounded to the explicitly requested snapshot paths.

dos[bot]
dos Bot approved these changes Nov 23, 2026

@dos dos Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⏱️ Code Review completed (21 eligible files · 79,473 chars · 2 model call(s) · 3 PR unit(s))

Whole diff with bounded context at base d69a6ba / head db0de89.

✅ CLEAN_PASS: Complete diff reviewed

Context investigation was bounded to the explicitly requested snapshot paths.

@JOY

Copy link
Copy Markdown
Contributor Author

PO acceptance: merge at db0de89

@JOY
JOY (JOY) merged commit 4b8c2c4 into dev Nov 23, 2026
6 checks passed
@JOY
JOY (JOY) deleted the antigravity/give-335-embed-widget branch November 23, 2026 13:16
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