diff --git a/.env.example b/.env.example index 67d10f1..3a95e77 100644 --- a/.env.example +++ b/.env.example @@ -10,6 +10,8 @@ CLOUDINARY_TRIAL_PRESET=pravaha_trial TRIALS_PER_DAY=5 # Read-only demo Studio for visitors without the passcode: on | off STUDIO_DEMO=on +# Optional: who may frame /embed/ask (a frame-ancestors source list; default any HTTPS page). docs/EMBED.md +EMBED_FRAME_ANCESTORS= # Neon Postgres — pooled connection string DATABASE_URL= diff --git a/README.md b/README.md index 348c764..277dfce 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,7 @@ Colleges, clubs and coaching institutes record hundreds of hours of lectures and | **Study Packs** | Every session gets a summary, key concepts, a quiz whose explanations **play the moment** the teacher explains it, and a "Session in 60 seconds" highlight reel. Pravaha generates them automatically from the transcript. | | **Moments** | One tap turns any cited clip into a vertical, AI-cropped, subtitled short for WhatsApp or Instagram, with its own share page and preview card. The short is just a Cloudinary URL; nothing is rendered. | | **Insights** | Organizers see what learners ask, the **knowledge gaps** the library can't answer yet (what to record next), and which Moments get shared. Visitors can explore a read-only demo of the Studio. | +| **Embed** | Organizers copy one ` +``` + +| URL | What it asks | +|---|---| +| `/embed/ask` | Every published session | +| `/embed/ask?session=` | One session | +| `…&theme=dark` or `theme=light` | Forces a theme to match the host page; otherwise it follows the learner's system | + +Paste it wherever the LMS accepts HTML: in Moodle a **Page** or **Label** in the HTML editor, in Canvas the +page editor's HTML view, in Google Sites **Embed → Embed code**. + +## What the learner gets + +The same grounded Ask as the site: streamed progress, an answer only from the recordings, a playable clip for +every claim, an Answer Reel and follow-up questions that stay in the embed. Links to the rest of Pravaha +(a session's Watch page, a shared answer) open in a new tab so the course page stays put. The site header and +footer are left out. + +## Fitting the frame + +`height="640"` fits a question and a short answer; the embed scrolls inside it. Hosts that run their own +scripts can size it to the content instead: the embed posts its height whenever it changes. + +```html + +``` + +The message carries only `{ type: "pravaha:height", height }`, never the question or the answer. + +## Security + +`/embed/*` is the only route that may be framed (`next.config.ts`). Every other page keeps +`X-Frame-Options: DENY` and `frame-ancestors 'none'`, so the Studio, Watch pages and shared answers can't be +clickjacked. The embed itself has no sign-in, cookies or actions beyond asking, which is what makes it safe +to frame. + +By default any HTTPS page may frame it. An institute can restrict it to its own LMS with an environment +variable (a [`frame-ancestors`](https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/frame-ancestors) +source list, applied at build time, so redeploy after changing it): + +```bash +EMBED_FRAME_ANCESTORS="https://moodle.example.edu https://*.instructure.com" +``` + +## Limits + +Ask is rate-limited per IP address (60 questions an hour) before any paid AI call (`src/lib/rate-limit.ts`). +A whole class behind one campus network shares an address, so a deployment for a large class should raise +`ASK_PER_IP_PER_HOUR`. The library-wide cap of 500 questions a day still bounds the cost. diff --git a/docs/SECURITY.md b/docs/SECURITY.md index 0d38ca0..2d58f89 100644 --- a/docs/SECURITY.md +++ b/docs/SECURITY.md @@ -81,4 +81,4 @@ Rights confirmation is required before upload (`rights_confirmed_at`), sessions ## HTTP Security Headers -Every response carries (`next.config.ts`): `Strict-Transport-Security` (2 years, subdomains, preload), `X-Content-Type-Options: nosniff`, `Referrer-Policy: strict-origin-when-cross-origin`, `X-Frame-Options: DENY` plus `Content-Security-Policy: frame-ancestors 'none'; base-uri 'self'; form-action 'self'; object-src 'none'` (no clickjacking, no base-tag or plugin injection), and a `Permissions-Policy` that turns off camera, microphone, geolocation, payment and USB. `X-Powered-By` is removed. A full script-source CSP is deliberately not set yet: the Cloudinary Video Player loads scripts, styles and HLS segments from several hosts, so that policy needs testing against every player feature first. +Every response carries (`next.config.ts`): `Strict-Transport-Security` (2 years, subdomains, preload), `X-Content-Type-Options: nosniff`, `Referrer-Policy: strict-origin-when-cross-origin`, `X-Frame-Options: DENY` plus `Content-Security-Policy: frame-ancestors 'none'; base-uri 'self'; form-action 'self'; object-src 'none'` (no clickjacking, no base-tag or plugin injection; the one exception is `/embed/*`, the sign-in-free Ask box an LMS frames, whose `frame-ancestors` is `https:` or the `EMBED_FRAME_ANCESTORS` list; see [`EMBED.md`](EMBED.md)), and a `Permissions-Policy` that turns off camera, microphone, geolocation, payment and USB. `X-Powered-By` is removed. A full script-source CSP is deliberately not set yet: the Cloudinary Video Player loads scripts, styles and HLS segments from several hosts, so that policy needs testing against every player feature first. diff --git a/next.config.ts b/next.config.ts index 126c5c8..6a378b8 100644 --- a/next.config.ts +++ b/next.config.ts @@ -3,14 +3,18 @@ import type { NextConfig } from "next"; // Baseline security headers on every response (docs/SECURITY.md). No full script CSP yet: the Cloudinary // Video Player loads its own scripts, styles and HLS media from several hosts, so a strict policy needs // testing against every player feature first. frame-ancestors still blocks clickjacking. -const securityHeaders = [ +const baseHeaders = [ { key: "Strict-Transport-Security", value: "max-age=63072000; includeSubDomains; preload" }, { key: "X-Content-Type-Options", value: "nosniff" }, { key: "Referrer-Policy", value: "strict-origin-when-cross-origin" }, - { key: "X-Frame-Options", value: "DENY" }, - { key: "Content-Security-Policy", value: "frame-ancestors 'none'; base-uri 'self'; form-action 'self'; object-src 'none'" }, { key: "Permissions-Policy", value: "camera=(), microphone=(), geolocation=(), payment=(), usb=()" }, ]; +const policy = (ancestors: string) => `frame-ancestors ${ancestors}; base-uri 'self'; form-action 'self'; object-src 'none'`; + +// /embed/* is the one framable route: the Ask box an LMS or course page puts in an iframe (docs/EMBED.md). +// EMBED_FRAME_ANCESTORS narrows it to an institute's own origins; the default allows any HTTPS page. +// Every other route stays unframable (X-Frame-Options can't be relaxed per route, so it skips /embed). +const embedAncestors = process.env.EMBED_FRAME_ANCESTORS?.trim() || "https:"; const nextConfig: NextConfig = { poweredByHeader: false, @@ -18,7 +22,17 @@ const nextConfig: NextConfig = { remotePatterns: [{ protocol: "https", hostname: "res.cloudinary.com" }], }, async headers() { - return [{ source: "/(.*)", headers: securityHeaders }]; + return [ + { source: "/(.*)", headers: baseHeaders }, + { + source: "/((?!embed(?:/|$)).*)", + headers: [ + { key: "X-Frame-Options", value: "DENY" }, + { key: "Content-Security-Policy", value: policy("'none'") }, + ], + }, + { source: "/embed/:path*", headers: [{ key: "Content-Security-Policy", value: policy(embedAncestors) }] }, + ]; }, }; diff --git a/src/app/embed/ask/page.tsx b/src/app/embed/ask/page.tsx new file mode 100644 index 0000000..5604df0 --- /dev/null +++ b/src/app/embed/ask/page.tsx @@ -0,0 +1,79 @@ +import type { Metadata } from "next"; +import Link from "next/link"; +import { z } from "zod"; + +import { AskSession } from "@/components/AskSession"; +import { EmbedFrame } from "@/components/EmbedFrame"; +import { LogoMark } from "@/components/Logo"; +import { getLecture, suggestedQuestions } from "@/lib/lectures"; +import { getStudyPack } from "@/lib/study-packs"; +import { pickSuggestions } from "@/lib/suggestions"; + +export const metadata: Metadata = { + title: "Ask — Pravaha", + description: "Ask the recordings a question; every answer plays the moment it came from.", + robots: { index: false, follow: false }, +}; + +type Props = { searchParams: Promise<{ session?: string }> }; + +// The Ask box an LMS or course page puts in an iframe: /embed/ask asks the published library, +// /embed/ask?session= one session (unlisted ones too: the organizer chose to share it). This route alone +// may be framed (next.config.ts); the site header and footer are left out (SiteChrome). +export default async function EmbedAskPage({ searchParams }: Props) { + const { session } = await searchParams; + + if (session === undefined) { + const suggestions = await suggestedQuestions(3); + return ( + + + + ); + } + + const lecture = z.uuid().safeParse(session).success ? await getLecture(session) : null; + // Trials expire within a day, so an embed of one would break; they never get an embed. + if (!lecture || lecture.status !== "ready" || lecture.trialExpiresAt) { + return ( + +

+ This session isn't available to ask. It may still be processing or have been removed. +

+
+ ); + } + + const pack = await getStudyPack(lecture.id); + return ( + +

+ + {lecture.title} + + {lecture.speaker ? ` · ${lecture.speaker}` : ""} +

+ +
+ ); +} + +function EmbedShell({ children }: { children: React.ReactNode }) { + return ( + +
+ + + Pravaha + + {children} +

+ Answers come only from these recordings, with a clip for every claim ·{" "} + + Privacy + +

+
+
+ ); +} diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 5042d00..acb13ee 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -4,6 +4,7 @@ import Link from "next/link"; import { LogoMark } from "@/components/Logo"; import { SearchShortcut } from "@/components/SearchShortcut"; +import { SiteChrome } from "@/components/SiteChrome"; import { THEME_INIT_SCRIPT, ThemeToggle } from "@/components/ThemeToggle"; import { publicBaseUrl } from "@/lib/site"; @@ -42,57 +43,61 @@ export default function RootLayout({ children }: { children: React.ReactNode }) Skip to content -
- -
- +
+ + Library + + + Saved + + + Studio + + + Try it + + +
+ + + +
{children}
-
-
-

- - Pravaha · ask your recordings, watch the answer -

-

- - Privacy - {" "} - ·{" "} - - Terms - {" "} - · Built on Cloudinary · Team Code Blooded ·{" "} - - GitHub - -

-
-
+ +
+
+

+ + Pravaha · ask your recordings, watch the answer +

+

+ + Privacy + {" "} + ·{" "} + + Terms + {" "} + · Built on Cloudinary · Team Code Blooded ·{" "} + + GitHub + +

+
+
+
); diff --git a/src/components/AskSession.tsx b/src/components/AskSession.tsx index a5406ea..c3c9fe5 100644 --- a/src/components/AskSession.tsx +++ b/src/components/AskSession.tsx @@ -6,7 +6,16 @@ import { AskAnswer } from "@/components/AskAnswer"; // "Ask this session": the same grounded Ask as the home page, scoped to one recording. Follow-up // questions stay in this session. Suggestions come from the session's Study Pack when it has one. -export function AskSession({ lectureId, suggestions = [] }: { lectureId: string; suggestions?: string[] }) { +// Without `lectureId` it asks the whole library (the /embed/ask box an LMS frames). +export function AskSession({ + lectureId, + suggestions = [], + heading = "Ask this session", +}: { + lectureId?: string; + suggestions?: string[]; + heading?: string; +}) { const [draft, setDraft] = useState(""); const [question, setQuestion] = useState(null); @@ -20,7 +29,7 @@ export function AskSession({ lectureId, suggestions = [] }: { lectureId: string; return (

- Ask this session + {heading}

{ @@ -30,7 +39,7 @@ export function AskSession({ lectureId, suggestions = [] }: { lectureId: string; className="relative mt-3" > { + setCopied(true); + setTimeout(() => setCopied(false), 2000); + }, + () => {}, + ); + } + + return ( +
+

{lectureId ? "Embed “Ask this session”" : "Embed “Ask the library”"}

+

+ Paste into Moodle, Canvas, Google Sites or any page that accepts HTML. Learners ask without an account, and every answer + plays the moment it came from. +

+ +