@lowdefy/server 6.0.0 → 7.0.0

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.
Files changed (119) hide show
  1. package/client/App.jsx +41 -21
  2. package/client/Page.jsx +101 -24
  3. package/client/clearReloadedForTypes.js +29 -0
  4. package/client/clientBuildImports.test.mjs +60 -0
  5. package/client/getShownPage.js +28 -0
  6. package/client/getShownPage.test.mjs +52 -0
  7. package/client/loadAllIcons.js +45 -0
  8. package/client/loadPageTypes.js +44 -0
  9. package/client/main.jsx +22 -1
  10. package/client/reloadedForTypesKey.js +21 -0
  11. package/client/shouldReloadForTypes.js +38 -0
  12. package/client/shouldReloadForTypes.test.mjs +63 -0
  13. package/client/types.js +28 -0
  14. package/lib/build/security.js +26 -0
  15. package/lib/client/auth/Auth.jsx +11 -6
  16. package/lib/client/auth/AuthConfigured.jsx +224 -39
  17. package/lib/client/auth/AuthNotConfigured.js +44 -4
  18. package/lib/client/auth/createFetchCheckingBuild.js +45 -0
  19. package/lib/client/auth/createFetchCheckingBuild.test.mjs +100 -0
  20. package/lib/client/bundleBuildId.js +23 -0
  21. package/lib/client/createLogUsage.js +46 -25
  22. package/lib/client/createLogUsage.test.mjs +90 -0
  23. package/lib/client/getCspNonce.js +25 -0
  24. package/lib/server/auth/createSystemContext.js +70 -0
  25. package/lib/server/auth/{getAuthConfig.js → getAuth.js} +15 -7
  26. package/lib/server/auth/getStrategies.js +40 -0
  27. package/lib/server/getClientAddress.js +56 -0
  28. package/lib/server/getClientAddress.test.mjs +108 -0
  29. package/lib/server/log/createHandleError.js +16 -4
  30. package/lib/server/log/createHandleError.test.mjs +102 -0
  31. package/lib/server/log/createLogger.js +10 -0
  32. package/lib/server/log/createLogger.test.mjs +117 -0
  33. package/lib/server/log/logRequest.js +12 -4
  34. package/lib/server/log/maskCredentialKeys.js +37 -0
  35. package/lib/server/log/projectErrorForLog.js +61 -0
  36. package/lib/server/log/redactUrlQuery.js +75 -0
  37. package/lib/server/log/redactUrlQuery.test.mjs +73 -0
  38. package/lib/server/{auth/session.js → log/serializeErrorForLog.js} +5 -11
  39. package/lib/server/log/serializeErrorForLog.test.mjs +337 -0
  40. package/lib/server/scrubSecrets.js +21 -0
  41. package/lib/server/sentry/captureSentryError.js +29 -3
  42. package/lib/server/sentry/captureSentryError.test.mjs +128 -0
  43. package/lib/server/sentry/initSentry.js +46 -14
  44. package/lib/server/sentry/initSentry.serverless.test.mjs +165 -0
  45. package/lib/server/sentry/initSentry.test.mjs +365 -0
  46. package/lib/server/sentry/scrubEvent.js +40 -0
  47. package/lib/server/sentry/scrubEvent.test.mjs +88 -0
  48. package/lib/server/sentry/sentrySdk.js +23 -0
  49. package/lib/server/sentry/setSentryUser.js +3 -3
  50. package/lowdefy/build.mjs +1 -0
  51. package/lowdefy/build.test.mjs +68 -0
  52. package/lowdefy/createCustomPluginTypesMap.mjs +3 -2
  53. package/package.json +47 -42
  54. package/package.original.json +47 -42
  55. package/src/app.js +74 -17
  56. package/src/app.test.mjs +118 -0
  57. package/src/html/collectChunkClosure.js +31 -0
  58. package/src/html/collectLazyImports.js +41 -0
  59. package/src/html/collectPageTypesAssets.js +57 -0
  60. package/src/html/collectPageTypesAssets.test.mjs +130 -0
  61. package/src/html/getAssets.js +20 -1
  62. package/src/html/getHomePath.js +29 -0
  63. package/src/html/getPageAssets.js +32 -0
  64. package/src/html/getPageAssets.test.mjs +47 -0
  65. package/src/html/renderPage.contentSecurityPolicy.test.mjs +134 -0
  66. package/src/html/renderPage.js +80 -19
  67. package/src/html/renderPage.test.mjs +189 -0
  68. package/src/html/template.js +50 -15
  69. package/src/html/template.test.mjs +126 -0
  70. package/src/index.js +28 -19
  71. package/src/initServer.js +61 -0
  72. package/src/initServer.test.mjs +116 -0
  73. package/src/lib/getRequestPath.js +29 -0
  74. package/src/middleware/apiContext.js +68 -13
  75. package/src/middleware/apiContext.test.mjs +178 -0
  76. package/src/middleware/createSameOriginGuard.js +36 -0
  77. package/src/middleware/createSameOriginGuard.test.mjs +102 -0
  78. package/src/middleware/errorHandler.js +56 -4
  79. package/src/middleware/errorHandler.test.mjs +440 -0
  80. package/src/middleware/noStore.js +27 -0
  81. package/src/middleware/refuseOtherBuild.js +49 -0
  82. package/src/middleware/refuseOtherBuild.test.mjs +63 -0
  83. package/src/middleware/requestTimeout.js +38 -0
  84. package/src/middleware/sentry.js +13 -5
  85. package/src/middleware/stampBuildId.js +30 -0
  86. package/src/middleware/stampBuildId.test.mjs +41 -0
  87. package/src/middleware/stampCaller.js +38 -0
  88. package/src/middleware/stampCaller.test.mjs +64 -0
  89. package/src/routes/agent.js +15 -3
  90. package/src/routes/apiPage.js +54 -9
  91. package/src/routes/apiPage.test.mjs +143 -0
  92. package/src/routes/auth.js +15 -9
  93. package/src/routes/auth.test.mjs +85 -0
  94. package/src/routes/clientError.js +11 -13
  95. package/src/routes/cron.js +3 -1
  96. package/src/routes/cronForward.js +4 -2
  97. package/src/routes/detached.js +22 -9
  98. package/src/routes/endpoints.js +27 -26
  99. package/src/routes/endpointsTextAnswer.test.mjs +77 -0
  100. package/src/routes/mcp.js +82 -5
  101. package/src/routes/mcp.test.mjs +215 -0
  102. package/src/routes/mcpMemberToken.test.mjs +125 -0
  103. package/src/routes/mcpProtectedResourceMetadata.js +34 -0
  104. package/src/routes/mcpProtectedResourceMetadata.test.mjs +65 -0
  105. package/src/routes/mountOauthDiscovery.js +44 -0
  106. package/src/routes/mountOauthDiscovery.test.mjs +79 -0
  107. package/src/routes/mountPageRoutes.js +29 -0
  108. package/src/routes/mountPageRoutes.test.mjs +56 -0
  109. package/src/routes/normalizeMcpProtocolVersionHeader.js +46 -0
  110. package/src/routes/normalizeMcpProtocolVersionHeader.test.mjs +62 -0
  111. package/src/routes/request.js +5 -3
  112. package/src/routes/requestBody.test.mjs +146 -0
  113. package/src/routes/usage.js +6 -2
  114. package/src/routes/user.js +27 -0
  115. package/src/routes/websocket.js +26 -3
  116. package/src/routes/websocket.test.mjs +106 -0
  117. package/src/routes/wellKnownFallback.js +30 -0
  118. package/vite.config.js +55 -2
  119. package/lib/server/apiWrapper.js +0 -87
@@ -0,0 +1,61 @@
1
+ /*
2
+ Copyright 2020-2026 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ import checkEnvironmentGuards from '@lowdefy/node-utils/checkEnvironmentGuards.js';
18
+
19
+ import initSentryServer from '../lib/server/sentry/initSentry.js';
20
+
21
+ // The startup every production entry runs before it serves: the Node server (src/index.js) and the
22
+ // Vercel function entry that `lowdefy vercel-output` generates. Keeping it in one place stops the
23
+ // entries drifting apart. The build artifacts are read relative to process.cwd(), so an entry that
24
+ // has to change directory imports this module dynamically, after the chdir. A serverless entry
25
+ // passes serverless: true, so Sentry reports errors only unless the app turns tracing on.
26
+ async function initServer({ serverless = false } = {}) {
27
+ const sentry = await initSentryServer({ serverless });
28
+
29
+ // The build checked the current environment's guards, but an image built once can be started
30
+ // with different variables, so they are checked again before the server takes any traffic — and
31
+ // before the app loads, so a misconfigured start fails without importing every plugin under the
32
+ // wrong variables. The failure is sent to Sentry here because the Vercel launcher catches errors
33
+ // thrown while it imports the function entry, so no global handler would report it, and the
34
+ // instance can be torn down before a queued event is sent.
35
+ const { default: config } = await import('../lib/build/config.js');
36
+ try {
37
+ checkEnvironmentGuards({
38
+ name: config.environment,
39
+ guards: config.environments?.[config.environment]?.guards,
40
+ });
41
+ } catch (error) {
42
+ if (sentry) {
43
+ sentry.captureException(error);
44
+ await sentry.flush(2000);
45
+ }
46
+ throw error;
47
+ }
48
+
49
+ // Import after Sentry init so instrumentation observes the module graph.
50
+ const { default: createApp } = await import('./app.js');
51
+ const { default: createLogger } = await import('../lib/server/log/createLogger.js');
52
+
53
+ const logger = createLogger({ server: 'lowdefy' });
54
+ if (sentry) {
55
+ logger.info('Sentry enabled: server');
56
+ }
57
+
58
+ return { createApp, logger, sentry };
59
+ }
60
+
61
+ export default initServer;
@@ -0,0 +1,116 @@
1
+ /*
2
+ Copyright 2020-2026 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ import { jest } from '@jest/globals';
18
+
19
+ const calls = [];
20
+ const info = jest.fn();
21
+ const guards = { env: { BETTER_AUTH_URL: '^https://app\\.example\\.com$' } };
22
+
23
+ const checkEnvironmentGuards = jest.fn(() => {
24
+ calls.push('checkEnvironmentGuards');
25
+ });
26
+ const captureException = jest.fn();
27
+ const flush = jest.fn(async () => true);
28
+ const sentry = { captureException, flush };
29
+ const initSentryServer = jest.fn(async () => {
30
+ calls.push('initSentryServer');
31
+ return null;
32
+ });
33
+ const createApp = jest.fn();
34
+
35
+ jest.unstable_mockModule('@lowdefy/node-utils/checkEnvironmentGuards.js', () => ({
36
+ default: checkEnvironmentGuards,
37
+ }));
38
+ jest.unstable_mockModule('../lib/server/sentry/initSentry.js', () => ({
39
+ default: initSentryServer,
40
+ }));
41
+ jest.unstable_mockModule('./app.js', () => {
42
+ calls.push('import app');
43
+ return { default: createApp };
44
+ });
45
+ jest.unstable_mockModule('../lib/server/log/createLogger.js', () => ({
46
+ default: () => ({ info }),
47
+ }));
48
+ jest.unstable_mockModule('../lib/build/config.js', () => ({
49
+ default: { environment: 'prod', environments: { prod: { guards } } },
50
+ }));
51
+
52
+ const { default: initServer } = await import('./initServer.js');
53
+
54
+ // The app module is imported once and then cached, so find the run that imported it rather than
55
+ // assuming this test runs first.
56
+ test('initServer initialises Sentry and checks the guards before importing the app', async () => {
57
+ const result = await initServer();
58
+ const importIndex = calls.indexOf('import app');
59
+ expect(calls.slice(importIndex - 2, importIndex + 1)).toEqual([
60
+ 'initSentryServer',
61
+ 'checkEnvironmentGuards',
62
+ 'import app',
63
+ ]);
64
+ expect(result.createApp).toBe(createApp);
65
+ });
66
+
67
+ test('initServer checks the current environment guards', async () => {
68
+ await initServer();
69
+ expect(checkEnvironmentGuards.mock.calls).toEqual([[{ name: 'prod', guards }]]);
70
+ });
71
+
72
+ test('initServer throws when the current environment guards fail', async () => {
73
+ checkEnvironmentGuards.mockImplementationOnce(() => {
74
+ throw new Error('Environment "prod" guards failed.');
75
+ });
76
+ await expect(initServer()).rejects.toThrow('Environment "prod" guards failed.');
77
+ });
78
+
79
+ test('initServer sends a guard failure to Sentry and flushes it before throwing', async () => {
80
+ const error = new Error('Environment "prod" guards failed.');
81
+ initSentryServer.mockResolvedValueOnce(sentry);
82
+ checkEnvironmentGuards.mockImplementationOnce(() => {
83
+ throw error;
84
+ });
85
+ await expect(initServer()).rejects.toBe(error);
86
+ expect(captureException.mock.calls).toEqual([[error]]);
87
+ expect(flush).toHaveBeenCalled();
88
+ });
89
+
90
+ test('initServer throws a guard failure without Sentry when Sentry was not started', async () => {
91
+ const error = new Error('Environment "prod" guards failed.');
92
+ checkEnvironmentGuards.mockImplementationOnce(() => {
93
+ throw error;
94
+ });
95
+ await expect(initServer()).rejects.toBe(error);
96
+ expect(captureException).not.toHaveBeenCalled();
97
+ });
98
+
99
+ test('initServer starts Sentry for a long-running server unless the entry is serverless', async () => {
100
+ await initServer();
101
+ await initServer({ serverless: true });
102
+ expect(initSentryServer.mock.calls).toEqual([[{ serverless: false }], [{ serverless: true }]]);
103
+ });
104
+
105
+ test('initServer logs that Sentry is enabled and returns its SDK when Sentry was started', async () => {
106
+ initSentryServer.mockResolvedValueOnce(sentry);
107
+ const result = await initServer();
108
+ expect(result.sentry).toBe(sentry);
109
+ expect(info.mock.calls).toEqual([['Sentry enabled: server']]);
110
+ });
111
+
112
+ test('initServer does not log Sentry when Sentry was not started', async () => {
113
+ const result = await initServer();
114
+ expect(result.sentry).toBe(null);
115
+ expect(info).not.toHaveBeenCalled();
116
+ });
@@ -0,0 +1,29 @@
1
+ /*
2
+ Copyright 2020-2026 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ // The rest of the request path after basePath and prefix, still encoded: the
18
+ // page matcher decodes each segment once, and Hono's c.req.path is partly
19
+ // decoded, so it is taken from the request URL. path goes to the matcher as it
20
+ // arrived; matchedPath is path with one trailing "/" removed, the form the
21
+ // client compares its navigations with.
22
+ function getRequestPath({ c, basePath, prefix }) {
23
+ const pathname = new URL(c.req.url).pathname.slice(basePath.length);
24
+ const path = pathname.startsWith(prefix) ? pathname.slice(prefix.length) : '';
25
+ const matchedPath = path.endsWith('/') ? path.slice(0, -1) : path;
26
+ return { path, matchedPath };
27
+ }
28
+
29
+ export default getRequestPath;
@@ -15,7 +15,14 @@
15
15
  */
16
16
 
17
17
  import path from 'node:path';
18
- import { createApiContext } from '@lowdefy/api';
18
+ import {
19
+ createApiContext,
20
+ createRequestSignal,
21
+ ensureMcpOauthResource,
22
+ resolveAuthentication,
23
+ resolvePinnedOrganization,
24
+ resolveTenantPreflight,
25
+ } from '@lowdefy/api';
19
26
  import { getSecretsFromEnv } from '@lowdefy/node-utils';
20
27
  import { v4 as uuid } from 'uuid';
21
28
 
@@ -23,21 +30,25 @@ import agents from '../../build/plugins/agents.js';
23
30
  import appMeta from '../../lib/build/appMeta.js';
24
31
  import config from '../../lib/build/config.js';
25
32
  import connections from '../../build/plugins/connections.js';
33
+ import captureSentryError from '../../lib/server/sentry/captureSentryError.js';
26
34
  import createHandleError from '../../lib/server/log/createHandleError.js';
27
35
  import createLogger from '../../lib/server/log/createLogger.js';
28
36
  import fileCache from '../../lib/server/fileCache.js';
29
- import getSession from '../../lib/server/auth/session.js';
30
- import getStrategyCaller from '../../lib/server/auth/strategies.js';
37
+ import getAuth from '../../lib/server/auth/getAuth.js';
38
+ import getClientAddress from '../../lib/server/getClientAddress.js';
39
+ import getStrategies from '../../lib/server/auth/getStrategies.js';
31
40
  import i18nConfig from '../../lib/build/i18n.js';
32
41
  import jsMap from '../../build/plugins/operators/serverJsMap.js';
33
42
  import logRequest from '../../lib/server/log/logRequest.js';
34
43
  import loggerConfig from '../../lib/build/logger.js';
44
+ import scrubSecrets from '../../lib/server/scrubSecrets.js';
35
45
  import notifications, {
36
46
  interpolateProperties,
37
47
  renderEmail,
38
48
  } from '../../build/plugins/notifications.js';
39
49
  import operators from '../../build/plugins/operators/server.js';
40
50
  import setSentryUser from '../../lib/server/sentry/setSentryUser.js';
51
+ import steps from '../../build/plugins/steps.js';
41
52
  import websockets from '../../build/plugins/websockets.js';
42
53
 
43
54
  const secrets = getSecretsFromEnv();
@@ -55,11 +66,19 @@ function getRequestId(c) {
55
66
  return uuid();
56
67
  }
57
68
 
69
+ // The MCP route authenticates by access token alone - the mcp option switches
70
+ // resolveAuthentication onto its bearer branch, so it is set only for the MCP
71
+ // path. Every other path leaves it false and authentication behaves as before.
72
+ function isMcpPath(path) {
73
+ return path.endsWith('/api/mcp');
74
+ }
75
+
58
76
  // Replaces lib/server/apiWrapper.js. Builds the request context consumed by
59
77
  // @lowdefy/api functions. Errors thrown by handlers are routed by Hono to the
60
78
  // app-level error handler (src/middleware/errorHandler.js), which reads this
61
- // context back from the Hono context.
62
- function apiContext() {
79
+ // context back from the Hono context. clientAddressHeader is set by a platform
80
+ // entry whose edge supplies the client address (see getClientAddress).
81
+ function apiContext({ clientAddressHeader } = {}) {
63
82
  return async function apiContextMiddleware(c, next) {
64
83
  // The page-route mount ('/*') also matches /api/* paths that already
65
84
  // built a context — never build twice for one request.
@@ -83,6 +102,7 @@ function apiContext() {
83
102
  interpolateProperties,
84
103
  jsMap,
85
104
  logger: createLogger({ rid }),
105
+ mode: 'prod',
86
106
  notifications,
87
107
  operators,
88
108
  renderEmail,
@@ -91,7 +111,15 @@ function apiContext() {
91
111
  method: c.req.method,
92
112
  hostname: c.req.header('host'),
93
113
  },
114
+ scrubSecrets,
94
115
  secrets,
116
+ // Aborts when the client disconnects before the response is sent, or when the
117
+ // request timeout (src/middleware/requestTimeout.js) answers first.
118
+ signal: createRequestSignal({
119
+ clientSignal: c.req.raw.signal,
120
+ timeoutSignal: c.get('requestTimeoutSignal'),
121
+ }),
122
+ steps,
95
123
  // On Vercel (fluid compute) the platform request context keeps the
96
124
  // invocation alive until waitUntil promises settle; on long-lived hosts
97
125
  // the lookup resolves to nothing and background promises just run.
@@ -100,21 +128,48 @@ function apiContext() {
100
128
  websockets,
101
129
  };
102
130
  context.handleError = createHandleError({ context });
131
+ // Resolved once per request: the auth route hands it to BetterAuth, and the
132
+ // request log records it.
133
+ context.clientAddress = getClientAddress({
134
+ c,
135
+ clientAddressHeader,
136
+ logger: context.logger,
137
+ });
138
+ // Hoisted once per request - resolveAuthentication also needs it, and
139
+ // getBetterAuth memoizes the instance, but this keeps the auth engine
140
+ // construction to a single call site per request.
141
+ context.auth = getAuth({ logger: context.logger });
142
+ // The engine is constructed lazily on the first request, which would
143
+ // otherwise race the startup pinned-org ensure - await the memoized
144
+ // resolve so createApiContext reads a retained binding.
145
+ await resolvePinnedOrganization({ auth: context.auth, logger: context.logger });
146
+ // The one oauthResource row the MCP token audience validates against -
147
+ // ensured once per process, memoized like the pinned resolve above; a
148
+ // failure retries on the next request. No-op when the app is not an
149
+ // authorization server.
150
+ await ensureMcpOauthResource({ auth: context.auth, logger: context.logger });
103
151
  if (!c.req.path.includes('/api/auth')) {
104
- context.session = await getSession(c);
105
- if (!context.session?.user) {
106
- const caller = await getStrategyCaller(c, context.logger);
107
- if (caller) {
108
- context.session = { user: caller };
109
- }
110
- }
152
+ // resolveAuthentication is the single writer of context.user.
153
+ await resolveAuthentication(context, {
154
+ auth: context.auth,
155
+ headers: c.req.raw.headers,
156
+ strategies: getStrategies({ logger: context.logger }),
157
+ mcp: isMcpPath(c.req.path),
158
+ });
111
159
  // Set Sentry user context for authenticated requests
112
160
  setSentryUser({
113
- user: context.session?.user,
161
+ user: context.user,
114
162
  sentryConfig: loggerConfig.sentry,
115
163
  });
116
164
  }
117
165
  createApiContext(context);
166
+ // Under policy: tenant, report walled collections that hold unstamped rows
167
+ // (lazily-run-once, logged and captured once per offending collection). It
168
+ // never blocks or fails a request - a data fault must not take the app
169
+ // down - so it is started, not awaited. No-op under pinned.
170
+ void resolveTenantPreflight(context, {
171
+ captureError: (error) => captureSentryError({ error, context }),
172
+ });
118
173
  c.set('lowdefyContext', context);
119
174
  // Echo the request id so clients and proxies can quote it when reporting
120
175
  // a failure, and it can be matched to the rid on the server log lines.
@@ -0,0 +1,178 @@
1
+ /*
2
+ Copyright 2020-2026 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ import fs from 'fs';
18
+ import path from 'path';
19
+ import { fileURLToPath } from 'url';
20
+
21
+ import { Hono } from 'hono';
22
+ import { jest } from '@jest/globals';
23
+
24
+ const secret = 'planted-apicontext-secret';
25
+ process.env.LOWDEFY_SECRET_TEST = secret;
26
+
27
+ jest.unstable_mockModule('@lowdefy/api', () => ({
28
+ createApiContext: jest.fn(),
29
+ createRequestSignal: jest.fn(({ clientSignal, timeoutSignal }) =>
30
+ AbortSignal.any([clientSignal, timeoutSignal].filter(Boolean))
31
+ ),
32
+ // Returns what the server passes, so the test reads the auth-hook system context inputs.
33
+ createSystemContext: jest.fn((options) => options),
34
+ ensureMcpOauthResource: jest.fn(async () => {}),
35
+ resolveAuthentication: jest.fn(async () => {}),
36
+ resolvePinnedOrganization: jest.fn(async () => {}),
37
+ resolveTenantPreflight: jest.fn(async () => {}),
38
+ }));
39
+ jest.unstable_mockModule('../../lib/build/appMeta.js', () => ({ default: {} }));
40
+ jest.unstable_mockModule('../../lib/build/config.js', () => ({ default: {} }));
41
+ jest.unstable_mockModule('../../lib/build/i18n.js', () => ({ default: {} }));
42
+ jest.unstable_mockModule('../../lib/build/logger.js', () => ({ default: { sentry: {} } }));
43
+ jest.unstable_mockModule('../../lib/server/log/createHandleError.js', () => ({
44
+ default: jest.fn(() => jest.fn()),
45
+ }));
46
+ jest.unstable_mockModule('../../lib/server/log/createLogger.js', () => ({
47
+ default: jest.fn(() => ({
48
+ debug: jest.fn(),
49
+ info: jest.fn(),
50
+ warn: jest.fn(),
51
+ error: jest.fn(),
52
+ })),
53
+ }));
54
+ jest.unstable_mockModule('../../lib/server/fileCache.js', () => ({ default: {} }));
55
+ jest.unstable_mockModule('../../lib/server/auth/getAuth.js', () => ({
56
+ default: jest.fn(() => null),
57
+ }));
58
+ jest.unstable_mockModule('../../lib/server/auth/getStrategies.js', () => ({
59
+ default: jest.fn(() => null),
60
+ }));
61
+ jest.unstable_mockModule('../../lib/server/log/logRequest.js', () => ({ default: jest.fn() }));
62
+ jest.unstable_mockModule('../../lib/server/sentry/setSentryUser.js', () => ({
63
+ default: jest.fn(),
64
+ }));
65
+
66
+ // apiContext statically imports the app build's plugin artifacts, which only
67
+ // exist in a built app (build/** is gitignored). Materialize the minimal set
68
+ // it needs so the import resolves; clean up only what this test created.
69
+ const dirname = path.dirname(fileURLToPath(import.meta.url));
70
+ const pluginsDir = path.resolve(dirname, '../../build/plugins');
71
+ const fixtures = [
72
+ { file: 'agents.js', content: 'export default {};\n' },
73
+ { file: 'connections.js', content: 'export default {};\n' },
74
+ {
75
+ file: 'notifications.js',
76
+ content:
77
+ 'export default {};\nexport const interpolateProperties = () => {};\nexport const renderEmail = () => {};\n',
78
+ },
79
+ { file: path.join('operators', 'server.js'), content: 'export default {};\n' },
80
+ { file: path.join('operators', 'serverJsMap.js'), content: 'export default {};\n' },
81
+ { file: 'steps.js', content: 'export default {};\n' },
82
+ { file: 'websockets.js', content: 'export default {};\n' },
83
+ ].map(({ file, content }) => ({ absolutePath: path.join(pluginsDir, file), content }));
84
+
85
+ const createdFixtures = [];
86
+ fixtures.forEach(({ absolutePath, content }) => {
87
+ if (!fs.existsSync(absolutePath)) {
88
+ fs.mkdirSync(path.dirname(absolutePath), { recursive: true });
89
+ fs.writeFileSync(absolutePath, content);
90
+ createdFixtures.push(absolutePath);
91
+ }
92
+ });
93
+
94
+ // A checkout that has built an app already holds real artifacts here, which import plugins this
95
+ // package does not depend on, so the stubs are also mocked over whatever is on disk.
96
+ fixtures.forEach(({ absolutePath }) => {
97
+ jest.unstable_mockModule(absolutePath, () => ({
98
+ default: {},
99
+ interpolateProperties: () => {},
100
+ renderEmail: () => {},
101
+ }));
102
+ });
103
+
104
+ afterAll(() => {
105
+ createdFixtures.forEach((absolutePath) => fs.rmSync(absolutePath, { force: true }));
106
+ });
107
+
108
+ const { default: apiContext } = await import('./apiContext.js');
109
+ const { default: requestTimeout } = await import('./requestTimeout.js');
110
+ const { default: createSystemContext } = await import(
111
+ '../../lib/server/auth/createSystemContext.js'
112
+ );
113
+
114
+ function createApp() {
115
+ const app = new Hono();
116
+ app.use('*', apiContext());
117
+ app.get('/api/ping', (c) => {
118
+ const context = c.get('lowdefyContext');
119
+ return c.json({
120
+ mode: context.mode,
121
+ scrubbed: context.scrubSecrets(`token ${secret} end`),
122
+ });
123
+ });
124
+ return app;
125
+ }
126
+
127
+ test('apiContext sets mode to prod', async () => {
128
+ const res = await createApp().request('/api/ping');
129
+ expect(res.status).toEqual(200);
130
+ const body = await res.json();
131
+ expect(body.mode).toEqual('prod');
132
+ });
133
+
134
+ test('apiContext scrubSecrets redacts a planted secret', async () => {
135
+ const res = await createApp().request('/api/ping');
136
+ expect(res.status).toEqual(200);
137
+ const body = await res.json();
138
+ expect(body.scrubbed).toEqual('token [REDACTED] end');
139
+ });
140
+
141
+ test('the auth-hook system context is built with mode prod and a scrubSecrets that redacts', () => {
142
+ const context = createSystemContext({ auth: null });
143
+ expect(context.mode).toEqual('prod');
144
+ expect(context.scrubSecrets(`token ${secret} end`)).toEqual('token [REDACTED] end');
145
+ });
146
+
147
+ test('the request timeout answers 504 and cancels the work the request left running', async () => {
148
+ const app = new Hono();
149
+ app.use('*', requestTimeout({ timeoutMs: 20 }));
150
+ app.use('*', apiContext());
151
+ let reason;
152
+ app.get('/api/slow', async (c) => {
153
+ const { signal } = c.get('lowdefyContext');
154
+ reason = await new Promise((resolve) => {
155
+ signal.addEventListener('abort', () => resolve(signal.reason), { once: true });
156
+ });
157
+ return c.json({ finished: true });
158
+ });
159
+ const res = await app.request('/api/slow');
160
+ expect(res.status).toEqual(504);
161
+ expect(reason.name).toEqual('TimeoutError');
162
+ expect(reason.message).toEqual('The request timeout of 20ms was exceeded.');
163
+ });
164
+
165
+ test('a request that answers within the timeout leaves its signal unaborted', async () => {
166
+ const app = new Hono();
167
+ app.use('*', requestTimeout({ timeoutMs: 20 }));
168
+ app.use('*', apiContext());
169
+ let signal;
170
+ app.get('/api/fast', (c) => {
171
+ signal = c.get('lowdefyContext').signal;
172
+ return c.json({ finished: true });
173
+ });
174
+ const res = await app.request('/api/fast');
175
+ expect(res.status).toEqual(200);
176
+ await new Promise((resolve) => setTimeout(resolve, 40));
177
+ expect(signal.aborted).toBe(false);
178
+ });
@@ -0,0 +1,36 @@
1
+ /*
2
+ Copyright 2020-2026 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ import { isSameOriginRequest } from '@lowdefy/api';
18
+
19
+ // The cross-site defence for /api/client-error, which only this app's own
20
+ // pages call. The rule itself - Sec-Fetch-Site, then Origin against Host - is
21
+ // isSameOriginRequest in @lowdefy/api, which every server shares. Unlike a
22
+ // websocket handshake, a fetch can carry X-Forwarded-Host where a CORS
23
+ // preflight allows it, so HTTP routes compare the Origin with Host only.
24
+ function createSameOriginGuard({ allowNoOrigin = false } = {}) {
25
+ // Returns the 403 response to answer with, or null when the request may
26
+ // proceed. A guard, not Hono middleware, so a route keeps one entry point
27
+ // and the refusal is visible at the top of the handler that owns it.
28
+ return function guardSameOrigin(c) {
29
+ if (isSameOriginRequest({ getHeader: (name) => c.req.header(name), allowNoOrigin })) {
30
+ return null;
31
+ }
32
+ return c.json({ error: 'Forbidden' }, 403);
33
+ };
34
+ }
35
+
36
+ export default createSameOriginGuard;
@@ -0,0 +1,102 @@
1
+ /*
2
+ Copyright 2020-2026 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ import { Hono } from 'hono';
18
+
19
+ import createSameOriginGuard from './createSameOriginGuard.js';
20
+
21
+ function createApp({ allowNoOrigin } = {}) {
22
+ const guardSameOrigin = createSameOriginGuard({ allowNoOrigin });
23
+ const app = new Hono();
24
+ app.all('/post', (c) => {
25
+ const forbidden = guardSameOrigin(c);
26
+ if (forbidden) {
27
+ return forbidden;
28
+ }
29
+ return c.json({ allowed: true });
30
+ });
31
+ return app;
32
+ }
33
+
34
+ function post({ allowNoOrigin, headers }) {
35
+ return createApp({ allowNoOrigin }).request('/post', { headers, method: 'POST' });
36
+ }
37
+
38
+ test.each([
39
+ ['origin matches the host', { host: 'app.test', origin: 'https://app.test' }],
40
+ [
41
+ 'sec-fetch-site says same-origin',
42
+ { host: 'app.test', origin: 'https://app.test', 'sec-fetch-site': 'same-origin' },
43
+ ],
44
+ [
45
+ 'sec-fetch-site says none',
46
+ { host: 'app.test', origin: 'https://app.test', 'sec-fetch-site': 'none' },
47
+ ],
48
+ [
49
+ 'the origin carries a port matching the host',
50
+ { host: 'app.test:3000', origin: 'http://app.test:3000' },
51
+ ],
52
+ ])('the guard allows a post when %s', async (_, headers) => {
53
+ const res = await post({ headers });
54
+
55
+ expect(res.status).toEqual(200);
56
+ expect(await res.json()).toEqual({ allowed: true });
57
+ });
58
+
59
+ test.each([
60
+ ['the origin is another site', { host: 'app.test', origin: 'https://evil.test' }],
61
+ ['the origin is a sibling subdomain', { host: 'app.test', origin: 'https://other.app.test' }],
62
+ ['the origin is not a url', { host: 'app.test', origin: 'not a url' }],
63
+ [
64
+ 'sec-fetch-site says cross-site',
65
+ { host: 'app.test', origin: 'https://app.test', 'sec-fetch-site': 'cross-site' },
66
+ ],
67
+ [
68
+ 'sec-fetch-site says same-site',
69
+ { host: 'app.test', origin: 'https://app.test', 'sec-fetch-site': 'same-site' },
70
+ ],
71
+ ['there is no origin', { host: 'app.test' }],
72
+ ])('the guard answers 403 when %s', async (_, headers) => {
73
+ const res = await post({ headers });
74
+
75
+ expect(res.status).toEqual(403);
76
+ expect(await res.json()).toEqual({ error: 'Forbidden' });
77
+ });
78
+
79
+ test('the guard lets a caller that sends no origin through when the route allows it', async () => {
80
+ const res = await post({ allowNoOrigin: true, headers: { host: 'app.test' } });
81
+
82
+ expect(res.status).toEqual(200);
83
+ expect(await res.json()).toEqual({ allowed: true });
84
+ });
85
+
86
+ test('allowNoOrigin does not excuse a browser posting cross-site', async () => {
87
+ const res = await post({
88
+ allowNoOrigin: true,
89
+ headers: { host: 'app.test', origin: 'https://evil.test' },
90
+ });
91
+
92
+ expect(res.status).toEqual(403);
93
+ });
94
+
95
+ test('allowNoOrigin does not excuse a cross-site sec-fetch-site with no origin', async () => {
96
+ const res = await post({
97
+ allowNoOrigin: true,
98
+ headers: { host: 'app.test', 'sec-fetch-site': 'cross-site' },
99
+ });
100
+
101
+ expect(res.status).toEqual(403);
102
+ });