diff --git a/.env.example b/.env.example index 8b81b33..13bcd95 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 +# 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 26232d3..1d430ae 100644 --- a/apps/api/src/app.ts +++ b/apps/api/src/app.ts @@ -29,6 +29,11 @@ export function createApp( database: { pool?: Pool; schema?: string } = {}, uploadDirectory = process.env.UPLOAD_DIR ?? fileURLToPath(new URL('../../../uploads/', import.meta.url)), + // 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, @@ -42,7 +47,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..5d89092 --- /dev/null +++ b/apps/api/src/graphiql.test.ts @@ -0,0 +1,29 @@ +// @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 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 API_GRAPHIQL=false', async () => { + const response = await openInBrowser(false); + expect(response.headers.get('content-type') ?? '').not.toContain( + 'text/html', + ); + }); +}); diff --git a/docs/operations.md b/docs/operations.md index dcc11fb..b17e313 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` | `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`