forked from ericc-ch/copilot-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
264 lines (231 loc) · 7.92 KB
/
auth.ts
File metadata and controls
264 lines (231 loc) · 7.92 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
import type { Context, MiddlewareHandler } from "hono"
import consola from "consola"
import crypto from "node:crypto"
import type { KeyRow } from "~/services/keys"
import { getConfig } from "~/lib/config-store"
import { HTTPError } from "~/lib/error"
import { checkKeyRateLimit } from "~/lib/rate-limit"
import { audit } from "~/services/audit"
import { findKeyByHash } from "~/services/keys"
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
export type KeyVar = {
key: KeyRow
/**
* Set by authMiddleware when the request arrived with `X-Capi-Debug: 1`
* AND the resolved key is admin-tier. The header itself is always
* stripped (so it never reaches upstream); downstream middleware (e.g.
* the trace capture in middleware/trace.ts) must read this flag instead.
*/
debug_via_header?: boolean
}
// Regex for full sk-cap- token format: prefix + 52 base32 uppercase chars
const SK_CAP_RE = /^sk-cap-[A-Z2-7]{52}$/
// ---------------------------------------------------------------------------
// Sentinel key for --no-auth mode
// ---------------------------------------------------------------------------
const NO_AUTH_SENTINEL: KeyRow = {
id: "__noauth__",
hash: "",
tier: "admin",
label: null,
allowed_models: '["*"]',
rate_limit_override: null,
debug_enabled: 0,
created_at: 0,
revoked_at: null,
}
// ---------------------------------------------------------------------------
// Startup warning
// ---------------------------------------------------------------------------
let _noAuthWarnedOnce = false
/**
* Emit a startup warning when auth is disabled.
* Safe to call unconditionally — reads the config and skips if auth is enabled.
* Idempotent (only warns once per process lifetime).
*/
export function warnNoAuth(): void {
if (_noAuthWarnedOnce || getConfig().features.auth) return
_noAuthWarnedOnce = true
consola.warn(
"\x1B[33m[auth] --no-auth mode: authentication is DISABLED. All requests are accepted as admin.\x1B[0m",
)
}
/** Test-only reset so each test gets a fresh warned state. */
export function _resetNoAuthWarned_TEST_ONLY(): void {
_noAuthWarnedOnce = false
}
// ---------------------------------------------------------------------------
// requireAdmin middleware
// ---------------------------------------------------------------------------
/**
* Hono middleware that enforces admin tier.
* Mount on any route group that requires admin access.
* Usage: `adminRoutes.use("*", requireAdminMiddleware)`
*/
export const requireAdminMiddleware: MiddlewareHandler<{
Variables: KeyVar
}> = async (c, next) => {
const key = c.get("key")
if (key.tier !== "admin") {
return c.json(
{
error: {
message: "Admin tier required",
type: "permission_denied",
code: "permission_denied",
},
},
403,
)
}
await next()
}
/**
* Helper for one-off admin checks in route handlers.
* Returns a 403 Response if the caller is not admin, null otherwise.
* Callers MUST check the return value and return it from the handler.
*/
export function requireAdmin(
c: Context<{ Variables: KeyVar }>,
): Response | null {
const key = c.get("key")
if (key.tier !== "admin") {
return Response.json(
{
error: {
message: "Admin tier required",
type: "permission_denied",
code: "permission_denied",
},
},
{ status: 403 },
)
}
return null
}
// ---------------------------------------------------------------------------
// isModelAllowed
// ---------------------------------------------------------------------------
export function isModelAllowed(
allowedModelsJson: string,
model: string,
): boolean {
let models: unknown
try {
models = JSON.parse(allowedModelsJson)
} catch {
consola.error(
`[auth] Failed to parse allowed_models JSON: ${allowedModelsJson}`,
)
return false
}
if (!Array.isArray(models)) {
consola.error(
`[auth] allowed_models is not an array: ${JSON.stringify(models)}`,
)
return false
}
return models.some(
(m): m is string => typeof m === "string" && (m === "*" || m === model),
)
}
// ---------------------------------------------------------------------------
// Auth middleware helpers
// ---------------------------------------------------------------------------
const AUTH_401_HEADERS = { "WWW-Authenticate": 'Bearer realm="copilot-api"' }
type HonoCtx = Parameters<MiddlewareHandler<{ Variables: KeyVar }>>[0]
function auditReject(c: HonoCtx, hashPrefix?: string): void {
audit({
actor_key_id: "__noauth__",
actor_tier: "system",
action: "auth.reject",
...(hashPrefix !== undefined && { target: hashPrefix }),
ip: c.req.header("x-forwarded-for"),
user_agent: c.req.header("user-agent"),
})
}
function rejectJson(c: HonoCtx, message: string): ReturnType<HonoCtx["json"]> {
return c.json(
{ error: { message, type: "invalid_api_key", code: "invalid_api_key" } },
401,
AUTH_401_HEADERS,
)
}
// ---------------------------------------------------------------------------
// Auth middleware
// ---------------------------------------------------------------------------
export const authMiddleware: MiddlewareHandler<{ Variables: KeyVar }> = async (
c,
next,
) => {
// Strip sensitive client headers BEFORE any branching.
// Note: Bun allows .delete() on server Request.headers (non-spec extension).
// copilotHeaders() always builds its own Headers from state.copilotToken so
// the client's Authorization is never forwarded upstream regardless.
c.req.raw.headers.delete("x-api-key")
c.req.raw.headers.delete("cookie")
if (!getConfig().features.auth) {
c.set("key", NO_AUTH_SENTINEL)
await next()
return
}
const authHeader = c.req.header("Authorization")
if (!authHeader) {
auditReject(c)
return rejectJson(c, "Missing Authorization header")
}
// Case-insensitive scheme extraction per RFC 7235 §2
const bearer =
authHeader.toLowerCase().startsWith("bearer ") ?
authHeader.slice(7)
: authHeader
c.req.raw.headers.delete("authorization")
if (!SK_CAP_RE.test(bearer)) {
const hint =
bearer.startsWith("sk-cap-") ?
"Malformed sk-cap-* key (expected sk-cap- + 52 uppercase base32 chars)"
: "this proxy does not forward your GitHub token; use a sk-cap-* key issued by this server"
consola.warn("[auth] Rejected request: invalid bearer token format")
const prefix = crypto
.createHash("sha256")
.update(bearer)
.digest("hex")
.slice(0, 8)
auditReject(c, prefix)
return rejectJson(c, hint)
}
const hash = crypto.createHash("sha256").update(bearer).digest("hex")
const keyRecord = findKeyByHash(hash)
if (!keyRecord || keyRecord.revoked_at !== null) {
consola.warn("[auth] Rejected request: key not found or revoked")
auditReject(c, hash.slice(0, 8))
return rejectJson(c, "Invalid API key")
}
// Strip X-Capi-Debug unconditionally; only honor for admin-tier keys via context
const debugHeader = c.req.header("x-capi-debug")
c.req.raw.headers.delete("x-capi-debug")
if (debugHeader !== undefined && keyRecord.tier !== "admin") {
consola.warn("[auth] Stripped X-Capi-Debug from client-tier request")
}
// Surface the admin-tier debug-toggle via context so downstream middleware
// (trace.ts) can opt in to capture WITHOUT relying on the header (which
// we've already deleted to avoid leaking the bit upstream).
if (debugHeader === "1" && keyRecord.tier === "admin") {
c.set("debug_via_header", true)
}
try {
checkKeyRateLimit(keyRecord.id, keyRecord.rate_limit_override)
} catch (err) {
if (err instanceof HTTPError) {
return new Response(err.response.body, {
status: err.response.status,
headers: err.response.headers,
})
}
throw err
}
c.set("key", keyRecord)
await next()
}