kreftjs 0.0.21-beta → 0.0.22-beta

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/server.d.ts CHANGED
@@ -973,6 +973,23 @@ export declare function namespace(prefix: string): Record<string, any>
973
973
  export declare function startAutoGarbageCollector(options?: { thresholdMB?: number; intervalMs?: number; log?: boolean }): void
974
974
  export declare function stopAutoGarbageCollector(): void
975
975
  export declare function getMemoryStats(): { rssMB: number; heapTotalMB: number; heapUsedMB: number; externalMB: number; gcExposed: boolean }
976
+ export interface MemoryAlert { type: 'memory-high' | 'memory-leak'; message: string; at: string; heapUsedMB: number; rssMB: number; [k: string]: any }
977
+ export interface MemoryReport {
978
+ heapUsedMB: number; heapTotalMB: number; rssMB: number; externalMB: number; gcExposed: boolean
979
+ running: boolean; startedAt: string | null; checks: number; cleanups: number; gcRuns: number; v8Collections: number
980
+ reclaimedMB: number; evictedEntries: number; trendMBPerMin: number | null; overStreak: number
981
+ leak: { suspected: boolean; riseMB: number; floorsMB: number[] }
982
+ alerts: MemoryAlert[]; history: Array<{ t: number; heapUsedMB: number; rssMB: number }>; config: any
983
+ }
984
+ /** Everything a dashboard or alert needs, as plain JSON */
985
+ export declare function getMemoryReport(): MemoryReport
986
+ /** Register a cache cleaner: fn(fraction) drops that share of its least-used entries and returns how many went */
987
+ export declare function onMemoryPressure(name: string, fn: (fraction: number) => number | void): () => void
988
+ /** Called when memory stays high after cleaning, or a leak is suspected */
989
+ export declare function onMemoryAlert(fn: (alert: MemoryAlert) => void): () => void
990
+ /** Writes a V8 heap snapshot (open in Chrome DevTools > Memory); returns the file path */
991
+ export declare function writeHeapSnapshot(file?: string): string
992
+ export declare function checkMemory(): { action: string; [k: string]: any }
976
993
 
977
994
  /** Native Model Context Protocol (MCP) Server */
978
995
  export declare class McpServer {
@@ -984,6 +1001,31 @@ export declare class McpServer {
984
1001
  }
985
1002
  export declare function getMcpServer(options?: { baseDir?: string }): McpServer
986
1003
 
1004
+ /** Kreft Shield: SHIELD=true in .env limits and bans abusive addresses */
1005
+ export interface ShieldBan { ip: string; until: string; level: number; secondsLeft: number }
1006
+ export declare const Shield: {
1007
+ enabled(): boolean
1008
+ bans(): ShieldBan[]
1009
+ ban(ip: string, minutes?: number, reason?: string): { ip: string; minutes: number; reason: string; until: string; level: number }
1010
+ unban(ip: string): boolean
1011
+ status(): { enabled: boolean; tracked: number; banned: number; blocked: number; limited: number; bans: number; eventLoopLagMs: number; overloaded: boolean; limits: Record<string, number> }
1012
+ on(event: 'ban', fn: (info: { ip: string; minutes: number; reason: string; until: string; level: number }) => void): typeof Shield
1013
+ off(event: 'ban', fn: (...a: any[]) => void): typeof Shield
1014
+ }
1015
+
1016
+ /** MCP client (Streamable HTTP): call tools on a remote MCP server */
1017
+ export declare class McpClient {
1018
+ constructor(url: string, options?: { headers?: Record<string, string>; timeout?: number })
1019
+ server: any
1020
+ connect(): Promise<this>
1021
+ tools(): Promise<Array<{ name: string; description?: string; inputSchema?: any }>>
1022
+ call(name: string, args?: Record<string, any>): Promise<any>
1023
+ resources(): Promise<any[]>
1024
+ readResource(uri: string): Promise<any>
1025
+ close(): Promise<void>
1026
+ }
1027
+ export declare const Mcp: { connect(url: string, options?: { headers?: Record<string, string>; timeout?: number }): Promise<McpClient> }
1028
+
987
1029
  export interface CleanHtmlOptions {
988
1030
  allowedTags?: string[]
989
1031
  allowedAttributes?: Record<string, string[]>
@@ -6,11 +6,15 @@
6
6
  //
7
7
  // An app or script that sends `device_name` when it logs in gets an API token in the answer instead:
8
8
  // Authorization: Bearer 5|k3J9…
9
- const { response, Str, Crypt, cookieSecure } = require('kreftjs/server')
9
+ const { response, Str, Crypt, Hash, cookieSecure } = require('kreftjs/server')
10
10
  const PersonalAccessToken = require('../Models/PersonalAccessToken')
11
11
 
12
12
  const SESSION = 'kreft_session'
13
13
  const XSRF = 'XSRF-TOKEN'
14
+ // The XSRF-TOKEN cookie is not a random value anyone could plant: it is derived from this login's secret token, so a
15
+ // page on a sibling subdomain (or a script that can only set cookies) cannot make one that matches. Only a page that
16
+ // can read this browser's cookie, i.e. your own, can send it back in X-XSRF-TOKEN.
17
+ const xsrfFor = (sessionToken) => Hash.sha256('xsrf|' + sessionToken)
14
18
  const days = () => Number(process.env.AUTH_TOKEN_DAYS || 30)
15
19
 
16
20
  const resolveSecure = () => typeof cookieSecure === 'function' ? cookieSecure() : (process.env.SESSION_SECURE_COOKIE ? process.env.SESSION_SECURE_COOKIE === 'true' : (process.env.APP_ENV === 'production' || /^https:/i.test(process.env.APP_URL || '')))
@@ -57,7 +61,7 @@ async function signIn(req, user, { remember = false, status = 200 } = {}) {
57
61
  path: '/',
58
62
  maxAge: remember ? days() * 86400 : undefined,
59
63
  })
60
- res.cookie(XSRF, Str.random(40), {
64
+ res.cookie(XSRF, xsrfFor(plainTextToken), {
61
65
  httpOnly: false,
62
66
  secure: isSecure,
63
67
  sameSite,
@@ -99,7 +103,7 @@ async function regenerate(req, res, user = null) {
99
103
  path: '/',
100
104
  maxAge: remember ? days() * 86400 : undefined,
101
105
  })
102
- res.cookie(XSRF, Str.random(40), {
106
+ res.cookie(XSRF, xsrfFor(plainTextToken), {
103
107
  httpOnly: false,
104
108
  secure: isSecure,
105
109
  sameSite,
@@ -174,4 +178,4 @@ function browserName(ua) {
174
178
  return os ? `${b} on ${os}` : b
175
179
  }
176
180
 
177
- module.exports = { signIn, signOut, regenerate, pendingTwoFactor, readPendingTwoFactor, recordTwoFactorFailure, consumeTwoFactorTicket, browserName, SESSION, XSRF }
181
+ module.exports = { xsrfFor, signIn, signOut, regenerate, pendingTwoFactor, readPendingTwoFactor, recordTwoFactorFailure, consumeTwoFactorTicket, browserName, SESSION, XSRF }
@@ -5,7 +5,7 @@ const { abort, Hash, Str, Crypt, response, cookieSecure } = require('kreftjs/ser
5
5
  const crypto = require('crypto')
6
6
  const PersonalAccessToken = require('../../Models/PersonalAccessToken')
7
7
  const User = require('../../Models/User')
8
- const { SESSION, XSRF } = require('../../Auth/Session')
8
+ const { SESSION, XSRF, xsrfFor } = require('../../Auth/Session')
9
9
 
10
10
  const SAFE = ['GET', 'HEAD', 'OPTIONS']
11
11
  const NEEDS = { GET: 'read', HEAD: 'read', OPTIONS: 'read', POST: 'create', PUT: 'update', PATCH: 'update', DELETE: 'delete' }
@@ -41,8 +41,11 @@ class Authenticate {
41
41
  abort(401, 'Unauthenticated.', extra)
42
42
  }
43
43
 
44
- // a cookie login must prove the page is ours for every change: X-XSRF-TOKEN = the XSRF-TOKEN cookie
45
- if (fromCookie && !SAFE.includes(req.method) && !same(req.header('x-xsrf-token'), req.cookie(XSRF))) {
44
+ // a cookie login must prove the page is ours for every change: X-XSRF-TOKEN must be the value made from this login's
45
+ // secret (Session.xsrfFor), which the XSRF-TOKEN cookie carries. A planted cookie is not enough, as it would be if
46
+ // the header only had to equal the cookie.
47
+ const expectedXsrf = fromCookie ? xsrfFor(sessionPayload) : null
48
+ if (fromCookie && !SAFE.includes(req.method) && !same(req.header('x-xsrf-token'), expectedXsrf)) {
46
49
  abort(419, 'CSRF token mismatch. Reload the page and try again.')
47
50
  }
48
51
 
@@ -76,7 +79,7 @@ class Authenticate {
76
79
  req.accessToken = token
77
80
  const res = await next(req)
78
81
  // a cookie login without the XSRF cookie (cleared, or an old browser): give it a new one
79
- if (fromCookie && !req.cookie(XSRF)) res.cookie(XSRF, Str.random(40), { httpOnly: false, secure: cookieSecure(), sameSite: process.env.SESSION_SAME_SITE || 'Lax', path: '/', maxAge: token.remember ? days * 86400 : undefined })
82
+ if (fromCookie && req.cookie(XSRF) !== expectedXsrf) res.cookie(XSRF, expectedXsrf, { httpOnly: false, secure: cookieSecure(), sameSite: process.env.SESSION_SAME_SITE || 'Lax', path: '/', maxAge: token.remember ? days * 86400 : undefined })
80
83
  return res
81
84
  }
82
85
  }
@@ -5,6 +5,10 @@ const User = require('../../app/Models/User')
5
5
  // that user is an admin too, so /admin opens for it
6
6
  class DatabaseSeeder extends Seeder {
7
7
  async run() {
8
+ // an admin whose password is "password" must never exist on a live site: this seeder is for development only
9
+ if (process.env.APP_ENV === 'production' || process.env.NODE_ENV === 'production') {
10
+ throw new Error('DatabaseSeeder creates an admin with the password "password" and refuses to run in production. Seed real data instead.')
11
+ }
8
12
  const user = await User.create({ name: 'Test User', email: 'test@example.com', password: Hash.make('password') })
9
13
  user.email_verified_at = new Date()
10
14
  user.is_admin = true // never fillable: only set like this, by your own code
@@ -1,4 +1,4 @@
1
- // Type definitions for Kreft 0.0.21-beta
1
+ // Type definitions for Kreft 0.0.22-beta
2
2
  // Use from TypeScript or for editor hints in plain JavaScript (// @ts-check).
3
3
 
4
4
  export interface Signal<T> { value: T }
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * Kreft v0.0.21-beta — reactive by nature
2
+ * Kreft v0.0.22-beta — reactive by nature
3
3
  * The .kreft component language (state · derive · action · when · watch · on),
4
4
  * { } views with k-if / k-each / k-model, components, slots, router, stores,
5
5
  * share / receive, event bus and a template engine.
@@ -5601,7 +5601,7 @@
5601
5601
  stores, storeLog, busLog,
5602
5602
  };
5603
5603
 
5604
- const VERSION = '0.0.21-beta';
5604
+ const VERSION = '0.0.22-beta';
5605
5605
  const Kreftjs = {
5606
5606
  version: VERSION,
5607
5607
  // PUBLIC_ values from .env (kreft dev and kreft build put them here); never secrets
Binary file