Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion apps/api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -42,7 +47,8 @@ export function createApp(
const cookies = new WeakMap<Request, string>();
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: [
Expand Down
29 changes: 29 additions & 0 deletions apps/api/src/graphiql.test.ts
Original file line number Diff line number Diff line change
@@ -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',
);
});
});
1 change: 1 addition & 0 deletions docs/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down