jskelet 0.6.3 → 0.6.4
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/AGENTS.md +136 -136
- package/CHANGELOG.md +628 -620
- package/LICENSE +21 -21
- package/README.md +2 -0
- package/bin/jskelet.mjs +130 -130
- package/docs/01-baslangic.md +291 -291
- package/docs/02-mimari.md +310 -310
- package/docs/03-routing.md +515 -515
- package/docs/04-render-ve-sablonlar.md +667 -661
- package/docs/05-islands.md +486 -486
- package/docs/06-cache.md +1467 -1443
- package/docs/07-yapilandirma.md +1208 -1197
- package/docs/08-build.md +429 -429
- package/docs/09-dev-araclari.md +364 -364
- package/docs/10-dagitim.md +348 -338
- package/docs/12-panel-ve-oturum.md +479 -478
- package/docs/README.md +83 -83
- package/docs/en/01-getting-started.md +298 -298
- package/docs/en/02-architecture.md +329 -329
- package/docs/en/03-routing.md +531 -531
- package/docs/en/04-rendering.md +675 -669
- package/docs/en/05-islands.md +497 -497
- package/docs/en/06-caching.md +1476 -1453
- package/docs/en/07-configuration.md +1229 -1219
- package/docs/en/08-build.md +447 -447
- package/docs/en/09-dev-tools.md +373 -373
- package/docs/en/10-deployment.md +351 -340
- package/docs/en/11-migration.md +398 -398
- package/docs/en/12-dashboards-and-sessions.md +489 -488
- package/docs/en/README.md +87 -87
- package/package.json +137 -137
- package/src/build/ensure-build.mjs +19 -19
- package/src/build/paths.mjs +153 -153
- package/src/build/resolve-peer.mjs +36 -36
- package/src/build/tasks/client.mjs +349 -349
- package/src/build/tasks/css.mjs +235 -235
- package/src/build/tasks/fonts.mjs +146 -146
- package/src/build/tasks/icons.mjs +357 -357
- package/src/build/tasks/images.mjs +244 -244
- package/src/build/tasks/precompress.mjs +78 -78
- package/src/build/tasks/templates.mjs +20 -20
- package/src/client/admin/i18n.js +764 -764
- package/src/client/admin/login.html +74 -74
- package/src/client/admin/panel.css +809 -809
- package/src/client/admin/panel.html +495 -495
- package/src/client/admin/panel.js +1251 -1251
- package/src/client/devtools/report.html +185 -185
- package/src/client/devtools/report.js +745 -745
- package/src/client/devtools/seo.js +628 -628
- package/src/client/dom.js +95 -95
- package/src/client/form.js +192 -192
- package/src/client/index.js +45 -45
- package/src/client/registry.js +305 -305
- package/src/client/safe-image.js +91 -91
- package/src/client/shared-cookie.js +225 -225
- package/src/client/store.js +36 -36
- package/src/client/swap.js +188 -188
- package/src/compile/codegen.js +336 -336
- package/src/compile/compile-all.js +149 -149
- package/src/compile/errors.js +66 -66
- package/src/compile/expr.js +409 -409
- package/src/compile/index.js +17 -17
- package/src/compile/parse.js +541 -541
- package/src/compile/resolve.js +211 -211
- package/src/compile/scan-exports.js +51 -51
- package/src/config/defaults.js +541 -534
- package/src/config/index.js +1500 -1469
- package/src/config/pattern.js +107 -107
- package/src/generate.mjs +163 -163
- package/src/http/control-flow.js +71 -71
- package/src/http/cookies-entry.js +21 -21
- package/src/http/cookies.js +277 -277
- package/src/http/request-cache.js +46 -46
- package/src/http/request-context.js +165 -165
- package/src/http/shared-cookie.js +178 -178
- package/src/index.js +101 -101
- package/src/init.mjs +232 -230
- package/src/migrate/apply.mjs +262 -262
- package/src/migrate/babel.mjs +79 -79
- package/src/migrate/classify.mjs +155 -155
- package/src/migrate/config.mjs +126 -126
- package/src/migrate/fs-walk.mjs +191 -191
- package/src/migrate/parse.mjs +26 -26
- package/src/migrate/scan.mjs +177 -177
- package/src/migrate/transform/expr-source.mjs +168 -168
- package/src/migrate/transform/island.mjs +67 -67
- package/src/migrate/transform/jsx-to-component.mjs +302 -302
- package/src/migrate/transform/jsx-to-jsk.mjs +330 -330
- package/src/migrate/transform/page-split.mjs +435 -435
- package/src/migrate/write.mjs +81 -81
- package/src/migrate.mjs +171 -171
- package/src/runtime/alias-hooks.mjs +119 -119
- package/src/runtime/register.mjs +4 -4
- package/src/server/admin/actions.js +229 -229
- package/src/server/admin/auth.js +125 -125
- package/src/server/admin/event-log.js +151 -151
- package/src/server/admin/gate.js +209 -209
- package/src/server/admin/inventory.js +188 -188
- package/src/server/admin/mount.js +56 -56
- package/src/server/admin/router.js +216 -216
- package/src/server/admin/snapshot.js +241 -241
- package/src/server/assets.js +147 -147
- package/src/server/auth/handoff.js +309 -309
- package/src/server/cache-blob.js +70 -70
- package/src/server/cache-control.js +45 -0
- package/src/server/cache-deps.js +42 -42
- package/src/server/cache-vary.js +113 -113
- package/src/server/cloudflare.js +607 -607
- package/src/server/create-app.js +366 -366
- package/src/server/data-cache.js +553 -553
- package/src/server/dev/report.js +485 -485
- package/src/server/dev/socket.js +170 -170
- package/src/server/dev/version-check.mjs +139 -139
- package/src/server/disk-cache.js +233 -233
- package/src/server/ejs-adapter.js +59 -59
- package/src/server/html-cache.js +1196 -1196
- package/src/server/image-optimizer.js +500 -500
- package/src/server/logs/access-middleware.js +66 -66
- package/src/server/logs/file-sink.js +193 -193
- package/src/server/logs/pipeline.js +165 -165
- package/src/server/logs/s3-put.js +214 -214
- package/src/server/logs/s3-sink.js +112 -112
- package/src/server/metadata.js +102 -102
- package/src/server/middleware/compression.js +205 -205
- package/src/server/middleware/csrf.js +134 -134
- package/src/server/middleware/dev-gate.js +75 -75
- package/src/server/middleware/headers.js +37 -37
- package/src/server/middleware/redirects.js +32 -32
- package/src/server/middleware/robots-txt.js +341 -341
- package/src/server/middleware/static-precompressed.js +121 -121
- package/src/server/middleware/trailing-slash.js +53 -53
- package/src/server/middleware/upstream-proxy.js +141 -141
- package/src/server/og-image.js +369 -356
- package/src/server/port-guard.js +255 -255
- package/src/server/prewarm.js +1082 -1082
- package/src/server/redis.js +588 -588
- package/src/server/render.js +910 -910
- package/src/server/router.js +157 -157
- package/src/server/status-page.js +265 -265
- package/src/server/upstream-limiter.js +376 -376
- package/src/server/upstream-tracking.js +166 -166
- package/src/shared/cookie-domain.js +66 -66
- package/src/start.mjs +22 -22
- package/src/templates/layout.ejs +30 -30
- package/src/templates/layout.jsk +30 -30
- package/src/version.mjs +31 -31
- package/src/views/components/loader.js +101 -101
- package/src/views/helpers/html.js +102 -102
- package/src/views/helpers/tags.js +375 -375
- package/types/config/defaults.d.ts +6 -0
- package/types/config/index.d.ts +6 -0
- package/types/server/cache-control.d.ts +28 -0
- package/types/server/og-image.d.ts +5 -0
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `jskelet/cookies` paket girişi — imzalı cookie + paylaşımlı Domain yüzeyi.
|
|
3
|
-
*/
|
|
4
|
-
export {
|
|
5
|
-
clearCookie,
|
|
6
|
-
getSignedCookie,
|
|
7
|
-
isValidCookieName,
|
|
8
|
-
parseCookies,
|
|
9
|
-
randomToken,
|
|
10
|
-
safeEqual,
|
|
11
|
-
serializeCookie,
|
|
12
|
-
setCookie,
|
|
13
|
-
setSignedCookie,
|
|
14
|
-
} from "./cookies.js";
|
|
15
|
-
export {
|
|
16
|
-
clearSharedCookie,
|
|
17
|
-
requestIsHttps,
|
|
18
|
-
resolveSharedCookieDomain,
|
|
19
|
-
SHARED_COOKIE_WARN_BYTES,
|
|
20
|
-
writeSharedCookie,
|
|
21
|
-
} from "./shared-cookie.js";
|
|
1
|
+
/**
|
|
2
|
+
* `jskelet/cookies` paket girişi — imzalı cookie + paylaşımlı Domain yüzeyi.
|
|
3
|
+
*/
|
|
4
|
+
export {
|
|
5
|
+
clearCookie,
|
|
6
|
+
getSignedCookie,
|
|
7
|
+
isValidCookieName,
|
|
8
|
+
parseCookies,
|
|
9
|
+
randomToken,
|
|
10
|
+
safeEqual,
|
|
11
|
+
serializeCookie,
|
|
12
|
+
setCookie,
|
|
13
|
+
setSignedCookie,
|
|
14
|
+
} from "./cookies.js";
|
|
15
|
+
export {
|
|
16
|
+
clearSharedCookie,
|
|
17
|
+
requestIsHttps,
|
|
18
|
+
resolveSharedCookieDomain,
|
|
19
|
+
SHARED_COOKIE_WARN_BYTES,
|
|
20
|
+
writeSharedCookie,
|
|
21
|
+
} from "./shared-cookie.js";
|
package/src/http/cookies.js
CHANGED
|
@@ -1,277 +1,277 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Cookie okuma/yazma ve HMAC ile imzalama.
|
|
3
|
-
*
|
|
4
|
-
* Neden framework'te: kişiye özel her sayfa bir oturum cookie'sine dayanıyor
|
|
5
|
-
* ve bunu elle yazan her proje aynı üç hatayı tekrar ediyor — `HttpOnly`
|
|
6
|
-
* unutmak, imzasız değere güvenmek, karşılaştırmayı `===` ile yapmak.
|
|
7
|
-
* Burada varsayılanlar güvenli tarafta ve imza doğrulaması sabit zamanlı.
|
|
8
|
-
*
|
|
9
|
-
* Framework **kimlik sağlamaz**: oturumun içinde ne olduğu, ne kadar
|
|
10
|
-
* yaşadığı ve kimin verdiği uygulamanın kararı. Buradaki yüzey yalnızca
|
|
11
|
-
* "bu değeri ben yazdım, kurcalanmamış" garantisini veriyor.
|
|
12
|
-
*
|
|
13
|
-
* Bağımlılık eklenmez; `node:crypto` yeterli.
|
|
14
|
-
*/
|
|
15
|
-
import crypto from "node:crypto";
|
|
16
|
-
import process from "node:process";
|
|
17
|
-
import { getConfig } from "../config/index.js";
|
|
18
|
-
import { markTainted } from "./request-context.js";
|
|
19
|
-
|
|
20
|
-
/** Ayrıştırılmış cookie'ler istek başına bir kez hesaplanır. */
|
|
21
|
-
const PARSED = Symbol("jskelet.cookies");
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* @param {string} value
|
|
25
|
-
* @returns {string}
|
|
26
|
-
*/
|
|
27
|
-
function base64url(value) {
|
|
28
|
-
return Buffer.from(value, "utf8").toString("base64url");
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* @param {string} value
|
|
33
|
-
* @returns {string | null}
|
|
34
|
-
*/
|
|
35
|
-
function fromBase64url(value) {
|
|
36
|
-
try {
|
|
37
|
-
return Buffer.from(value, "base64url").toString("utf8");
|
|
38
|
-
} catch {
|
|
39
|
-
return null;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* İmza sırrı. `security.cookieSecret` ya da `JSKELET_SECRET`.
|
|
45
|
-
*
|
|
46
|
-
* Yokluğunda imzasız cookie yazmak en kötü sonuç olurdu: uygulama kendini
|
|
47
|
-
* güvende sanar, değer kurcalanabilir. Bu yüzden imzalı API sır olmadan
|
|
48
|
-
* hata verir — config hatasının siteyi düşürmemesi kuralı burada geçmez,
|
|
49
|
-
* çünkü sessiz alternatif bir güvenlik açığı.
|
|
50
|
-
*
|
|
51
|
-
* @returns {string}
|
|
52
|
-
*/
|
|
53
|
-
function getSecret() {
|
|
54
|
-
/** @type {string | null} */
|
|
55
|
-
let configured = null;
|
|
56
|
-
|
|
57
|
-
try {
|
|
58
|
-
configured = getConfig().security.cookieSecret;
|
|
59
|
-
} catch {
|
|
60
|
-
// Config yüklenmemiş olabilir (script, test); env yine de geçerli.
|
|
61
|
-
configured = null;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const secret = configured ?? process.env.JSKELET_SECRET ?? null;
|
|
65
|
-
|
|
66
|
-
if (!secret) {
|
|
67
|
-
throw new Error(
|
|
68
|
-
"[cookies] a secret is required for signed cookies. Set " +
|
|
69
|
-
"`security.cookieSecret` in `jskelet.config.mjs` or the JSKELET_SECRET environment variable.",
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return secret;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* @param {string} value
|
|
78
|
-
* @returns {string}
|
|
79
|
-
*/
|
|
80
|
-
function sign(value) {
|
|
81
|
-
return crypto.createHmac("sha256", getSecret()).update(value).digest("base64url");
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Sabit zamanlı karşılaştırma: imza doğrulamasında erken çıkış, saldırganın
|
|
86
|
-
* baytları tek tek tahmin etmesine kapı aralar.
|
|
87
|
-
*
|
|
88
|
-
* @param {string} a
|
|
89
|
-
* @param {string} b
|
|
90
|
-
* @returns {boolean}
|
|
91
|
-
*/
|
|
92
|
-
export function safeEqual(a, b) {
|
|
93
|
-
const left = Buffer.from(String(a));
|
|
94
|
-
const right = Buffer.from(String(b));
|
|
95
|
-
if (left.length !== right.length) return false;
|
|
96
|
-
return crypto.timingSafeEqual(left, right);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* `Cookie` başlığını ayrıştırır.
|
|
101
|
-
*
|
|
102
|
-
* Okuma çıktının kullanıcıya bağlı olduğunu bildirir: bu sayfa artık public
|
|
103
|
-
* HTML cache'ine yazılamaz.
|
|
104
|
-
*
|
|
105
|
-
* @param {import('http').IncomingMessage} req
|
|
106
|
-
* @returns {Record<string, string>}
|
|
107
|
-
*/
|
|
108
|
-
export function parseCookies(req) {
|
|
109
|
-
markTainted("parseCookies(req)");
|
|
110
|
-
|
|
111
|
-
const cached = /** @type {any} */ (req)[PARSED];
|
|
112
|
-
if (cached) return cached;
|
|
113
|
-
|
|
114
|
-
/** @type {Record<string, string>} */
|
|
115
|
-
const out = {};
|
|
116
|
-
const header = req.headers?.cookie;
|
|
117
|
-
|
|
118
|
-
if (header) {
|
|
119
|
-
for (const part of header.split(";")) {
|
|
120
|
-
const index = part.indexOf("=");
|
|
121
|
-
if (index === -1) continue;
|
|
122
|
-
|
|
123
|
-
const name = part.slice(0, index).trim();
|
|
124
|
-
if (!name) continue;
|
|
125
|
-
|
|
126
|
-
try {
|
|
127
|
-
out[name] = decodeURIComponent(part.slice(index + 1).trim());
|
|
128
|
-
} catch {
|
|
129
|
-
// Bozuk yüzde kodlaması tüm başlığı çöpe atmamalı.
|
|
130
|
-
out[name] = part.slice(index + 1).trim();
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/** @type {any} */ (req)[PARSED] = out;
|
|
136
|
-
return out;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* @typedef {object} CookieOptions
|
|
141
|
-
* @property {string} [path] Varsayılan `/`.
|
|
142
|
-
* @property {string} [domain]
|
|
143
|
-
* @property {number} [maxAge] Saniye.
|
|
144
|
-
* @property {Date} [expires]
|
|
145
|
-
* @property {boolean} [httpOnly] Varsayılan `true`.
|
|
146
|
-
* @property {boolean} [secure] Varsayılan: development dışında `true`.
|
|
147
|
-
* `writeSharedCookie` protokole bakarak ezer (`https` → Secure).
|
|
148
|
-
* @property {"Strict" | "Lax" | "None"} [sameSite] Varsayılan `Lax`.
|
|
149
|
-
*/
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* RFC 6265 cookie-name (token). `;`, boşluk, CRLF gibi karakterler
|
|
153
|
-
* Set-Cookie enjeksiyonuna yol açardı — reddedilir.
|
|
154
|
-
*
|
|
155
|
-
* @param {unknown} name
|
|
156
|
-
* @returns {boolean}
|
|
157
|
-
*/
|
|
158
|
-
export function isValidCookieName(name) {
|
|
159
|
-
return typeof name === "string" && /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/.test(name);
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* Varsayılanlar bilinçli olarak kısıtlayıcı: `HttpOnly` ile JS okuyamaz,
|
|
164
|
-
* `SameSite=Lax` ile çapraz site POST'larında gönderilmez (CSRF'nin büyük
|
|
165
|
-
* kısmını kapatan tek satır), `Secure` üretimde açık.
|
|
166
|
-
*
|
|
167
|
-
* @param {string} name
|
|
168
|
-
* @param {string} value
|
|
169
|
-
* @param {CookieOptions} [options]
|
|
170
|
-
* @returns {string}
|
|
171
|
-
*/
|
|
172
|
-
export function serializeCookie(name, value, options = {}) {
|
|
173
|
-
if (!isValidCookieName(name)) {
|
|
174
|
-
throw new Error(
|
|
175
|
-
`[cookies] invalid cookie name ${JSON.stringify(name)}; ` +
|
|
176
|
-
"names must be RFC 6265 tokens (no spaces, semicolons, or control chars)",
|
|
177
|
-
);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
const parts = [`${name}=${encodeURIComponent(value)}`];
|
|
181
|
-
|
|
182
|
-
parts.push(`Path=${options.path ?? "/"}`);
|
|
183
|
-
if (options.domain) parts.push(`Domain=${options.domain}`);
|
|
184
|
-
if (options.maxAge !== undefined) parts.push(`Max-Age=${Math.floor(options.maxAge)}`);
|
|
185
|
-
if (options.expires) parts.push(`Expires=${options.expires.toUTCString()}`);
|
|
186
|
-
if (options.httpOnly !== false) parts.push("HttpOnly");
|
|
187
|
-
if (options.secure ?? process.env.NODE_ENV !== "development") parts.push("Secure");
|
|
188
|
-
parts.push(`SameSite=${options.sameSite ?? "Lax"}`);
|
|
189
|
-
|
|
190
|
-
return parts.join("; ");
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* @param {import('http').ServerResponse} res
|
|
195
|
-
* @param {string} name
|
|
196
|
-
* @param {string} value
|
|
197
|
-
* @param {CookieOptions} [options]
|
|
198
|
-
*/
|
|
199
|
-
export function setCookie(res, name, value, options = {}) {
|
|
200
|
-
const existing = res.getHeader("Set-Cookie");
|
|
201
|
-
const serialized = serializeCookie(name, value, options);
|
|
202
|
-
|
|
203
|
-
/** @type {string[]} */
|
|
204
|
-
const all = existing
|
|
205
|
-
? Array.isArray(existing)
|
|
206
|
-
? [...existing.map(String)]
|
|
207
|
-
: [String(existing)]
|
|
208
|
-
: [];
|
|
209
|
-
|
|
210
|
-
all.push(serialized);
|
|
211
|
-
res.setHeader("Set-Cookie", all);
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
* @param {import('http').ServerResponse} res
|
|
216
|
-
* @param {string} name
|
|
217
|
-
* @param {CookieOptions} [options]
|
|
218
|
-
*/
|
|
219
|
-
export function clearCookie(res, name, options = {}) {
|
|
220
|
-
setCookie(res, name, "", { ...options, maxAge: 0, expires: new Date(0) });
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
/**
|
|
224
|
-
* İmzalı cookie yazar. Değer okunabilir kalır (şifreleme değil, imza);
|
|
225
|
-
* gizli kalması gereken veriyi cookie'ye koymayın, kimliğini koyun.
|
|
226
|
-
*
|
|
227
|
-
* @param {import('http').ServerResponse} res
|
|
228
|
-
* @param {string} name
|
|
229
|
-
* @param {string} value
|
|
230
|
-
* @param {CookieOptions} [options]
|
|
231
|
-
*/
|
|
232
|
-
export function setSignedCookie(res, name, value, options = {}) {
|
|
233
|
-
const encoded = base64url(value);
|
|
234
|
-
setCookie(res, name, `${encoded}.${sign(encoded)}`, options);
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
/**
|
|
238
|
-
* İmzalı cookie okur. İmza uymuyorsa `null` — bozuk imza, yok sayılmalı,
|
|
239
|
-
* "belki geçerlidir" diye kullanılmamalı.
|
|
240
|
-
*
|
|
241
|
-
* @param {import('http').IncomingMessage} req
|
|
242
|
-
* @param {string} name
|
|
243
|
-
* @returns {string | null}
|
|
244
|
-
*/
|
|
245
|
-
export function getSignedCookie(req, name) {
|
|
246
|
-
const raw = parseCookies(req)[name];
|
|
247
|
-
if (!raw) return null;
|
|
248
|
-
|
|
249
|
-
const index = raw.lastIndexOf(".");
|
|
250
|
-
if (index <= 0) return null;
|
|
251
|
-
|
|
252
|
-
const encoded = raw.slice(0, index);
|
|
253
|
-
const signature = raw.slice(index + 1);
|
|
254
|
-
|
|
255
|
-
let expected;
|
|
256
|
-
try {
|
|
257
|
-
expected = sign(encoded);
|
|
258
|
-
} catch {
|
|
259
|
-
// Sır yoksa imzalı okuma sessizce başarısız olur: sunucu ayakta kalır
|
|
260
|
-
// ama hiçbir oturum geçerli sayılmaz.
|
|
261
|
-
return null;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
if (!safeEqual(signature, expected)) return null;
|
|
265
|
-
return fromBase64url(encoded);
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
/**
|
|
269
|
-
* Kriptografik rastgele token. CSRF token'ı ve oturum kimliği için.
|
|
270
|
-
*
|
|
271
|
-
* @param {number} [bytes]
|
|
272
|
-
* @returns {string}
|
|
273
|
-
*/
|
|
274
|
-
export function randomToken(bytes = 32) {
|
|
275
|
-
return crypto.randomBytes(bytes).toString("base64url");
|
|
276
|
-
}
|
|
277
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Cookie okuma/yazma ve HMAC ile imzalama.
|
|
3
|
+
*
|
|
4
|
+
* Neden framework'te: kişiye özel her sayfa bir oturum cookie'sine dayanıyor
|
|
5
|
+
* ve bunu elle yazan her proje aynı üç hatayı tekrar ediyor — `HttpOnly`
|
|
6
|
+
* unutmak, imzasız değere güvenmek, karşılaştırmayı `===` ile yapmak.
|
|
7
|
+
* Burada varsayılanlar güvenli tarafta ve imza doğrulaması sabit zamanlı.
|
|
8
|
+
*
|
|
9
|
+
* Framework **kimlik sağlamaz**: oturumun içinde ne olduğu, ne kadar
|
|
10
|
+
* yaşadığı ve kimin verdiği uygulamanın kararı. Buradaki yüzey yalnızca
|
|
11
|
+
* "bu değeri ben yazdım, kurcalanmamış" garantisini veriyor.
|
|
12
|
+
*
|
|
13
|
+
* Bağımlılık eklenmez; `node:crypto` yeterli.
|
|
14
|
+
*/
|
|
15
|
+
import crypto from "node:crypto";
|
|
16
|
+
import process from "node:process";
|
|
17
|
+
import { getConfig } from "../config/index.js";
|
|
18
|
+
import { markTainted } from "./request-context.js";
|
|
19
|
+
|
|
20
|
+
/** Ayrıştırılmış cookie'ler istek başına bir kez hesaplanır. */
|
|
21
|
+
const PARSED = Symbol("jskelet.cookies");
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @param {string} value
|
|
25
|
+
* @returns {string}
|
|
26
|
+
*/
|
|
27
|
+
function base64url(value) {
|
|
28
|
+
return Buffer.from(value, "utf8").toString("base64url");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @param {string} value
|
|
33
|
+
* @returns {string | null}
|
|
34
|
+
*/
|
|
35
|
+
function fromBase64url(value) {
|
|
36
|
+
try {
|
|
37
|
+
return Buffer.from(value, "base64url").toString("utf8");
|
|
38
|
+
} catch {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* İmza sırrı. `security.cookieSecret` ya da `JSKELET_SECRET`.
|
|
45
|
+
*
|
|
46
|
+
* Yokluğunda imzasız cookie yazmak en kötü sonuç olurdu: uygulama kendini
|
|
47
|
+
* güvende sanar, değer kurcalanabilir. Bu yüzden imzalı API sır olmadan
|
|
48
|
+
* hata verir — config hatasının siteyi düşürmemesi kuralı burada geçmez,
|
|
49
|
+
* çünkü sessiz alternatif bir güvenlik açığı.
|
|
50
|
+
*
|
|
51
|
+
* @returns {string}
|
|
52
|
+
*/
|
|
53
|
+
function getSecret() {
|
|
54
|
+
/** @type {string | null} */
|
|
55
|
+
let configured = null;
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
configured = getConfig().security.cookieSecret;
|
|
59
|
+
} catch {
|
|
60
|
+
// Config yüklenmemiş olabilir (script, test); env yine de geçerli.
|
|
61
|
+
configured = null;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const secret = configured ?? process.env.JSKELET_SECRET ?? null;
|
|
65
|
+
|
|
66
|
+
if (!secret) {
|
|
67
|
+
throw new Error(
|
|
68
|
+
"[cookies] a secret is required for signed cookies. Set " +
|
|
69
|
+
"`security.cookieSecret` in `jskelet.config.mjs` or the JSKELET_SECRET environment variable.",
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return secret;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* @param {string} value
|
|
78
|
+
* @returns {string}
|
|
79
|
+
*/
|
|
80
|
+
function sign(value) {
|
|
81
|
+
return crypto.createHmac("sha256", getSecret()).update(value).digest("base64url");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Sabit zamanlı karşılaştırma: imza doğrulamasında erken çıkış, saldırganın
|
|
86
|
+
* baytları tek tek tahmin etmesine kapı aralar.
|
|
87
|
+
*
|
|
88
|
+
* @param {string} a
|
|
89
|
+
* @param {string} b
|
|
90
|
+
* @returns {boolean}
|
|
91
|
+
*/
|
|
92
|
+
export function safeEqual(a, b) {
|
|
93
|
+
const left = Buffer.from(String(a));
|
|
94
|
+
const right = Buffer.from(String(b));
|
|
95
|
+
if (left.length !== right.length) return false;
|
|
96
|
+
return crypto.timingSafeEqual(left, right);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* `Cookie` başlığını ayrıştırır.
|
|
101
|
+
*
|
|
102
|
+
* Okuma çıktının kullanıcıya bağlı olduğunu bildirir: bu sayfa artık public
|
|
103
|
+
* HTML cache'ine yazılamaz.
|
|
104
|
+
*
|
|
105
|
+
* @param {import('http').IncomingMessage} req
|
|
106
|
+
* @returns {Record<string, string>}
|
|
107
|
+
*/
|
|
108
|
+
export function parseCookies(req) {
|
|
109
|
+
markTainted("parseCookies(req)");
|
|
110
|
+
|
|
111
|
+
const cached = /** @type {any} */ (req)[PARSED];
|
|
112
|
+
if (cached) return cached;
|
|
113
|
+
|
|
114
|
+
/** @type {Record<string, string>} */
|
|
115
|
+
const out = {};
|
|
116
|
+
const header = req.headers?.cookie;
|
|
117
|
+
|
|
118
|
+
if (header) {
|
|
119
|
+
for (const part of header.split(";")) {
|
|
120
|
+
const index = part.indexOf("=");
|
|
121
|
+
if (index === -1) continue;
|
|
122
|
+
|
|
123
|
+
const name = part.slice(0, index).trim();
|
|
124
|
+
if (!name) continue;
|
|
125
|
+
|
|
126
|
+
try {
|
|
127
|
+
out[name] = decodeURIComponent(part.slice(index + 1).trim());
|
|
128
|
+
} catch {
|
|
129
|
+
// Bozuk yüzde kodlaması tüm başlığı çöpe atmamalı.
|
|
130
|
+
out[name] = part.slice(index + 1).trim();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** @type {any} */ (req)[PARSED] = out;
|
|
136
|
+
return out;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* @typedef {object} CookieOptions
|
|
141
|
+
* @property {string} [path] Varsayılan `/`.
|
|
142
|
+
* @property {string} [domain]
|
|
143
|
+
* @property {number} [maxAge] Saniye.
|
|
144
|
+
* @property {Date} [expires]
|
|
145
|
+
* @property {boolean} [httpOnly] Varsayılan `true`.
|
|
146
|
+
* @property {boolean} [secure] Varsayılan: development dışında `true`.
|
|
147
|
+
* `writeSharedCookie` protokole bakarak ezer (`https` → Secure).
|
|
148
|
+
* @property {"Strict" | "Lax" | "None"} [sameSite] Varsayılan `Lax`.
|
|
149
|
+
*/
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* RFC 6265 cookie-name (token). `;`, boşluk, CRLF gibi karakterler
|
|
153
|
+
* Set-Cookie enjeksiyonuna yol açardı — reddedilir.
|
|
154
|
+
*
|
|
155
|
+
* @param {unknown} name
|
|
156
|
+
* @returns {boolean}
|
|
157
|
+
*/
|
|
158
|
+
export function isValidCookieName(name) {
|
|
159
|
+
return typeof name === "string" && /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/.test(name);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Varsayılanlar bilinçli olarak kısıtlayıcı: `HttpOnly` ile JS okuyamaz,
|
|
164
|
+
* `SameSite=Lax` ile çapraz site POST'larında gönderilmez (CSRF'nin büyük
|
|
165
|
+
* kısmını kapatan tek satır), `Secure` üretimde açık.
|
|
166
|
+
*
|
|
167
|
+
* @param {string} name
|
|
168
|
+
* @param {string} value
|
|
169
|
+
* @param {CookieOptions} [options]
|
|
170
|
+
* @returns {string}
|
|
171
|
+
*/
|
|
172
|
+
export function serializeCookie(name, value, options = {}) {
|
|
173
|
+
if (!isValidCookieName(name)) {
|
|
174
|
+
throw new Error(
|
|
175
|
+
`[cookies] invalid cookie name ${JSON.stringify(name)}; ` +
|
|
176
|
+
"names must be RFC 6265 tokens (no spaces, semicolons, or control chars)",
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const parts = [`${name}=${encodeURIComponent(value)}`];
|
|
181
|
+
|
|
182
|
+
parts.push(`Path=${options.path ?? "/"}`);
|
|
183
|
+
if (options.domain) parts.push(`Domain=${options.domain}`);
|
|
184
|
+
if (options.maxAge !== undefined) parts.push(`Max-Age=${Math.floor(options.maxAge)}`);
|
|
185
|
+
if (options.expires) parts.push(`Expires=${options.expires.toUTCString()}`);
|
|
186
|
+
if (options.httpOnly !== false) parts.push("HttpOnly");
|
|
187
|
+
if (options.secure ?? process.env.NODE_ENV !== "development") parts.push("Secure");
|
|
188
|
+
parts.push(`SameSite=${options.sameSite ?? "Lax"}`);
|
|
189
|
+
|
|
190
|
+
return parts.join("; ");
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* @param {import('http').ServerResponse} res
|
|
195
|
+
* @param {string} name
|
|
196
|
+
* @param {string} value
|
|
197
|
+
* @param {CookieOptions} [options]
|
|
198
|
+
*/
|
|
199
|
+
export function setCookie(res, name, value, options = {}) {
|
|
200
|
+
const existing = res.getHeader("Set-Cookie");
|
|
201
|
+
const serialized = serializeCookie(name, value, options);
|
|
202
|
+
|
|
203
|
+
/** @type {string[]} */
|
|
204
|
+
const all = existing
|
|
205
|
+
? Array.isArray(existing)
|
|
206
|
+
? [...existing.map(String)]
|
|
207
|
+
: [String(existing)]
|
|
208
|
+
: [];
|
|
209
|
+
|
|
210
|
+
all.push(serialized);
|
|
211
|
+
res.setHeader("Set-Cookie", all);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* @param {import('http').ServerResponse} res
|
|
216
|
+
* @param {string} name
|
|
217
|
+
* @param {CookieOptions} [options]
|
|
218
|
+
*/
|
|
219
|
+
export function clearCookie(res, name, options = {}) {
|
|
220
|
+
setCookie(res, name, "", { ...options, maxAge: 0, expires: new Date(0) });
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* İmzalı cookie yazar. Değer okunabilir kalır (şifreleme değil, imza);
|
|
225
|
+
* gizli kalması gereken veriyi cookie'ye koymayın, kimliğini koyun.
|
|
226
|
+
*
|
|
227
|
+
* @param {import('http').ServerResponse} res
|
|
228
|
+
* @param {string} name
|
|
229
|
+
* @param {string} value
|
|
230
|
+
* @param {CookieOptions} [options]
|
|
231
|
+
*/
|
|
232
|
+
export function setSignedCookie(res, name, value, options = {}) {
|
|
233
|
+
const encoded = base64url(value);
|
|
234
|
+
setCookie(res, name, `${encoded}.${sign(encoded)}`, options);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* İmzalı cookie okur. İmza uymuyorsa `null` — bozuk imza, yok sayılmalı,
|
|
239
|
+
* "belki geçerlidir" diye kullanılmamalı.
|
|
240
|
+
*
|
|
241
|
+
* @param {import('http').IncomingMessage} req
|
|
242
|
+
* @param {string} name
|
|
243
|
+
* @returns {string | null}
|
|
244
|
+
*/
|
|
245
|
+
export function getSignedCookie(req, name) {
|
|
246
|
+
const raw = parseCookies(req)[name];
|
|
247
|
+
if (!raw) return null;
|
|
248
|
+
|
|
249
|
+
const index = raw.lastIndexOf(".");
|
|
250
|
+
if (index <= 0) return null;
|
|
251
|
+
|
|
252
|
+
const encoded = raw.slice(0, index);
|
|
253
|
+
const signature = raw.slice(index + 1);
|
|
254
|
+
|
|
255
|
+
let expected;
|
|
256
|
+
try {
|
|
257
|
+
expected = sign(encoded);
|
|
258
|
+
} catch {
|
|
259
|
+
// Sır yoksa imzalı okuma sessizce başarısız olur: sunucu ayakta kalır
|
|
260
|
+
// ama hiçbir oturum geçerli sayılmaz.
|
|
261
|
+
return null;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (!safeEqual(signature, expected)) return null;
|
|
265
|
+
return fromBase64url(encoded);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Kriptografik rastgele token. CSRF token'ı ve oturum kimliği için.
|
|
270
|
+
*
|
|
271
|
+
* @param {number} [bytes]
|
|
272
|
+
* @returns {string}
|
|
273
|
+
*/
|
|
274
|
+
export function randomToken(bytes = 32) {
|
|
275
|
+
return crypto.randomBytes(bytes).toString("base64url");
|
|
276
|
+
}
|
|
277
|
+
|