@constructive-io/graphql-server 5.32.5 → 5.32.7
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.
- package/esm/middleware/auth.js +13 -6
- package/esm/server.js +6 -5
- package/middleware/auth.d.ts +6 -1
- package/middleware/auth.js +15 -7
- package/package.json +7 -7
- package/server.js +5 -4
package/esm/middleware/auth.js
CHANGED
|
@@ -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
|
|
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=${!!
|
|
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 =
|
|
59
|
-
|
|
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
|
|
172
|
-
|
|
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
|
-
|
|
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);
|
package/middleware/auth.d.ts
CHANGED
|
@@ -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;
|
package/middleware/auth.js
CHANGED
|
@@ -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
|
|
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=${!!
|
|
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 =
|
|
65
|
-
|
|
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.
|
|
3
|
+
"version": "5.32.7",
|
|
4
4
|
"author": "Constructive <developers@constructive.io>",
|
|
5
5
|
"description": "Constructive GraphQL Server",
|
|
6
6
|
"main": "index.js",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"@constructive-io/graphql-env": "^3.37.2",
|
|
48
48
|
"@constructive-io/graphql-types": "^3.36.1",
|
|
49
49
|
"@constructive-io/llm-env": "^0.16.0",
|
|
50
|
-
"@constructive-io/query-builder": "^3.17.
|
|
50
|
+
"@constructive-io/query-builder": "^3.17.2",
|
|
51
51
|
"@constructive-io/s3-utils": "^2.35.0",
|
|
52
52
|
"@constructive-io/url-domains": "^2.32.0",
|
|
53
53
|
"@graphile-contrib/pg-many-to-many": "2.0.0-rc.2",
|
|
@@ -67,11 +67,11 @@
|
|
|
67
67
|
"graphile-build-pg": "5.1.3",
|
|
68
68
|
"graphile-cache": "^4.16.1",
|
|
69
69
|
"graphile-config": "1.1.0",
|
|
70
|
-
"graphile-function-bindings": "^1.18.
|
|
70
|
+
"graphile-function-bindings": "^1.18.3",
|
|
71
71
|
"graphile-plugin-utils": "^1.14.1",
|
|
72
72
|
"graphile-presigned-url-plugin": "^1.23.3",
|
|
73
|
-
"graphile-scoped-introspection": "^0.5.
|
|
74
|
-
"graphile-settings": "^6.27.
|
|
73
|
+
"graphile-scoped-introspection": "^0.5.2",
|
|
74
|
+
"graphile-settings": "^6.27.1",
|
|
75
75
|
"graphile-utils": "5.0.3",
|
|
76
76
|
"graphql": "16.13.0",
|
|
77
77
|
"graphql-upload": "^13.0.0",
|
|
@@ -94,11 +94,11 @@
|
|
|
94
94
|
"@types/request-ip": "^0.0.41",
|
|
95
95
|
"@types/supertest": "^7.2.1",
|
|
96
96
|
"cookie-parser": "^1.4.7",
|
|
97
|
-
"graphile-test": "5.18.
|
|
97
|
+
"graphile-test": "5.18.3",
|
|
98
98
|
"makage": "^0.8.0",
|
|
99
99
|
"nodemon": "^3.1.14",
|
|
100
100
|
"supertest": "^7.2.2",
|
|
101
101
|
"ts-node": "^10.9.2"
|
|
102
102
|
},
|
|
103
|
-
"gitHead": "
|
|
103
|
+
"gitHead": "0b8a4fa54f658bf3b3a8776b98cc5fb373103ce8"
|
|
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
|
|
179
|
-
|
|
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
|
-
|
|
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);
|