@tylix/core 0.5.2 → 0.5.3
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/package.json +1 -1
- package/src/http/Server.js +106 -1
package/package.json
CHANGED
package/src/http/Server.js
CHANGED
|
@@ -6,6 +6,41 @@ import { parseCookies } from './parseCookies.js'
|
|
|
6
6
|
import { buildDebugPayload } from './debug/buildDebugPayload.js'
|
|
7
7
|
import { renderDebugPage } from './debug/renderDebugPage.js'
|
|
8
8
|
|
|
9
|
+
const UNSAFE_METHODS = new Set(['POST', 'PUT', 'PATCH', 'DELETE'])
|
|
10
|
+
|
|
11
|
+
// Pages inline their scripts, so script-src needs 'unsafe-inline'. The rest
|
|
12
|
+
// still blocks framing, base-tag and form hijacking, plugins, and data being
|
|
13
|
+
// sent to other origins. ws:/wss: is open because the channels server runs
|
|
14
|
+
// on its own port. Override the whole policy with CSP_POLICY if needed.
|
|
15
|
+
const DEFAULT_CSP = [
|
|
16
|
+
"default-src 'self'",
|
|
17
|
+
"script-src 'self' 'unsafe-inline' https://challenges.cloudflare.com",
|
|
18
|
+
"style-src 'self' 'unsafe-inline'",
|
|
19
|
+
"img-src 'self' data: blob: https:",
|
|
20
|
+
"font-src 'self' data:",
|
|
21
|
+
"connect-src 'self' ws: wss:",
|
|
22
|
+
'frame-src https://challenges.cloudflare.com',
|
|
23
|
+
"frame-ancestors 'none'",
|
|
24
|
+
"base-uri 'self'",
|
|
25
|
+
"form-action 'self'",
|
|
26
|
+
"object-src 'none'",
|
|
27
|
+
].join('; ')
|
|
28
|
+
|
|
29
|
+
function parseList(value) {
|
|
30
|
+
return String(value || '')
|
|
31
|
+
.split(',')
|
|
32
|
+
.map((item) => item.trim())
|
|
33
|
+
.filter(Boolean)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function originOf(value) {
|
|
37
|
+
try {
|
|
38
|
+
return new URL(value).origin
|
|
39
|
+
} catch {
|
|
40
|
+
return null
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
9
44
|
// Forwarding headers are only believed when the app is told it sits behind
|
|
10
45
|
// a proxy it controls, because any client can send X-Forwarded-For. The LAST
|
|
11
46
|
// hop is the one our own proxy appended, so a forged leading entry is ignored.
|
|
@@ -20,6 +55,12 @@ function clientIp(req, trustProxy) {
|
|
|
20
55
|
return req.socket.remoteAddress || 'unknown'
|
|
21
56
|
}
|
|
22
57
|
|
|
58
|
+
function requestHost(req, trustProxy) {
|
|
59
|
+
const forwarded = trustProxy ? String(req.headers['x-forwarded-host'] || '') : ''
|
|
60
|
+
const raw = forwarded ? forwarded.split(',').pop() : String(req.headers.host || '')
|
|
61
|
+
return raw.trim().toLowerCase()
|
|
62
|
+
}
|
|
63
|
+
|
|
23
64
|
function isSecure(req, trustProxy) {
|
|
24
65
|
if (req.socket.encrypted) return true
|
|
25
66
|
if (!trustProxy) return false
|
|
@@ -65,6 +106,22 @@ export class Server {
|
|
|
65
106
|
// generic 500. Defaults to the APP_DEBUG=true env var.
|
|
66
107
|
this.debug = debug ?? process.env.APP_DEBUG === 'true'
|
|
67
108
|
|
|
109
|
+
// Origins allowed to make browser write requests from elsewhere
|
|
110
|
+
// (TRUSTED_ORIGINS=https://a.example,https://b.example). APP_URL counts too.
|
|
111
|
+
this.trustedOrigins = new Set(
|
|
112
|
+
[...parseList(process.env.TRUSTED_ORIGINS), process.env.APP_URL]
|
|
113
|
+
.filter(Boolean)
|
|
114
|
+
.map(originOf)
|
|
115
|
+
.filter(Boolean),
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
// CSP_MODE: off | report-only | enforce. Production defaults to
|
|
119
|
+
// report-only: violations show in the browser console, nothing breaks.
|
|
120
|
+
this.cspMode = String(
|
|
121
|
+
process.env.CSP_MODE || (this.production ? 'report-only' : 'off'),
|
|
122
|
+
).toLowerCase()
|
|
123
|
+
this.csp = process.env.CSP_POLICY || DEFAULT_CSP
|
|
124
|
+
|
|
68
125
|
if (this.debug && this.production) {
|
|
69
126
|
console.warn(
|
|
70
127
|
'[tylix] APP_DEBUG is on while NODE_ENV=production. Error responses ' +
|
|
@@ -84,13 +141,61 @@ export class Server {
|
|
|
84
141
|
}
|
|
85
142
|
}
|
|
86
143
|
|
|
87
|
-
|
|
144
|
+
applySecurityHeaders(req, res) {
|
|
88
145
|
res.setHeader('X-Content-Type-Options', 'nosniff')
|
|
89
146
|
res.setHeader('Referrer-Policy', 'strict-origin-when-cross-origin')
|
|
147
|
+
res.setHeader('X-Frame-Options', 'DENY')
|
|
148
|
+
res.setHeader('Permissions-Policy', 'camera=(), microphone=(), geolocation=()')
|
|
149
|
+
// allow-popups keeps OAuth and payment popups working.
|
|
150
|
+
res.setHeader('Cross-Origin-Opener-Policy', 'same-origin-allow-popups')
|
|
151
|
+
|
|
152
|
+
if (req.secure) {
|
|
153
|
+
res.setHeader('Strict-Transport-Security', 'max-age=31536000')
|
|
154
|
+
}
|
|
90
155
|
|
|
156
|
+
if (this.cspMode === 'enforce') {
|
|
157
|
+
res.setHeader('Content-Security-Policy', this.csp)
|
|
158
|
+
} else if (this.cspMode === 'report-only') {
|
|
159
|
+
res.setHeader('Content-Security-Policy-Report-Only', this.csp)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// API responses carry private data: never let a browser or proxy cache them.
|
|
163
|
+
// Route handlers can still override this.
|
|
164
|
+
if (String(req.url).startsWith('/api/')) {
|
|
165
|
+
res.setHeader('Cache-Control', 'no-store')
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Browsers always label cross-origin requests (Sec-Fetch-Site / Origin), so
|
|
170
|
+
// a browser write from another site is refused. Requests with neither header
|
|
171
|
+
// (curl, webhooks, server-to-server) are not browser CSRF and pass through.
|
|
172
|
+
isCrossSiteWrite(req) {
|
|
173
|
+
if (!UNSAFE_METHODS.has(req.method)) return false
|
|
174
|
+
|
|
175
|
+
const site = req.headers['sec-fetch-site']
|
|
176
|
+
const origin = req.headers.origin
|
|
177
|
+
if (!site && !origin) return false
|
|
178
|
+
if (site === 'same-origin' || site === 'none') return false
|
|
179
|
+
|
|
180
|
+
if (!origin) return true
|
|
181
|
+
|
|
182
|
+
const parsed = originOf(origin)
|
|
183
|
+
if (!parsed) return true
|
|
184
|
+
if (this.trustedOrigins.has(parsed)) return false
|
|
185
|
+
return new URL(parsed).host !== requestHost(req, this.trustProxy)
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
async handle(req, res) {
|
|
91
189
|
req.ip = clientIp(req, this.trustProxy)
|
|
92
190
|
req.secure = isSecure(req, this.trustProxy)
|
|
93
191
|
|
|
192
|
+
this.applySecurityHeaders(req, res)
|
|
193
|
+
|
|
194
|
+
if (this.isCrossSiteWrite(req)) {
|
|
195
|
+
res.status(403).json({ error: 'Cross-site request blocked.' })
|
|
196
|
+
return
|
|
197
|
+
}
|
|
198
|
+
|
|
94
199
|
let match = this.router.match(req.method, req.url)
|
|
95
200
|
// HEAD is GET without a body; Node drops the body for us.
|
|
96
201
|
if (!match && req.method === 'HEAD') {
|