@goodandready/dsh-kanban 0.2.34 → 0.2.36
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/lib/board-tool.js +8 -0
- package/lib/client.js +3800 -3587
- package/lib/config.js +10 -0
- package/lib/dependencies.js +175 -0
- package/lib/index.js +110 -1255
- package/lib/routes/board.js +286 -0
- package/lib/routes/security.js +274 -0
- package/lib/routes/task-actions.js +418 -0
- package/lib/routes/task-worktree.js +107 -0
- package/lib/routes/task.js +237 -0
- package/lib/routes.js +73 -1209
- package/lib/server/route-handlers.js +367 -0
- package/lib/server/services.js +225 -0
- package/lib/server/task-handler.js +569 -0
- package/lib/server/timers.js +204 -0
- package/lib/store.js +23 -2
- package/lib/time-machine.js +214 -0
- package/lib/watchdog.js +169 -0
- package/package.json +1 -1
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import { COLUMN_ORDER, columnsOf, wipLimitField, columnNamesOf } from '../config.js'
|
|
2
|
+
import { parsePlan, planItems, planProgress, taskState, queuePosition } from '../plan.js'
|
|
3
|
+
import { columnStats, doneWithin, stale } from '../metrics.js'
|
|
4
|
+
import { isStale } from '../time.js'
|
|
5
|
+
import { facetsOf, facetsOfTask, groupByRepo, sortByLife } from '../filters.js'
|
|
6
|
+
import { computeNextRun } from '../cron.js'
|
|
7
|
+
import { computeBlocksMap, checkTaskBlocked } from '../dependencies.js'
|
|
8
|
+
|
|
9
|
+
export function withPlan(task, liveOf, config, now, sameSession) {
|
|
10
|
+
const plan = parsePlan(task.plan)
|
|
11
|
+
// Registry error implies agent unavailable; do not fail whole board render.
|
|
12
|
+
let live
|
|
13
|
+
try {
|
|
14
|
+
if (typeof liveOf === 'function' && task.sessionId) live = liveOf(task.sessionId)
|
|
15
|
+
} catch { live = undefined }
|
|
16
|
+
const liveStatus = typeof live === 'object' && live !== null ? live.status : live
|
|
17
|
+
const liveStep = typeof live === 'object' && live !== null
|
|
18
|
+
? (live.step || live.activity || live.stateDetail || '')
|
|
19
|
+
: ''
|
|
20
|
+
|
|
21
|
+
const costMetrics = task.costMetrics || null
|
|
22
|
+
const costLimitExceeded = (config?.costMeterEnabled !== false && Number(config?.taskCostLimit) > 0 && costMetrics?.costUsd)
|
|
23
|
+
? costMetrics.costUsd >= Number(config.taskCostLimit)
|
|
24
|
+
: Boolean(task.costLimitExceeded)
|
|
25
|
+
|
|
26
|
+
return {
|
|
27
|
+
...task,
|
|
28
|
+
plan: { items: planItems(plan), progress: planProgress(plan) },
|
|
29
|
+
state: taskState({ task, live: liveStatus }),
|
|
30
|
+
liveStep: liveStep || task.liveStep || '',
|
|
31
|
+
costMetrics,
|
|
32
|
+
costLimitExceeded,
|
|
33
|
+
// Queue position calculated dynamically across active tasks in same session.
|
|
34
|
+
queuePos: queuePosition(task, sameSession),
|
|
35
|
+
// Label facets extracted on server for consistent client filtering.
|
|
36
|
+
facets: facetsOfTask(task),
|
|
37
|
+
// Stale calculation performed on host where configuration resides.
|
|
38
|
+
stale: isStale({
|
|
39
|
+
now,
|
|
40
|
+
updatedAt: task.updatedAt,
|
|
41
|
+
sessionId: task.sessionId,
|
|
42
|
+
afterMinutes: config?.staleAfterMin,
|
|
43
|
+
}),
|
|
44
|
+
// Overdue calculated on host using server clock.
|
|
45
|
+
overdue: typeof task.dueAt === 'number' && task.dueAt > 0 && task.dueAt < now,
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Order tasks: by column, grouped by repository, with active tasks on top. */
|
|
50
|
+
function orderForBoard(tasks, columns) {
|
|
51
|
+
const out = []
|
|
52
|
+
for (const column of columns) {
|
|
53
|
+
const inColumn = tasks.filter((t) => t.column === column)
|
|
54
|
+
for (const group of groupByRepo(sortByLife(inColumn))) out.push(...group.tasks)
|
|
55
|
+
}
|
|
56
|
+
// Retain tasks in columns not visible on current board to prevent data loss.
|
|
57
|
+
const seen = new Set(out.map((t) => t.id))
|
|
58
|
+
for (const task of tasks) if (!seen.has(task.id)) out.push(task)
|
|
59
|
+
return out
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Build board response payload: columns in canonical order, tasks, and limits.
|
|
64
|
+
*/
|
|
65
|
+
export function buildBoard({ store, config, board = 'main', repo, liveOf, sync, projectRoot, costMeterAvailable = false }) {
|
|
66
|
+
// Uniform timestamp across snapshot calculation.
|
|
67
|
+
const now = Date.now()
|
|
68
|
+
// Tasks are sorted and grouped on server: repo grouping with active items first.
|
|
69
|
+
const raw = store.listTasks({ board, repo })
|
|
70
|
+
// Queue position scoped per session.
|
|
71
|
+
const bySession = new Map()
|
|
72
|
+
for (const task of raw) {
|
|
73
|
+
const id = typeof task.sessionId === 'string' ? task.sessionId : ''
|
|
74
|
+
if (id === '') continue
|
|
75
|
+
if (!bySession.has(id)) bySession.set(id, [])
|
|
76
|
+
bySession.get(id).push(task)
|
|
77
|
+
}
|
|
78
|
+
const blocksMap = computeBlocksMap(raw)
|
|
79
|
+
const tasks = orderForBoard(
|
|
80
|
+
raw.map((task) => {
|
|
81
|
+
const enriched = withPlan(task, liveOf, config, now, bySession.get(task.sessionId) ?? [])
|
|
82
|
+
if (task.isEpic) {
|
|
83
|
+
enriched.subtasksCount = store.countSubtasks(task.id)
|
|
84
|
+
}
|
|
85
|
+
enriched.blocks = blocksMap[task.id] || []
|
|
86
|
+
enriched.isBlocked = checkTaskBlocked(task, store).isBlocked
|
|
87
|
+
return enriched
|
|
88
|
+
}),
|
|
89
|
+
columnsOf(store.boardKind(board)),
|
|
90
|
+
)
|
|
91
|
+
const kind = store.boardKind(board)
|
|
92
|
+
// Column custom names sent from server; locale translation handled by client.
|
|
93
|
+
const names = columnNamesOf(config)
|
|
94
|
+
const columns = columnsOf(kind).map((id) => {
|
|
95
|
+
const count = tasks.filter((t) => t.column === id).length
|
|
96
|
+
const limit = wipLimitField(config, id)
|
|
97
|
+
return { id, name: names[id], count, limit, overLimit: limit !== undefined && count > limit }
|
|
98
|
+
})
|
|
99
|
+
// Filter counts calculated over all tasks on board.
|
|
100
|
+
return {
|
|
101
|
+
board, kind, now, boards: store.listBoards(), columns, tasks,
|
|
102
|
+
// Resolved project root {path, set} computed on host.
|
|
103
|
+
projectRoot: projectRoot ?? { path: '', set: false },
|
|
104
|
+
|
|
105
|
+
facets: facetsOf(tasks),
|
|
106
|
+
// Sync telemetry state attached directly to board snapshot.
|
|
107
|
+
sync: sync ?? { state: 'never' },
|
|
108
|
+
costMeterAvailable: Boolean(costMeterAvailable),
|
|
109
|
+
remoteMode: Boolean(config?.remoteMode),
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Card move transition. WIP limit is advisory: move executes, and exceeded status
|
|
115
|
+
* is returned for UI highlighting.
|
|
116
|
+
*/
|
|
117
|
+
|
|
118
|
+
export function boardMetrics({ store, board = 'main', now = Date.now(), staleDays = 14 }) {
|
|
119
|
+
const tasks = store.listTasks({ board })
|
|
120
|
+
const byTask = new Map()
|
|
121
|
+
for (const row of store.listAllTransitions()) {
|
|
122
|
+
if (!byTask.has(row.taskId)) byTask.set(row.taskId, [])
|
|
123
|
+
byTask.get(row.taskId).push(row)
|
|
124
|
+
}
|
|
125
|
+
const rows = tasks.map((task) => ({ task, moves: byTask.get(task.id) ?? [] }))
|
|
126
|
+
const day = 24 * 60 * 60 * 1000
|
|
127
|
+
|
|
128
|
+
return {
|
|
129
|
+
tasks: tasks.length,
|
|
130
|
+
columns: columnStats(rows, now),
|
|
131
|
+
done: {
|
|
132
|
+
week: doneWithin(rows, now - 7 * day, now),
|
|
133
|
+
month: doneWithin(rows, now - 30 * day, now),
|
|
134
|
+
},
|
|
135
|
+
staleDays,
|
|
136
|
+
stale: stale(tasks, now, staleDays * day).slice(0, 50),
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Check whether a task can be resumed.
|
|
142
|
+
* Eligible when task has recorded sessionId but no active agent is running.
|
|
143
|
+
*/
|
|
144
|
+
|
|
145
|
+
export function exportBoard({ store }) {
|
|
146
|
+
return store.exportAll()
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Restore board from backup snapshot (idempotent; skips existing IDs).
|
|
151
|
+
*
|
|
152
|
+
* @returns {{imported: number, skipped: number, error?: string}}
|
|
153
|
+
*/
|
|
154
|
+
export function importBoard({ store, input }) {
|
|
155
|
+
let backupMeta = undefined
|
|
156
|
+
if (typeof store.backupDatabase === 'function') {
|
|
157
|
+
try {
|
|
158
|
+
backupMeta = store.backupDatabase()
|
|
159
|
+
} catch { /* backup best effort */ }
|
|
160
|
+
}
|
|
161
|
+
if (input === null || typeof input !== 'object') return { error: 'bad-import', status: 400 }
|
|
162
|
+
if (input.version !== 1) return { error: 'bad-import-version', status: 400 }
|
|
163
|
+
if (!Array.isArray(input.tasks)) return { error: 'bad-import', status: 400 }
|
|
164
|
+
|
|
165
|
+
let imported = 0
|
|
166
|
+
let skipped = 0
|
|
167
|
+
|
|
168
|
+
const doImport = () => {
|
|
169
|
+
for (const board of Array.isArray(input.boards) ? input.boards : []) {
|
|
170
|
+
if (typeof board?.id !== 'string' || board.id === '') continue
|
|
171
|
+
store.createBoard({
|
|
172
|
+
id: board.id,
|
|
173
|
+
title: board.title,
|
|
174
|
+
kind: board.kind,
|
|
175
|
+
position: board.position,
|
|
176
|
+
createdAt: board.createdAt,
|
|
177
|
+
})
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
for (const task of input.tasks) {
|
|
181
|
+
if (typeof task?.id !== 'string' || task.id === '') continue
|
|
182
|
+
if (store.getTask(task.id) !== undefined) { skipped += 1; continue }
|
|
183
|
+
if (typeof store.restoreTask === 'function') {
|
|
184
|
+
store.restoreTask(task)
|
|
185
|
+
} else {
|
|
186
|
+
store.createTask(task)
|
|
187
|
+
}
|
|
188
|
+
imported += 1
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
for (const trans of Array.isArray(input.transitions) ? input.transitions : []) {
|
|
192
|
+
if (typeof trans?.taskId !== 'string' || trans.taskId === '') continue
|
|
193
|
+
if (typeof store.restoreTransition === 'function') {
|
|
194
|
+
store.restoreTransition(trans)
|
|
195
|
+
} else if (typeof store.addTransition === 'function') {
|
|
196
|
+
store.addTransition(trans)
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (typeof store.withTransaction === 'function') {
|
|
202
|
+
store.withTransaction(doImport)
|
|
203
|
+
} else {
|
|
204
|
+
doImport()
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return { imported, skipped, backup: backupMeta?.backup }
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Export tasks to Excel-compatible CSV with UTF-8 BOM (#223).
|
|
212
|
+
*
|
|
213
|
+
* @param {{store: object, board?: string}} options
|
|
214
|
+
* @returns {string} CSV string with UTF-8 BOM prefix
|
|
215
|
+
*/
|
|
216
|
+
export function exportTasksCsv({ store, board = 'main' }) {
|
|
217
|
+
const tasks = store.listTasks({ board })
|
|
218
|
+
const headers = [
|
|
219
|
+
'ID',
|
|
220
|
+
'Title',
|
|
221
|
+
'Column',
|
|
222
|
+
'Priority',
|
|
223
|
+
'Repository',
|
|
224
|
+
'Issue #',
|
|
225
|
+
'Assignee',
|
|
226
|
+
'Model',
|
|
227
|
+
'Provider',
|
|
228
|
+
'Reasoning Effort',
|
|
229
|
+
'Workspace',
|
|
230
|
+
'Branch',
|
|
231
|
+
'Created',
|
|
232
|
+
'Updated',
|
|
233
|
+
]
|
|
234
|
+
|
|
235
|
+
function escapeCsv(val) {
|
|
236
|
+
if (val === null || val === undefined) return ''
|
|
237
|
+
let str = String(val)
|
|
238
|
+
// OWASP CSV Injection mitigation (#340)
|
|
239
|
+
const trimmed = str.replace(/^[\s\t\r\n]+/, '')
|
|
240
|
+
if (/^[=+\-@\t\r]/.test(str) || (trimmed.length > 0 && /^[=+\-@]/.test(trimmed))) {
|
|
241
|
+
str = `'${str}`
|
|
242
|
+
}
|
|
243
|
+
if (str.includes(',') || str.includes('"') || str.includes('\n') || str.includes('\r')) {
|
|
244
|
+
return `"${str.replace(/"/g, '""')}"`
|
|
245
|
+
}
|
|
246
|
+
return str
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function formatDate(ts) {
|
|
250
|
+
if (!ts || typeof ts !== 'number') return ''
|
|
251
|
+
try {
|
|
252
|
+
return new Date(ts).toISOString().replace('T', ' ').slice(0, 19)
|
|
253
|
+
} catch {
|
|
254
|
+
return ''
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const rows = [headers.join(',')]
|
|
259
|
+
for (const t of tasks) {
|
|
260
|
+
const row = [
|
|
261
|
+
escapeCsv(t.id),
|
|
262
|
+
escapeCsv(t.title),
|
|
263
|
+
escapeCsv(t.column),
|
|
264
|
+
escapeCsv(t.priority),
|
|
265
|
+
escapeCsv(t.repo),
|
|
266
|
+
escapeCsv(t.issueNumber ?? ''),
|
|
267
|
+
escapeCsv(t.assignee),
|
|
268
|
+
escapeCsv(t.model),
|
|
269
|
+
escapeCsv(t.provider),
|
|
270
|
+
escapeCsv(t.reasoningEffort ?? ''),
|
|
271
|
+
escapeCsv(t.workspaceId),
|
|
272
|
+
escapeCsv(t.branch),
|
|
273
|
+
escapeCsv(formatDate(t.createdAt)),
|
|
274
|
+
escapeCsv(formatDate(t.updatedAt)),
|
|
275
|
+
]
|
|
276
|
+
rows.push(row.join(','))
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// UTF-8 BOM prefix for Microsoft Excel
|
|
280
|
+
return '\uFEFF' + rows.join('\r\n')
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Clean up task worktree.
|
|
286
|
+
*/
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import crypto from 'node:crypto'
|
|
2
|
+
|
|
3
|
+
export const MAX_BODY_BYTES = 256 * 1024
|
|
4
|
+
|
|
5
|
+
export function isLoopbackAddress(addr) {
|
|
6
|
+
if (!addr || typeof addr !== 'string') return false
|
|
7
|
+
const clean = addr.toLowerCase().replace(/^::ffff:/, '').replace(/^\[|\]$/g, '')
|
|
8
|
+
return (
|
|
9
|
+
clean === '127.0.0.1' ||
|
|
10
|
+
clean === '::1' ||
|
|
11
|
+
clean === 'localhost' ||
|
|
12
|
+
clean === 'localhost.' ||
|
|
13
|
+
clean.startsWith('127.')
|
|
14
|
+
)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function isLanAddress(addr) {
|
|
18
|
+
if (!addr || typeof addr !== 'string') return false
|
|
19
|
+
const clean = addr.toLowerCase().replace(/^::ffff:/, '').replace(/^\[|\]$/g, '')
|
|
20
|
+
if (isLoopbackAddress(clean)) return true
|
|
21
|
+
if (clean.startsWith('10.') || clean.startsWith('192.168.')) return true
|
|
22
|
+
const m = clean.match(/^172\.(\d+)\./)
|
|
23
|
+
if (m) {
|
|
24
|
+
const octet = parseInt(m[1], 10)
|
|
25
|
+
if (octet >= 16 && octet <= 31) return true
|
|
26
|
+
}
|
|
27
|
+
return false
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function isTrustedHost(host) {
|
|
31
|
+
if (!host || typeof host !== 'string') return false
|
|
32
|
+
let hostname = host.trim()
|
|
33
|
+
if (hostname.startsWith('[')) {
|
|
34
|
+
const end = hostname.indexOf(']')
|
|
35
|
+
if (end > 0) hostname = hostname.slice(1, end)
|
|
36
|
+
} else {
|
|
37
|
+
hostname = hostname.split(':')[0]
|
|
38
|
+
}
|
|
39
|
+
hostname = hostname.toLowerCase()
|
|
40
|
+
if (!hostname) return false
|
|
41
|
+
if (hostname === 'localhost' || hostname === 'localhost.' || hostname.endsWith('.local')) return true
|
|
42
|
+
if (isLoopbackAddress(hostname) || isLanAddress(hostname)) return true
|
|
43
|
+
return false
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function normalizeIp(addr) {
|
|
47
|
+
if (!addr || typeof addr !== 'string') return ''
|
|
48
|
+
return addr.toLowerCase().replace(/^::ffff:/, '').replace(/^\[|\]$/g, '')
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function cookieNameForAuthority(authority) {
|
|
52
|
+
if (!authority || typeof authority !== 'string') return ''
|
|
53
|
+
const hash = crypto.createHash('sha256').update(authority.trim()).digest('base64url')
|
|
54
|
+
return 'dsh-auth-' + hash
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function isLoopbackCookieName(name, host = '') {
|
|
58
|
+
if (!name || typeof name !== 'string' || !name.startsWith('dsh-auth-')) return false
|
|
59
|
+
const authorities = [
|
|
60
|
+
host,
|
|
61
|
+
'127.0.0.1:3000',
|
|
62
|
+
'127.0.0.1:3080',
|
|
63
|
+
'127.0.0.1:3082',
|
|
64
|
+
'127.0.0.1',
|
|
65
|
+
'localhost:3000',
|
|
66
|
+
'localhost:3080',
|
|
67
|
+
'localhost:3082',
|
|
68
|
+
'localhost',
|
|
69
|
+
'[::1]:3000',
|
|
70
|
+
'[::1]',
|
|
71
|
+
]
|
|
72
|
+
for (const auth of authorities) {
|
|
73
|
+
if (auth && isLoopbackAddress(auth.split(':')[0])) {
|
|
74
|
+
if (cookieNameForAuthority(auth) === name) return true
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return false
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function hasClientCredentials(req) {
|
|
81
|
+
if (!req || !req.headers) return false
|
|
82
|
+
if (req.headers.authorization) return true
|
|
83
|
+
if (req.headers['x-dsh-lan-pin']) return true
|
|
84
|
+
const cookie = req.headers.cookie
|
|
85
|
+
if (!cookie || typeof cookie !== 'string') return false
|
|
86
|
+
if (cookie.includes('dsh_token=') || cookie.includes('dsh_lan_pin=')) return true
|
|
87
|
+
|
|
88
|
+
const parts = cookie.split(';')
|
|
89
|
+
for (const part of parts) {
|
|
90
|
+
const trimmed = part.trim()
|
|
91
|
+
const eqIdx = trimmed.indexOf('=')
|
|
92
|
+
if (eqIdx === -1) continue
|
|
93
|
+
const name = trimmed.slice(0, eqIdx).trim()
|
|
94
|
+
if (name.startsWith('dsh-auth-')) {
|
|
95
|
+
if (!isLoopbackCookieName(name, req.headers.host)) {
|
|
96
|
+
return true
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return false
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function getClientIp(req) {
|
|
104
|
+
const socketAddress =
|
|
105
|
+
req?.socket?.remoteAddress ||
|
|
106
|
+
req?.connection?.remoteAddress ||
|
|
107
|
+
req?.info?.remoteAddress ||
|
|
108
|
+
''
|
|
109
|
+
const socketIp = normalizeIp(socketAddress)
|
|
110
|
+
const xff = req?.headers?.['x-forwarded-for']
|
|
111
|
+
if (xff && typeof xff === 'string' && (isLoopbackAddress(socketIp) || !socketIp)) {
|
|
112
|
+
const first = xff.split(',')[0].trim()
|
|
113
|
+
if (first) {
|
|
114
|
+
return normalizeIp(first)
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return socketIp
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Cross-site request, DNS rebinding, and authentication validation (#261, #369, #384).
|
|
122
|
+
*
|
|
123
|
+
* Requirements:
|
|
124
|
+
* 1. If DSH connection service is available (options.connection), requestRejection(req)
|
|
125
|
+
* is the authoritative gate for browser session authentication and trusted network access.
|
|
126
|
+
* If rejection is returned, the numeric status (e.g. 401 or 403) is returned.
|
|
127
|
+
* 2. Host header is required and must target a trusted host (loopback, LAN, .local).
|
|
128
|
+
* 3. Cross-site requests ('sec-fetch-site: cross-site' or origin mismatch) are rejected with 403.
|
|
129
|
+
* 4. Origin & Referer headers (if present) must match Host or a trusted host.
|
|
130
|
+
* 5. Network boundary validation:
|
|
131
|
+
* - Loopback callers (127.0.0.1, ::1) are allowed.
|
|
132
|
+
* - Non-loopback / LAN callers require valid connection authentication (fail-closed, return 401).
|
|
133
|
+
* - Unknown remote address (socket present but empty remoteAddress) fails closed with 403.
|
|
134
|
+
*
|
|
135
|
+
* @param {import('node:http').IncomingMessage} req
|
|
136
|
+
* @param {{ connection?: { requestRejection?: (req: any) => number | undefined } }} [options]
|
|
137
|
+
* @returns {number|undefined} Rejection HTTP status code (401, 403), or undefined if allowed.
|
|
138
|
+
*/
|
|
139
|
+
export function getRejectionStatus(req, options = {}) {
|
|
140
|
+
const connection = options?.connection
|
|
141
|
+
if (connection && typeof connection.requestRejection === 'function') {
|
|
142
|
+
try {
|
|
143
|
+
const rejection = connection.requestRejection(req)
|
|
144
|
+
if (rejection !== undefined) {
|
|
145
|
+
if (typeof rejection === 'number' && Number.isInteger(rejection) && rejection >= 400 && rejection < 600) {
|
|
146
|
+
return rejection
|
|
147
|
+
}
|
|
148
|
+
return rejection === 401 ? 401 : 403
|
|
149
|
+
}
|
|
150
|
+
} catch {
|
|
151
|
+
// Fail closed if connection service throws
|
|
152
|
+
return 403
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (!req || !req.headers) return 403
|
|
157
|
+
|
|
158
|
+
const host = req.headers.host
|
|
159
|
+
if (!host || !isTrustedHost(host)) {
|
|
160
|
+
return 403
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const site = req.headers['sec-fetch-site']
|
|
164
|
+
|
|
165
|
+
// 1. Explicit cross-site or same-site requests are rejected
|
|
166
|
+
if (site === 'cross-site' || site === 'same-site') {
|
|
167
|
+
return 403
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// 2. Validate origin against trusted hosts and host header
|
|
171
|
+
const origin = req.headers.origin
|
|
172
|
+
if (origin !== undefined) {
|
|
173
|
+
if (origin === 'null' || !origin) return 403
|
|
174
|
+
try {
|
|
175
|
+
const parsed = new URL(origin)
|
|
176
|
+
if (!isTrustedHost(parsed.hostname)) return 403
|
|
177
|
+
if (parsed.host && parsed.host !== host) return 403
|
|
178
|
+
} catch {
|
|
179
|
+
return 403
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// 3. Validate referer against trusted hosts and host header if present
|
|
184
|
+
const referer = req.headers.referer
|
|
185
|
+
if (referer !== undefined && referer) {
|
|
186
|
+
try {
|
|
187
|
+
const parsedRef = new URL(referer)
|
|
188
|
+
if (!isTrustedHost(parsedRef.hostname)) return 403
|
|
189
|
+
if (parsedRef.host && parsedRef.host !== host) return 403
|
|
190
|
+
} catch {
|
|
191
|
+
return 403
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// 4. Validate remote address and network boundary
|
|
196
|
+
const hasSocket = !!(req.socket || req.connection)
|
|
197
|
+
const remote = getClientIp(req)
|
|
198
|
+
|
|
199
|
+
if (hasSocket) {
|
|
200
|
+
if (!remote) {
|
|
201
|
+
return 403
|
|
202
|
+
}
|
|
203
|
+
if (!isLoopbackAddress(remote) && !isLanAddress(remote)) {
|
|
204
|
+
return 403
|
|
205
|
+
}
|
|
206
|
+
// Remote / LAN callers without connection authentication are rejected (#384)
|
|
207
|
+
if (!isLoopbackAddress(remote)) {
|
|
208
|
+
if (!connection) {
|
|
209
|
+
return 401
|
|
210
|
+
}
|
|
211
|
+
// If request was forwarded through a reverse proxy (x-forwarded-for),
|
|
212
|
+
// the proxy may have injected loopback credentials. Validate that the client
|
|
213
|
+
// itself supplied authentic credentials (bearer token, PIN, or client session cookie).
|
|
214
|
+
const xff = req.headers['x-forwarded-for']
|
|
215
|
+
if (xff && !hasClientCredentials(req)) {
|
|
216
|
+
return 401
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
} else {
|
|
220
|
+
// Synthetic mock request without socket
|
|
221
|
+
const xff = req.headers['x-forwarded-for']
|
|
222
|
+
if (xff) {
|
|
223
|
+
const first = xff.split(',')[0].trim()
|
|
224
|
+
if (first && !isLoopbackAddress(normalizeIp(first))) {
|
|
225
|
+
if (!connection || !hasClientCredentials(req)) {
|
|
226
|
+
return 401
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
} else if (!connection) {
|
|
230
|
+
const hostname = String(host).split(':')[0].toLowerCase()
|
|
231
|
+
if (!isLoopbackAddress(hostname) && hostname !== 'localhost') {
|
|
232
|
+
return 401
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// 5. Browser request with declared site:
|
|
238
|
+
// Must be same-origin or none
|
|
239
|
+
if (site && site !== 'same-origin' && site !== 'none') {
|
|
240
|
+
return 403
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// 6. Request without declared site from non-loopback requires connection
|
|
244
|
+
if (!site && remote && !isLoopbackAddress(remote) && !connection) {
|
|
245
|
+
return 401
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return undefined
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
export function isTrustedRequest(req, options = {}) {
|
|
252
|
+
return getRejectionStatus(req, options) === undefined
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
export function rejectUntrustedRequest(req, res, options = {}) {
|
|
256
|
+
const status = getRejectionStatus(req, options)
|
|
257
|
+
if (status !== undefined) {
|
|
258
|
+
try {
|
|
259
|
+
res.writeHead(status, {
|
|
260
|
+
'Content-Type': 'application/json; charset=utf-8',
|
|
261
|
+
'Cache-Control': 'no-store',
|
|
262
|
+
})
|
|
263
|
+
res.end(
|
|
264
|
+
JSON.stringify({
|
|
265
|
+
error: status === 401 ? 'unauthorized' : 'cross-site',
|
|
266
|
+
})
|
|
267
|
+
)
|
|
268
|
+
} catch {
|
|
269
|
+
/* socket closed */
|
|
270
|
+
}
|
|
271
|
+
return true
|
|
272
|
+
}
|
|
273
|
+
return false
|
|
274
|
+
}
|