@defra/lis-hubs-infra-core 0.32.0 → 0.33.0
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/README.md +4 -0
- package/package.json +1 -1
- package/src/logging/index.js +2 -0
- package/src/logging/index.test.js +25 -1
- package/src/spoke.csrf.test.js +108 -0
- package/src/spoke.js +26 -3
package/README.md
CHANGED
|
@@ -121,6 +121,10 @@ sending the spoke the signed-in user's hub JWT, taken from the hub session by `g
|
|
|
121
121
|
the `x-lis-jwt-assertion` request header (the `JWT_ASSERTION_HEADER` constant exported by this package). It forwards only the `cookies_policy` consent cookie (the `COOKIES_POLICY_COOKIE_NAME` constant exported by this package),
|
|
122
122
|
so the spoke's layout can render the cookie banner state. It never sends the hub session cookie, any other
|
|
123
123
|
cookie or the browser's `Authorization` header, and strips any client-supplied copy of that header.
|
|
124
|
+
It sends the session's CSRF token, published by the `lis-csrf` plugin from `@defra/lis-infra-ui-services/csrf`
|
|
125
|
+
at `request.plugins['lis-csrf'].token`, in the `x-lis-csrf-token` header, and strips any client-supplied
|
|
126
|
+
copy. Its routes set `plugins: { 'lis-csrf': { token: false } }`: the hub checks a proxied request's
|
|
127
|
+
origin, and the spoke checks the token in its body, which the hub streams through unread.
|
|
124
128
|
|
|
125
129
|
```js
|
|
126
130
|
import { createProxyPlugin } from '@defra/lis-hubs-infra-core'
|
package/package.json
CHANGED
package/src/logging/index.js
CHANGED
|
@@ -20,6 +20,8 @@ const redactPaths = [
|
|
|
20
20
|
'req.headers.cookie',
|
|
21
21
|
// The signed-in user's hub JWT, JWT_ASSERTION_HEADER in spoke.js.
|
|
22
22
|
'req.headers["x-lis-jwt-assertion"]',
|
|
23
|
+
// The session's CSRF token, CSRF_TOKEN_HEADER in spoke.js.
|
|
24
|
+
'req.headers["x-lis-csrf-token"]',
|
|
23
25
|
'req.query.error_description',
|
|
24
26
|
'res.headers'
|
|
25
27
|
]
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { describe, expect, test, vi } from 'vitest'
|
|
2
2
|
|
|
3
3
|
import { logger } from './index.js'
|
|
4
|
-
import { JWT_ASSERTION_HEADER } from '../spoke.js'
|
|
4
|
+
import { CSRF_TOKEN_HEADER, JWT_ASSERTION_HEADER } from '../spoke.js'
|
|
5
5
|
|
|
6
6
|
describe('Logger', () => {
|
|
7
7
|
describe('serviceName', () => {
|
|
@@ -90,6 +90,30 @@ describe('Logger', () => {
|
|
|
90
90
|
expect(logLine.toString()).not.toContain('secret-assertion')
|
|
91
91
|
})
|
|
92
92
|
|
|
93
|
+
test('it removes the CSRF token header from logged output', () => {
|
|
94
|
+
// Arrange
|
|
95
|
+
const freshLogging = new logger.constructor()
|
|
96
|
+
const writeSpy = vi
|
|
97
|
+
.spyOn(process.stdout, 'write')
|
|
98
|
+
.mockImplementation(() => true)
|
|
99
|
+
|
|
100
|
+
// Act
|
|
101
|
+
freshLogging.info({
|
|
102
|
+
req: {
|
|
103
|
+
headers: {
|
|
104
|
+
[CSRF_TOKEN_HEADER]: 'secret-csrf-token',
|
|
105
|
+
accept: 'text/html'
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
})
|
|
109
|
+
const [logLine] = writeSpy.mock.calls[0]
|
|
110
|
+
writeSpy.mockRestore()
|
|
111
|
+
|
|
112
|
+
// Assert
|
|
113
|
+
expect(logLine.toString()).not.toContain('secret-csrf-token')
|
|
114
|
+
expect(logLine.toString()).toContain('text/html')
|
|
115
|
+
})
|
|
116
|
+
|
|
93
117
|
test('it removes an identity provider error_description from the request query', () => {
|
|
94
118
|
// Arrange
|
|
95
119
|
const freshLogging = new logger.constructor()
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { afterEach, beforeAll, describe, expect, test, vi } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { getServiceToken } from './service-token/issuer.js'
|
|
4
|
+
import { CSRF_TOKEN_HEADER, JWT_ASSERTION_HEADER, Spoke } from './spoke.js'
|
|
5
|
+
|
|
6
|
+
vi.mock('./service-token/issuer.js')
|
|
7
|
+
vi.mock('./circuit-breaker.js')
|
|
8
|
+
|
|
9
|
+
const mocks = {
|
|
10
|
+
getServiceToken: vi.mocked(getServiceToken)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function createSpoke() {
|
|
14
|
+
return new Spoke({
|
|
15
|
+
moduleName: 'cattle-home',
|
|
16
|
+
mounts: ['/holdings/{cph*3}/cattle'],
|
|
17
|
+
port: 3222,
|
|
18
|
+
environment: 'local',
|
|
19
|
+
timeout: 30000,
|
|
20
|
+
getUserToken: () => 'hub.jwt.value'
|
|
21
|
+
})
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function createRequest({ headers = {}, plugins = {} } = {}) {
|
|
25
|
+
const url = new URL('http://hub.test/holdings/12/345/0001/cattle/animals')
|
|
26
|
+
return { path: url.pathname, url, headers, plugins }
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
describe('Spoke CSRF handling', () => {
|
|
30
|
+
beforeAll(() => {
|
|
31
|
+
mocks.getServiceToken.mockResolvedValue('service-token')
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
afterEach(() => {
|
|
35
|
+
vi.clearAllMocks()
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
test('it keeps the origin check on its routes and leaves the token to the spoke', () => {
|
|
39
|
+
// Act
|
|
40
|
+
const { routes } = createSpoke()
|
|
41
|
+
|
|
42
|
+
// Assert
|
|
43
|
+
expect(routes[0].options.plugins).toEqual({
|
|
44
|
+
'lis-csrf': { token: false }
|
|
45
|
+
})
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
test('it sends the session CSRF token in the header', async () => {
|
|
49
|
+
// Arrange
|
|
50
|
+
const spoke = createSpoke()
|
|
51
|
+
const request = createRequest({
|
|
52
|
+
plugins: { 'lis-csrf': { token: 'csrf-token' } }
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
// Act
|
|
56
|
+
const mapped = await spoke.mapUri(request)
|
|
57
|
+
|
|
58
|
+
// Assert
|
|
59
|
+
expect(mapped.headers).toEqual({
|
|
60
|
+
authorization: 'Bearer service-token',
|
|
61
|
+
[JWT_ASSERTION_HEADER]: 'hub.jwt.value',
|
|
62
|
+
[CSRF_TOKEN_HEADER]: 'csrf-token'
|
|
63
|
+
})
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
test('it drops a CSRF token header sent by the browser and uses the session token', async () => {
|
|
67
|
+
// Arrange
|
|
68
|
+
const spoke = createSpoke()
|
|
69
|
+
const request = createRequest({
|
|
70
|
+
headers: { [CSRF_TOKEN_HEADER]: 'evil', accept: 'text/html' },
|
|
71
|
+
plugins: { 'lis-csrf': { token: 'csrf-token' } }
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
// Act
|
|
75
|
+
const mapped = await spoke.mapUri(request)
|
|
76
|
+
|
|
77
|
+
// Assert
|
|
78
|
+
expect(mapped.headers[CSRF_TOKEN_HEADER]).toBe('csrf-token')
|
|
79
|
+
expect(request.headers).toEqual({ accept: 'text/html' })
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
test.each([
|
|
83
|
+
['no CSRF plugin state', {}],
|
|
84
|
+
['no token', { 'lis-csrf': {} }],
|
|
85
|
+
['an empty token', { 'lis-csrf': { token: '' } }],
|
|
86
|
+
['a token that is not a string', { 'lis-csrf': { token: 42 } }]
|
|
87
|
+
])(
|
|
88
|
+
'it drops a CSRF token header sent by the browser and sends none when there is %s',
|
|
89
|
+
async (_description, plugins) => {
|
|
90
|
+
// Arrange
|
|
91
|
+
const spoke = createSpoke()
|
|
92
|
+
const request = createRequest({
|
|
93
|
+
headers: { [CSRF_TOKEN_HEADER]: 'evil' },
|
|
94
|
+
plugins
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
// Act
|
|
98
|
+
const mapped = await spoke.mapUri(request)
|
|
99
|
+
|
|
100
|
+
// Assert
|
|
101
|
+
expect(mapped.headers).toEqual({
|
|
102
|
+
authorization: 'Bearer service-token',
|
|
103
|
+
[JWT_ASSERTION_HEADER]: 'hub.jwt.value'
|
|
104
|
+
})
|
|
105
|
+
expect(request.headers).toEqual({})
|
|
106
|
+
}
|
|
107
|
+
)
|
|
108
|
+
})
|
package/src/spoke.js
CHANGED
|
@@ -21,6 +21,16 @@ export const JWT_ASSERTION_HEADER = 'x-lis-jwt-assertion'
|
|
|
21
21
|
*/
|
|
22
22
|
export const COOKIES_POLICY_COOKIE_NAME = 'cookies_policy'
|
|
23
23
|
|
|
24
|
+
/**
|
|
25
|
+
* The CSRF plugin's name, which keys its route options and request.plugins,
|
|
26
|
+
* and the header a hub sends a spoke the session's CSRF token in. Both must
|
|
27
|
+
* match CSRF_PLUGIN_NAME and CSRF_TOKEN_HEADER in
|
|
28
|
+
* @defra/lis-infra-ui-services/csrf, which depends on this package, so this
|
|
29
|
+
* one cannot import them.
|
|
30
|
+
*/
|
|
31
|
+
const CSRF_PLUGIN_NAME = 'lis-csrf'
|
|
32
|
+
export const CSRF_TOKEN_HEADER = 'x-lis-csrf-token'
|
|
33
|
+
|
|
24
34
|
const INTERNAL_SERVER_ERROR = 500
|
|
25
35
|
const BAD_GATEWAY = 502
|
|
26
36
|
const GENERIC_ERROR_MESSAGE = 'An internal server error occurred'
|
|
@@ -113,6 +123,14 @@ export class Spoke {
|
|
|
113
123
|
return this.#mounts.map((mount) => ({
|
|
114
124
|
method: '*',
|
|
115
125
|
path: `${mount}/{path*}`,
|
|
126
|
+
options: {
|
|
127
|
+
plugins: {
|
|
128
|
+
// The body is streamed through untouched, so the hub cannot read
|
|
129
|
+
// the token in it; the spoke checks it against the token sent in
|
|
130
|
+
// CSRF_TOKEN_HEADER. The hub still checks the origin.
|
|
131
|
+
[CSRF_PLUGIN_NAME]: { token: false }
|
|
132
|
+
}
|
|
133
|
+
},
|
|
116
134
|
handler: {
|
|
117
135
|
proxy: {
|
|
118
136
|
passThrough: true,
|
|
@@ -140,6 +158,7 @@ export class Spoke {
|
|
|
140
158
|
async mapUri(request) {
|
|
141
159
|
const serviceToken = await this.#getServiceToken()
|
|
142
160
|
const userToken = this.#getUserToken(request)
|
|
161
|
+
const csrfToken = request.plugins?.[CSRF_PLUGIN_NAME]?.token
|
|
143
162
|
// Nothing may throw between here and h2o2 making the call, or a
|
|
144
163
|
// half-open probe would never report back.
|
|
145
164
|
this.#breaker.assertCallAllowed()
|
|
@@ -147,9 +166,10 @@ export class Spoke {
|
|
|
147
166
|
// passThrough copies request.headers itself and merges these headers on
|
|
148
167
|
// top, so a header can only be withheld from the spoke by removing it
|
|
149
168
|
// from the request. The spoke only gets the signed-in user's hub JWT,
|
|
150
|
-
// taken from the hub session and sent in a header,
|
|
151
|
-
//
|
|
152
|
-
// or a client-supplied
|
|
169
|
+
// taken from the hub session and sent in a header, the session's CSRF
|
|
170
|
+
// token, likewise, and the cookie-consent cookie, never the hub session
|
|
171
|
+
// cookie, the inbound authorization header or a client-supplied
|
|
172
|
+
// assertion or CSRF token header.
|
|
153
173
|
const forwardedCookies = pickCookie(
|
|
154
174
|
request.headers.cookie,
|
|
155
175
|
COOKIES_POLICY_COOKIE_NAME
|
|
@@ -157,6 +177,7 @@ export class Spoke {
|
|
|
157
177
|
delete request.headers.authorization
|
|
158
178
|
delete request.headers.cookie
|
|
159
179
|
delete request.headers[JWT_ASSERTION_HEADER]
|
|
180
|
+
delete request.headers[CSRF_TOKEN_HEADER]
|
|
160
181
|
|
|
161
182
|
return {
|
|
162
183
|
uri: `${this.#baseUri}${request.path}${request.url.search}`,
|
|
@@ -164,6 +185,8 @@ export class Spoke {
|
|
|
164
185
|
authorization: `Bearer ${serviceToken}`,
|
|
165
186
|
...(typeof userToken === 'string' &&
|
|
166
187
|
userToken && { [JWT_ASSERTION_HEADER]: userToken }),
|
|
188
|
+
...(typeof csrfToken === 'string' &&
|
|
189
|
+
csrfToken && { [CSRF_TOKEN_HEADER]: csrfToken }),
|
|
167
190
|
...(forwardedCookies && { cookie: forwardedCookies })
|
|
168
191
|
}
|
|
169
192
|
}
|