From b74cd78995467f418ad5286e8453b7c9402af9a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20=C3=98rjavik?= Date: Mon, 16 Nov 2026 14:09:37 +0100 Subject: [PATCH 1/2] chore(api): serve GraphiQL only when API_GRAPHIQL=true The deployed API served an interactive GraphiQL editor to anyone. Make it opt-in: .env.example and the Docker dev setup enable it, while the VM (which does not set the variable) turns it off on the next deploy without any server change. Closes #84 --- .env.example | 2 ++ apps/api/src/app.ts | 6 +++++- apps/api/src/graphiql.test.ts | 28 ++++++++++++++++++++++++++++ compose.dev.yaml | 2 ++ docs/operations.md | 1 + 5 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 apps/api/src/graphiql.test.ts diff --git a/.env.example b/.env.example index 8b81b33..2e79c56 100644 --- a/.env.example +++ b/.env.example @@ -5,5 +5,7 @@ API_PORT=3001 AUTH_TRUST_PROXY=false # false for localhost/current HTTP VM; true when HTTPS is configured. AUTH_COOKIE_SECURE=false +# Serve the GraphiQL editor at /graphql. Leave unset (off) in production. +API_GRAPHIQL=true # Optional absolute directory for persisted recipe images (default: uploads/ at project root). # UPLOAD_DIR=/var/lib/recipe-platform/uploads diff --git a/apps/api/src/app.ts b/apps/api/src/app.ts index 26232d3..950235f 100644 --- a/apps/api/src/app.ts +++ b/apps/api/src/app.ts @@ -29,6 +29,9 @@ export function createApp( database: { pool?: Pool; schema?: string } = {}, uploadDirectory = process.env.UPLOAD_DIR ?? fileURLToPath(new URL('../../../uploads/', import.meta.url)), + // GraphiQL is opt-in so the deployed API does not serve an interactive + // query editor. .env.example and the Docker dev setup turn it on locally. + enableGraphiql = process.env.API_GRAPHIQL === 'true', ) { const images = createImageStore( database.pool ?? db, @@ -42,7 +45,8 @@ export function createApp( const cookies = new WeakMap(); return createYoga<{ req?: IncomingMessage }, AuthContext>({ cors: false, - graphiql: { + graphiql: enableGraphiql && { + // POST requests without this header are rejected (CSRF check below). defaultHeaders: JSON.stringify({ 'X-Requested-With': 'recipe-platform' }), }, plugins: [ diff --git a/apps/api/src/graphiql.test.ts b/apps/api/src/graphiql.test.ts new file mode 100644 index 0000000..d934e20 --- /dev/null +++ b/apps/api/src/graphiql.test.ts @@ -0,0 +1,28 @@ +// @vitest-environment node +import { tmpdir } from 'node:os'; +import { describe, expect, it } from 'vitest'; +import { createApp } from './app.js'; + +// A browser opening /graphql asks for HTML; Yoga answers with GraphiQL only +// when it is enabled. No database query runs for this request. +async function openInBrowser(enableGraphiql: boolean) { + const app = createApp(undefined, false, false, {}, tmpdir(), enableGraphiql); + return app.fetch('http://localhost/graphql', { + headers: { accept: 'text/html' }, + }); +} + +describe('GraphiQL', () => { + it('is served when API_GRAPHIQL is enabled', async () => { + const response = await openInBrowser(true); + expect(response.headers.get('content-type')).toContain('text/html'); + expect(await response.text()).toContain('GraphiQL'); + }); + + it('is not served when disabled, as in production', async () => { + const response = await openInBrowser(false); + expect(response.headers.get('content-type') ?? '').not.toContain( + 'text/html', + ); + }); +}); diff --git a/compose.dev.yaml b/compose.dev.yaml index 792e4fe..8444c27 100644 --- a/compose.dev.yaml +++ b/compose.dev.yaml @@ -35,6 +35,8 @@ services: # The API must listen outside the container's loopback interface. API_HOST: 0.0.0.0 API_PORT: 3001 + # Serve the GraphiQL editor at http://localhost:3001/graphql. + API_GRAPHIQL: 'true' # Detect source edits reliably through Windows bind mounts. CHOKIDAR_USEPOLLING: 'true' diff --git a/docs/operations.md b/docs/operations.md index dcc11fb..18a4c5e 100644 --- a/docs/operations.md +++ b/docs/operations.md @@ -19,6 +19,7 @@ Lokalt kopieres `.env.example` til `.env`. På VM-en leser deploy-migrasjonen | `AUTH_TRUST_PROXY` | Aktiveres bare bak det dokumenterte lokale Apache-oppsettet | | `AUTH_COOKIE_SECURE` | `true` ved HTTPS; `false` i dagens HTTP-oppsett | | `UPLOAD_DIR` | Absolutt, vedvarende bildemappe som API-brukeren kan skrive til | +| `API_GRAPHIQL` | `true` viser GraphiQL lokalt; utelates (av) i produksjon | Sett `UPLOAD_DIR` utenfor release-mappene på VM-en, slik at opplastede bilder beholdes ved deploy. Apache må videresende `/project2/api/graphql` til `/graphql` From 6565e6f2a7ae9c1f5f232449dfb5c8266e02e6d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20=C3=98rjavik?= Date: Mon, 16 Nov 2026 14:54:56 +0100 Subject: [PATCH 2/2] chore(api): keep GraphiQL on by default and document why Following team review: GraphiQL is useful for reviewers exploring the API and grants no extra access (resolvers enforce auth, ownership and visibility; POSTs need the CSRF header). It is now on unless API_GRAPHIQL=false, and the README explains the choice. Closes #84 --- .env.example | 4 ++-- README.md | 4 ++++ apps/api/src/app.ts | 8 +++++--- apps/api/src/graphiql.test.ts | 9 +++++---- compose.dev.yaml | 2 -- docs/operations.md | 2 +- 6 files changed, 17 insertions(+), 12 deletions(-) diff --git a/.env.example b/.env.example index 2e79c56..13bcd95 100644 --- a/.env.example +++ b/.env.example @@ -5,7 +5,7 @@ API_PORT=3001 AUTH_TRUST_PROXY=false # false for localhost/current HTTP VM; true when HTTPS is configured. AUTH_COOKIE_SECURE=false -# Serve the GraphiQL editor at /graphql. Leave unset (off) in production. -API_GRAPHIQL=true +# GraphiQL editor at /graphql is on unless this is false (see README). +# API_GRAPHIQL=false # Optional absolute directory for persisted recipe images (default: uploads/ at project root). # UPLOAD_DIR=/var/lib/recipe-platform/uploads diff --git a/README.md b/README.md index 1441d45..57c2317 100644 --- a/README.md +++ b/README.md @@ -161,6 +161,10 @@ Cookie-autentiserte POST-forespørsler krever JSON og `X-Requested-With: recipe- Se [auth-forbedringer](docs/auth-improvements.md) for delt Zod-validering, feilkoder og rate limiting bak Apache. +### GraphiQL + +API-et viser GraphiQL, en interaktiv spørreeditor, på `/project2/api/graphql` når adressen åpnes i nettleseren. Den er bevisst tilgjengelig slik at faglærer og medstudenter kan utforske skjemaet og prøve spørringer. GraphiQL gir ingen ekstra tilgang: alle resolvere håndhever innlogging, eierskap og synlighet, og POST-forespørsler krever samme `X-Requested-With`-header som klienten (GraphiQL sender den automatisk). Sett `API_GRAPHIQL=false` for å slå den av. + ## Database Se [Recipe API og Delivery 2-status](docs/recipe-api.md) for CRUD-kontrakter, diff --git a/apps/api/src/app.ts b/apps/api/src/app.ts index 950235f..1d430ae 100644 --- a/apps/api/src/app.ts +++ b/apps/api/src/app.ts @@ -29,9 +29,11 @@ export function createApp( database: { pool?: Pool; schema?: string } = {}, uploadDirectory = process.env.UPLOAD_DIR ?? fileURLToPath(new URL('../../../uploads/', import.meta.url)), - // GraphiQL is opt-in so the deployed API does not serve an interactive - // query editor. .env.example and the Docker dev setup turn it on locally. - enableGraphiql = process.env.API_GRAPHIQL === 'true', + // GraphiQL (an in-browser query editor at /graphql) is on by default so + // reviewers can explore the API; it grants no extra access, because every + // resolver enforces authentication and ownership and POSTs need the CSRF + // header. Set API_GRAPHIQL=false to turn it off. + enableGraphiql = process.env.API_GRAPHIQL !== 'false', ) { const images = createImageStore( database.pool ?? db, diff --git a/apps/api/src/graphiql.test.ts b/apps/api/src/graphiql.test.ts index d934e20..5d89092 100644 --- a/apps/api/src/graphiql.test.ts +++ b/apps/api/src/graphiql.test.ts @@ -5,7 +5,7 @@ import { createApp } from './app.js'; // A browser opening /graphql asks for HTML; Yoga answers with GraphiQL only // when it is enabled. No database query runs for this request. -async function openInBrowser(enableGraphiql: boolean) { +async function openInBrowser(enableGraphiql?: boolean) { const app = createApp(undefined, false, false, {}, tmpdir(), enableGraphiql); return app.fetch('http://localhost/graphql', { headers: { accept: 'text/html' }, @@ -13,13 +13,14 @@ async function openInBrowser(enableGraphiql: boolean) { } describe('GraphiQL', () => { - it('is served when API_GRAPHIQL is enabled', async () => { - const response = await openInBrowser(true); + it('is served by default (API_GRAPHIQL unset)', async () => { + delete process.env.API_GRAPHIQL; + const response = await openInBrowser(); expect(response.headers.get('content-type')).toContain('text/html'); expect(await response.text()).toContain('GraphiQL'); }); - it('is not served when disabled, as in production', async () => { + it('is not served when API_GRAPHIQL=false', async () => { const response = await openInBrowser(false); expect(response.headers.get('content-type') ?? '').not.toContain( 'text/html', diff --git a/compose.dev.yaml b/compose.dev.yaml index 8444c27..792e4fe 100644 --- a/compose.dev.yaml +++ b/compose.dev.yaml @@ -35,8 +35,6 @@ services: # The API must listen outside the container's loopback interface. API_HOST: 0.0.0.0 API_PORT: 3001 - # Serve the GraphiQL editor at http://localhost:3001/graphql. - API_GRAPHIQL: 'true' # Detect source edits reliably through Windows bind mounts. CHOKIDAR_USEPOLLING: 'true' diff --git a/docs/operations.md b/docs/operations.md index 18a4c5e..b17e313 100644 --- a/docs/operations.md +++ b/docs/operations.md @@ -19,7 +19,7 @@ Lokalt kopieres `.env.example` til `.env`. På VM-en leser deploy-migrasjonen | `AUTH_TRUST_PROXY` | Aktiveres bare bak det dokumenterte lokale Apache-oppsettet | | `AUTH_COOKIE_SECURE` | `true` ved HTTPS; `false` i dagens HTTP-oppsett | | `UPLOAD_DIR` | Absolutt, vedvarende bildemappe som API-brukeren kan skrive til | -| `API_GRAPHIQL` | `true` viser GraphiQL lokalt; utelates (av) i produksjon | +| `API_GRAPHIQL` | `false` slår av GraphiQL; standard er på (se README) | Sett `UPLOAD_DIR` utenfor release-mappene på VM-en, slik at opplastede bilder beholdes ved deploy. Apache må videresende `/project2/api/graphql` til `/graphql`