-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathoauth.test.ts
More file actions
396 lines (339 loc) · 11.4 KB
/
oauth.test.ts
File metadata and controls
396 lines (339 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!/usr/bin/env node
import { test } from 'node:test'
import assert from 'node:assert/strict'
import { once } from 'node:events'
import { createServer, type IncomingMessage, type ServerResponse } from 'node:http'
import type { AddressInfo } from 'node:net'
import { spawn } from 'node:child_process'
import { setTimeout as delay } from 'node:timers/promises'
import { join } from 'node:path'
import { Client } from '@modelcontextprotocol/sdk/client/index.js'
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js'
const serverPath = join(import.meta.dirname, 'index.ts')
const inheritedEnv = Object.fromEntries(
Object.entries(process.env).filter(([, value]) => value !== undefined)
) as Record<string, string>
const oauthWellKnownPath = '/.well-known/oauth-authorization-server'
const protectedResourceMetadataPath = '/.well-known/oauth-protected-resource'
const mockIntrospectionResponses: Record<string, Record<string, unknown>> = {
'token-without-exp': {
active: true,
client_id: 'oauth-test-client',
scope: 'packages:list'
},
'token-with-wrong-scope': {
active: true,
client_id: 'oauth-test-client',
scope: 'packages:write'
}
}
function closeHttpServer (server: ReturnType<typeof createServer>): Promise<void> {
return new Promise((resolve, reject) => {
server.close((error) => {
if (error) {
reject(error)
return
}
resolve()
})
})
}
async function getFreePort (): Promise<number> {
const server = createServer()
server.listen(0, '127.0.0.1')
await once(server, 'listening')
const address = server.address() as AddressInfo
await closeHttpServer(server)
return address.port
}
async function readRequestBody (req: IncomingMessage): Promise<string> {
let body = ''
for await (const chunk of req) {
body += chunk
}
return body
}
async function assertOAuthErrorResponse (
response: Response,
serverBaseUrl: string,
expected: {
status: number
error: string
errorDescription: string
}
): Promise<void> {
const body = await response.json() as {
error?: string
error_description?: string
}
assert.equal(response.status, expected.status)
assert.equal(body.error, expected.error)
assert.equal(body.error_description, expected.errorDescription)
assert.equal(
response.headers.get('www-authenticate'),
`Bearer error="${expected.error}", error_description="${expected.errorDescription}", resource_metadata="${serverBaseUrl}${protectedResourceMetadataPath}"`
)
}
async function startMockIssuer (): Promise<{
baseUrl: string
close: () => Promise<void>
}> {
const server = createServer(async (req: IncomingMessage, res: ServerResponse) => {
const baseUrl = `http://${req.headers.host}`
const url = new URL(req.url || '/', baseUrl)
if (req.method === 'GET' && url.pathname === oauthWellKnownPath) {
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({
issuer: baseUrl,
authorization_endpoint: `${baseUrl}/authorize`,
token_endpoint: `${baseUrl}/token`,
introspection_endpoint: `${baseUrl}/introspect`
}))
return
}
if (req.method === 'POST' && url.pathname === '/introspect') {
const body = await readRequestBody(req)
const token = new URLSearchParams(body).get('token')
const introspectionResponse = token ? mockIntrospectionResponses[token] : undefined
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(introspectionResponse || { active: false }))
return
}
res.writeHead(404)
res.end('Not found')
})
server.listen(0, '127.0.0.1')
await once(server, 'listening')
const address = server.address() as AddressInfo
return {
baseUrl: `http://127.0.0.1:${address.port}`,
close: async () => { await closeHttpServer(server) }
}
}
async function stopChildProcess (child: ReturnType<typeof spawn>): Promise<void> {
if (child.exitCode !== null) {
return
}
let exited = false
const onExit = once(child, 'exit').then(() => {
exited = true
})
child.kill('SIGTERM')
await Promise.race([
onExit,
delay(3000).then(() => {
if (exited || child.exitCode !== null) {
return
}
child.kill('SIGKILL')
return onExit
})
])
}
async function waitForHealth (
baseUrl: string,
child: ReturnType<typeof spawn>,
getOutput: () => string
): Promise<void> {
const timeoutAt = Date.now() + 5000
while (Date.now() < timeoutAt) {
if (child.exitCode !== null) {
throw new Error(`HTTP server exited before becoming ready:\n${getOutput()}`)
}
try {
const response = await fetch(`${baseUrl}/health`)
if (response.ok) {
return
}
} catch {}
await delay(100)
}
throw new Error(`Timed out waiting for HTTP server readiness:\n${getOutput()}`)
}
async function startOAuthHttpServer (
issuerBaseUrl: string,
extraEnv: Record<string, string> = {}
): Promise<{
baseUrl: string
close: () => Promise<void>
}> {
const port = await getFreePort()
let output = ''
const child = spawn('node', ['--experimental-strip-types', serverPath], {
cwd: import.meta.dirname,
env: {
...inheritedEnv,
MCP_HTTP_MODE: 'true',
MCP_PORT: String(port),
SOCKET_OAUTH_ISSUER: issuerBaseUrl,
SOCKET_OAUTH_INTROSPECTION_CLIENT_ID: 'oauth-test-client-id',
SOCKET_OAUTH_INTROSPECTION_CLIENT_SECRET: 'oauth-test-client-secret',
...extraEnv
},
stdio: ['ignore', 'pipe', 'pipe']
})
child.stdout.on('data', (chunk: Buffer) => { output += chunk.toString() })
child.stderr.on('data', (chunk: Buffer) => { output += chunk.toString() })
const baseUrl = `http://127.0.0.1:${port}`
await waitForHealth(baseUrl, child, () => output)
return {
baseUrl,
close: async () => { await stopChildProcess(child) }
}
}
test('stdio mode ignores partial OAuth config', async (t) => {
const transport = new StdioClientTransport({
command: 'node',
args: ['--experimental-strip-types', serverPath],
env: {
...inheritedEnv,
SOCKET_API_KEY: 'test-api-key',
SOCKET_OAUTH_ISSUER: 'https://issuer.example.test'
}
})
const client = new Client(
{ name: 'oauth-stdio-test-client', version: '1.0.0' },
{ capabilities: {} }
)
t.after(async () => {
await client.close().catch(() => {})
})
await client.connect(transport)
const tools = await client.listTools()
assert.ok(tools.tools.some(tool => tool.name === 'depscore'))
})
test('HTTP OAuth metadata and auth semantics', async (t) => {
const issuer = await startMockIssuer()
const server = await startOAuthHttpServer(issuer.baseUrl)
t.after(async () => {
await server.close()
await issuer.close()
})
await t.test('does not expose upstream authorization server metadata', async () => {
const response = await fetch(`${server.baseUrl}${oauthWellKnownPath}`)
assert.equal(response.status, 404)
assert.match(await response.text(), /not found/i)
})
await t.test('serves protected resource metadata pointing to the issuer', async () => {
const response = await fetch(`${server.baseUrl}${protectedResourceMetadataPath}`)
const metadata = await response.json() as {
authorization_servers?: string[]
resource?: string
}
assert.equal(response.status, 200)
assert.deepEqual(metadata.authorization_servers, [issuer.baseUrl])
assert.equal(metadata.resource, `${server.baseUrl}/`)
})
await t.test('ignores forwarded host and proto headers unless TRUST_PROXY is enabled', async () => {
const response = await fetch(`${server.baseUrl}${protectedResourceMetadataPath}`, {
headers: {
'X-Forwarded-Host': 'attacker.example.com',
'X-Forwarded-Proto': 'https'
}
})
const metadata = await response.json() as { resource?: string }
assert.equal(response.status, 200)
assert.equal(metadata.resource, `${server.baseUrl}/`)
const unauthenticatedResponse = await fetch(`${server.baseUrl}/`, {
method: 'POST',
headers: {
'X-Forwarded-Host': 'attacker.example.com',
'X-Forwarded-Proto': 'https'
}
})
await assertOAuthErrorResponse(unauthenticatedResponse, server.baseUrl, {
status: 401,
error: 'invalid_request',
errorDescription: 'Missing Authorization header'
})
})
await t.test('returns invalid_request when the Authorization header is missing', async () => {
const response = await fetch(`${server.baseUrl}/`, { method: 'POST' })
await assertOAuthErrorResponse(response, server.baseUrl, {
status: 401,
error: 'invalid_request',
errorDescription: 'Missing Authorization header'
})
})
await t.test('returns invalid_token when introspection reports an inactive token', async () => {
const response = await fetch(`${server.baseUrl}/`, {
method: 'POST',
headers: {
Authorization: 'Bearer inactive-token'
}
})
await assertOAuthErrorResponse(response, server.baseUrl, {
status: 401,
error: 'invalid_token',
errorDescription: 'Invalid or expired token'
})
})
await t.test('returns insufficient_scope when the token is missing required scopes', async () => {
const response = await fetch(`${server.baseUrl}/`, {
method: 'POST',
headers: {
Authorization: 'Bearer token-with-wrong-scope'
}
})
await assertOAuthErrorResponse(response, server.baseUrl, {
status: 403,
error: 'insufficient_scope',
errorDescription: 'Missing required scopes: packages:list'
})
})
await t.test('accepts an active token even when introspection omits exp', async () => {
const response = await fetch(`${server.baseUrl}/`, {
method: 'POST',
headers: {
Authorization: 'Bearer token-without-exp',
'Content-Type': 'application/json',
Accept: 'application/json, text/event-stream'
},
body: JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'initialize',
params: {
protocolVersion: '0.1.0',
capabilities: {},
clientInfo: {
name: 'oauth-http-test-client',
version: '1.0.0'
}
}
})
})
const body = await response.json() as { result?: { serverInfo?: { name?: string } } }
assert.equal(response.status, 200)
assert.equal(body.result?.serverInfo?.name, 'socket')
})
})
test('TRUST_PROXY enables forwarded host and proto for OAuth metadata URLs', async (t) => {
const issuer = await startMockIssuer()
const server = await startOAuthHttpServer(issuer.baseUrl, { TRUST_PROXY: 'true' })
t.after(async () => {
await server.close()
await issuer.close()
})
const response = await fetch(`${server.baseUrl}${protectedResourceMetadataPath}`, {
headers: {
'X-Forwarded-Host': 'proxy.example.com',
'X-Forwarded-Proto': 'https'
}
})
const metadata = await response.json() as { resource?: string }
assert.equal(response.status, 200)
assert.equal(metadata.resource, 'https://proxy.example.com/')
const unauthenticatedResponse = await fetch(`${server.baseUrl}/`, {
method: 'POST',
headers: {
'X-Forwarded-Host': 'proxy.example.com',
'X-Forwarded-Proto': 'https'
}
})
await assertOAuthErrorResponse(unauthenticatedResponse, 'https://proxy.example.com', {
status: 401,
error: 'invalid_request',
errorDescription: 'Missing Authorization header'
})
})