@constructive-io/graphql-server 5.32.5 → 5.32.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,6 +22,14 @@ const parseCookieToken = (req, cookieName) => {
22
22
  const match = header.split(';').find((c) => c.trim().startsWith(`${cookieName}=`));
23
23
  return match ? decodeURIComponent(match.split('=')[1].trim()) : undefined;
24
24
  };
25
+ /**
26
+ * The Bearer token `authenticate` would use, or undefined when it would fall
27
+ * back to the session cookie (no header, another scheme, or an empty token).
28
+ */
29
+ export const getBearerToken = (req) => {
30
+ const [authType, authToken] = (req.headers.authorization ?? '').split(' ');
31
+ return authType?.toLowerCase() === 'bearer' && authToken ? authToken : undefined;
32
+ };
25
33
  export const createAuthenticateMiddleware = (opts) => {
26
34
  return async (req, res, next) => {
27
35
  const api = req.api;
@@ -49,16 +57,15 @@ export const createAuthenticateMiddleware = (opts) => {
49
57
  log.info(`[auth] strictAuth=${opts.server?.strictAuth ?? false}, authFn=${authFn ?? 'none'}`);
50
58
  if (authFn && rlsModule.privateSchema.schemaName) {
51
59
  const { authorization = '' } = req.headers;
52
- const [authType, authToken] = authorization.split(' ');
60
+ const [authType] = authorization.split(' ');
61
+ const bearerToken = getBearerToken(req);
53
62
  let token = {};
54
63
  log.info(`[auth] authorization header present=${!!authorization}, ` +
55
- `authType=${authType ?? 'none'}, hasToken=${!!authToken}`);
64
+ `authType=${authType ?? 'none'}, hasToken=${!!bearerToken}`);
56
65
  // Resolve the credential: prefer Bearer header, fall back to session cookie
57
66
  const cookieToken = parseCookieToken(req, SESSION_COOKIE_NAME);
58
- const effectiveToken = (authType?.toLowerCase() === 'bearer' && authToken)
59
- ? authToken
60
- : cookieToken;
61
- const tokenSource = (authType?.toLowerCase() === 'bearer' && authToken) ? 'bearer' : (cookieToken ? 'cookie' : 'none');
67
+ const effectiveToken = bearerToken ?? cookieToken;
68
+ const tokenSource = bearerToken ? 'bearer' : (cookieToken ? 'cookie' : 'none');
62
69
  if (effectiveToken) {
63
70
  log.info(`[auth] Processing ${tokenSource} authentication`);
64
71
  const context = {
package/esm/server.js CHANGED
@@ -16,7 +16,7 @@ import { startDebugSampler } from './diagnostics/debug-sampler';
16
16
  import { isDevelopmentObservabilityMode, isGraphqlObservabilityEnabled, isGraphqlObservabilityRequested, isLoopbackHost } from './diagnostics/observability';
17
17
  import { createAdmissionControlMiddleware } from './middleware/admission-control';
18
18
  import { createApiMiddleware } from './middleware/api';
19
- import { createAuthenticateMiddleware } from './middleware/auth';
19
+ import { createAuthenticateMiddleware, getBearerToken } from './middleware/auth';
20
20
  // Auth cookie handling is done via AuthCookiePlugin in grafserv
21
21
  import { createCaptchaMiddleware } from './middleware/captcha';
22
22
  import { parseCookieValue, rejectInvalidSessionCookieHeader, SESSION_COOKIE_NAME } from './middleware/cookie';
@@ -168,9 +168,8 @@ class Server {
168
168
  }
169
169
  });
170
170
  const csrfProtect = (req, res, next) => {
171
- // Skip CSRF for Bearer token auth
172
- const auth = req.headers.authorization;
173
- if (auth?.toLowerCase().startsWith('bearer ')) {
171
+ // Skip CSRF only when authenticate will use the Bearer token, not the cookie
172
+ if (getBearerToken(req)) {
174
173
  return next();
175
174
  }
176
175
  // Skip if no session cookie (anonymous requests)
@@ -185,7 +184,9 @@ class Server {
185
184
  csrf.setToken(req, res, next);
186
185
  };
187
186
  app.use(csrfSetToken); // Set CSRF token cookie on all requests
188
- app.use('/graphql', csrfProtect); // Enforce CSRF on GraphQL mutations
187
+ // Enforce CSRF on every cookie-authenticated route: GraphQL, the agentic
188
+ // router (/v1) and REST function invocation (/fn)
189
+ app.use(['/graphql', '/v1', '/fn'], csrfProtect);
189
190
  // The session-cookie opt-out does not bypass CSRF: it only stops sign-in
190
191
  // responses from setting cookies (see AuthCookiePlugin).
191
192
  app.use('/graphql', rejectInvalidSessionCookieHeader);
@@ -1,4 +1,9 @@
1
1
  import './types';
2
2
  import { PgpmOptions } from '@pgpmjs/types';
3
- import { RequestHandler } from 'express';
3
+ import { Request, RequestHandler } from 'express';
4
+ /**
5
+ * The Bearer token `authenticate` would use, or undefined when it would fall
6
+ * back to the session cookie (no header, another scheme, or an empty token).
7
+ */
8
+ export declare const getBearerToken: (req: Request) => string | undefined;
4
9
  export declare const createAuthenticateMiddleware: (opts: PgpmOptions) => RequestHandler;
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.createAuthenticateMiddleware = void 0;
6
+ exports.createAuthenticateMiddleware = exports.getBearerToken = void 0;
7
7
  require("./types"); // for Request type
8
8
  const errors_1 = require("@constructive-io/errors");
9
9
  const env_1 = require("@pgpmjs/env");
@@ -28,6 +28,15 @@ const parseCookieToken = (req, cookieName) => {
28
28
  const match = header.split(';').find((c) => c.trim().startsWith(`${cookieName}=`));
29
29
  return match ? decodeURIComponent(match.split('=')[1].trim()) : undefined;
30
30
  };
31
+ /**
32
+ * The Bearer token `authenticate` would use, or undefined when it would fall
33
+ * back to the session cookie (no header, another scheme, or an empty token).
34
+ */
35
+ const getBearerToken = (req) => {
36
+ const [authType, authToken] = (req.headers.authorization ?? '').split(' ');
37
+ return authType?.toLowerCase() === 'bearer' && authToken ? authToken : undefined;
38
+ };
39
+ exports.getBearerToken = getBearerToken;
31
40
  const createAuthenticateMiddleware = (opts) => {
32
41
  return async (req, res, next) => {
33
42
  const api = req.api;
@@ -55,16 +64,15 @@ const createAuthenticateMiddleware = (opts) => {
55
64
  log.info(`[auth] strictAuth=${opts.server?.strictAuth ?? false}, authFn=${authFn ?? 'none'}`);
56
65
  if (authFn && rlsModule.privateSchema.schemaName) {
57
66
  const { authorization = '' } = req.headers;
58
- const [authType, authToken] = authorization.split(' ');
67
+ const [authType] = authorization.split(' ');
68
+ const bearerToken = (0, exports.getBearerToken)(req);
59
69
  let token = {};
60
70
  log.info(`[auth] authorization header present=${!!authorization}, ` +
61
- `authType=${authType ?? 'none'}, hasToken=${!!authToken}`);
71
+ `authType=${authType ?? 'none'}, hasToken=${!!bearerToken}`);
62
72
  // Resolve the credential: prefer Bearer header, fall back to session cookie
63
73
  const cookieToken = parseCookieToken(req, SESSION_COOKIE_NAME);
64
- const effectiveToken = (authType?.toLowerCase() === 'bearer' && authToken)
65
- ? authToken
66
- : cookieToken;
67
- const tokenSource = (authType?.toLowerCase() === 'bearer' && authToken) ? 'bearer' : (cookieToken ? 'cookie' : 'none');
74
+ const effectiveToken = bearerToken ?? cookieToken;
75
+ const tokenSource = bearerToken ? 'bearer' : (cookieToken ? 'cookie' : 'none');
68
76
  if (effectiveToken) {
69
77
  log.info(`[auth] Processing ${tokenSource} authentication`);
70
78
  const context = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@constructive-io/graphql-server",
3
- "version": "5.32.5",
3
+ "version": "5.32.6",
4
4
  "author": "Constructive <developers@constructive.io>",
5
5
  "description": "Constructive GraphQL Server",
6
6
  "main": "index.js",
@@ -100,5 +100,5 @@
100
100
  "supertest": "^7.2.2",
101
101
  "ts-node": "^10.9.2"
102
102
  },
103
- "gitHead": "772d31b8f0afd8947d4f4df8b0ffee86414ec866"
103
+ "gitHead": "a94434095095bc1f1c06d8878e06f88d37b121b4"
104
104
  }
package/server.js CHANGED
@@ -175,9 +175,8 @@ class Server {
175
175
  }
176
176
  });
177
177
  const csrfProtect = (req, res, next) => {
178
- // Skip CSRF for Bearer token auth
179
- const auth = req.headers.authorization;
180
- if (auth?.toLowerCase().startsWith('bearer ')) {
178
+ // Skip CSRF only when authenticate will use the Bearer token, not the cookie
179
+ if ((0, auth_1.getBearerToken)(req)) {
181
180
  return next();
182
181
  }
183
182
  // Skip if no session cookie (anonymous requests)
@@ -192,7 +191,9 @@ class Server {
192
191
  csrf.setToken(req, res, next);
193
192
  };
194
193
  app.use(csrfSetToken); // Set CSRF token cookie on all requests
195
- app.use('/graphql', csrfProtect); // Enforce CSRF on GraphQL mutations
194
+ // Enforce CSRF on every cookie-authenticated route: GraphQL, the agentic
195
+ // router (/v1) and REST function invocation (/fn)
196
+ app.use(['/graphql', '/v1', '/fn'], csrfProtect);
196
197
  // The session-cookie opt-out does not bypass CSRF: it only stops sign-in
197
198
  // responses from setting cookies (see AuthCookiePlugin).
198
199
  app.use('/graphql', cookie_1.rejectInvalidSessionCookieHeader);