@lowdefy/api 6.1.0 → 7.1.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 (195) hide show
  1. package/dist/context/applySystemTrust.js +45 -0
  2. package/dist/context/createApiContext.js +18 -3
  3. package/dist/context/createAuthorizeOutcome.js +91 -0
  4. package/dist/context/createBoundSystemContext.js +33 -0
  5. package/dist/context/createEvaluateOperators.js +8 -3
  6. package/dist/context/createRequestSignal.js +47 -0
  7. package/dist/context/createSystemContext.js +80 -0
  8. package/dist/context/getCanonicalUrl.js +27 -0
  9. package/dist/context/isSameOriginRequest.js +85 -0
  10. package/dist/context/isWebSocketOriginAllowed.js +63 -0
  11. package/dist/context/normalizeInjectedCaller.js +58 -0
  12. package/dist/context/parseRequestBody.js +32 -0
  13. package/dist/context/readMemberships.js +71 -0
  14. package/dist/context/resolveAuthentication.js +758 -0
  15. package/dist/context/resolveStrategyCaller.js +7 -3
  16. package/dist/csp/addNonceToElements.js +23 -0
  17. package/dist/csp/createContentSecurityPolicy.js +45 -0
  18. package/dist/csp/getWebSocketSources.js +36 -0
  19. package/dist/email/renderAuthEmail.js +99 -0
  20. package/dist/index.js +21 -5
  21. package/dist/response/formatErrorForAgent.js +35 -0
  22. package/dist/response/redactResponse.js +8 -1
  23. package/dist/routes/agent/authorizeAgent.js +17 -5
  24. package/dist/routes/agent/callAgent.js +12 -2
  25. package/dist/routes/agent/collectChatFiles.js +39 -0
  26. package/dist/routes/agent/prepareAgent.js +23 -4
  27. package/dist/routes/auth/buildAdminPlugin.js +33 -0
  28. package/dist/routes/auth/buildCaptchaPlugin.js +61 -0
  29. package/dist/routes/auth/buildMcpTokenPlugin.js +77 -0
  30. package/dist/routes/auth/buildOauthPostLogin.js +73 -0
  31. package/dist/routes/auth/buildPhoneNumberPlugin.js +57 -0
  32. package/dist/routes/auth/buildProviders.js +61 -0
  33. package/dist/routes/auth/createAsMetadataHandler.js +27 -0
  34. package/dist/routes/auth/createAuthLogger.js +31 -0
  35. package/dist/routes/auth/createMcpAccessTokenClaims.js +52 -0
  36. package/dist/routes/auth/createOnAPIError.js +41 -0
  37. package/dist/routes/auth/createSendEmail.js +54 -0
  38. package/dist/routes/auth/ensureMcpTokenIndexes.js +48 -0
  39. package/dist/routes/auth/getAuthEnforcement.js +41 -0
  40. package/dist/routes/auth/getBetterAuth.js +107 -0
  41. package/dist/routes/auth/getBetterAuthConfig.js +767 -0
  42. package/dist/routes/auth/handleAuthRequest.js +43 -0
  43. package/dist/routes/auth/hooks/authHookPoints.js +204 -0
  44. package/dist/routes/auth/hooks/buildEngineHooks.js +68 -0
  45. package/dist/routes/auth/hooks/buildHooks.js +112 -0
  46. package/dist/routes/auth/{callbacks/addUserFieldsToSession.js → hooks/composeAfterSlot.js} +11 -13
  47. package/dist/routes/auth/hooks/composeBeforeSlot.js +41 -0
  48. package/dist/routes/auth/hooks/createHookDispatch.js +59 -0
  49. package/dist/routes/auth/hooks/createUserAfterHook.js +36 -0
  50. package/dist/routes/auth/hooks/createUserBeforeHook.js +50 -0
  51. package/dist/routes/auth/hooks/findHookUser.js +27 -0
  52. package/dist/routes/auth/modelNames.js +30 -0
  53. package/dist/routes/auth/organizations/OrganizationKeyError.js +28 -0
  54. package/dist/routes/auth/organizations/authorizeRole.js +27 -0
  55. package/dist/routes/auth/organizations/buildOrganizationPlugin.js +178 -0
  56. package/dist/routes/auth/organizations/createAcceptActiveOrgGuardHook.js +57 -0
  57. package/dist/routes/auth/organizations/createAcceptExistingMemberHook.js +120 -0
  58. package/dist/routes/auth/organizations/createActiveOrgPolicyHook.js +403 -0
  59. package/dist/routes/auth/organizations/createAdmissionGateHook.js +50 -0
  60. package/dist/routes/auth/organizations/createAfterAcceptInvitationHook.js +90 -0
  61. package/dist/routes/auth/organizations/createAutoJoinHook.js +68 -0
  62. package/dist/routes/auth/organizations/createEmailSendGate.js +59 -0
  63. package/dist/routes/auth/organizations/createExpiredInvitationGate.js +76 -0
  64. package/dist/routes/auth/organizations/ensureAuthIndexes.js +122 -0
  65. package/dist/routes/auth/organizations/ensureOrganization.js +132 -0
  66. package/dist/routes/auth/organizations/findPendingInvitation.js +43 -0
  67. package/dist/routes/auth/organizations/getHookRequestHeaders.js +24 -0
  68. package/dist/routes/auth/organizations/getOrganizationBinding.js +65 -0
  69. package/dist/routes/auth/organizations/isEmailAdmitted.js +89 -0
  70. package/dist/routes/auth/organizations/organizationAccessControl.js +94 -0
  71. package/dist/routes/auth/organizations/resolvePinnedOrganization.js +62 -0
  72. package/dist/routes/auth/requestHooks/beginTwoFactorChallenge.js +145 -0
  73. package/dist/routes/auth/requestHooks/buildRequestHooks.js +288 -0
  74. package/dist/routes/auth/requestHooks/createOauthPostLoginHook.js +43 -0
  75. package/dist/routes/auth/requestHooks/createTwoFactorChallengeHook.js +59 -0
  76. package/dist/routes/auth/requestHooks/dispatchRequestHooks.js +47 -0
  77. package/dist/routes/auth/requestHooks/redirectToChallenge.js +79 -0
  78. package/dist/routes/auth/requestHooks/resolveTwoFactorPageUrl.js +31 -0
  79. package/dist/routes/auth/resolveCookiePrefix.js +30 -0
  80. package/dist/routes/auth/sanitizeSessionResponse.js +27 -0
  81. package/dist/routes/auth/strategies/getAuthStrategies.js +1 -1
  82. package/dist/routes/connections/checkUnwalledReach.js +37 -0
  83. package/dist/routes/connections/getConnectionConfig.js +6 -1
  84. package/dist/routes/connections/resolveTenantPreflight.js +226 -0
  85. package/dist/routes/connections/resolveUnwalledReach.js +144 -0
  86. package/dist/routes/endpoints/acceptDetachedEndpoint.js +50 -0
  87. package/dist/routes/endpoints/authorizeApiEndpoint.js +13 -4
  88. package/dist/routes/endpoints/callEndpoint.js +22 -7
  89. package/dist/routes/endpoints/control/controlParallelFor.js +61 -22
  90. package/dist/routes/endpoints/control/controlReject.js +23 -1
  91. package/dist/routes/endpoints/control/controlReturn.js +29 -1
  92. package/dist/routes/endpoints/control/controlThrow.js +3 -2
  93. package/dist/routes/endpoints/control/controlTry.js +10 -1
  94. package/dist/routes/endpoints/control/controlWhile.js +55 -0
  95. package/dist/routes/endpoints/control/handleControl.js +3 -1
  96. package/dist/routes/endpoints/createWebhookAnswer.js +78 -0
  97. package/dist/routes/endpoints/evaluateRoutineOperators.js +2 -1
  98. package/dist/routes/endpoints/forwardScheduledEndpoint.js +3 -2
  99. package/dist/routes/endpoints/getEndpointConfig.js +18 -8
  100. package/dist/routes/endpoints/getSchemaValidator.js +39 -0
  101. package/dist/routes/endpoints/handleAgentCall.js +21 -2
  102. package/dist/routes/endpoints/handleAuthStep.js +214 -0
  103. package/dist/routes/endpoints/handleDescribeDynamicPolicy.js +64 -0
  104. package/dist/routes/endpoints/handleEndpointCall.js +43 -6
  105. package/dist/routes/endpoints/handleRenderNotification.js +14 -4
  106. package/dist/routes/endpoints/handleRequest.js +12 -1
  107. package/dist/routes/endpoints/handleValidateDynamic.js +106 -0
  108. package/dist/routes/endpoints/handleValidateSchema.js +13 -15
  109. package/dist/routes/endpoints/invokeEndpoint.js +26 -3
  110. package/dist/routes/endpoints/operatorScope.js +2 -1
  111. package/dist/routes/{auth/callbacks/createCallbackPlugins.js → endpoints/parseWebhookBody.js} +10 -6
  112. package/dist/routes/endpoints/resolveCallBinding.js +91 -0
  113. package/dist/routes/endpoints/resolveStepOrganizationId.js +64 -0
  114. package/dist/routes/endpoints/runDetachedEndpoint.js +63 -18
  115. package/dist/routes/endpoints/runRoutine.js +64 -5
  116. package/dist/routes/endpoints/runScheduledEndpoint.js +18 -11
  117. package/dist/routes/endpoints/runWebhookEndpoint.js +69 -23
  118. package/dist/routes/endpoints/runWebhookVerify.js +129 -0
  119. package/dist/routes/endpoints/scheduleBackground.js +10 -0
  120. package/dist/routes/endpoints/test/runTest.js +31 -9
  121. package/dist/routes/endpoints/validatePayload.js +144 -0
  122. package/dist/routes/log/logClientError.js +7 -4
  123. package/dist/routes/mcp/createMcpServer.js +143 -82
  124. package/dist/routes/mcp/getMcpJwks.js +93 -0
  125. package/dist/routes/mcp/getMcpResourceBinding.js +41 -0
  126. package/dist/routes/mcp/getMcpUri.js +69 -0
  127. package/dist/routes/mcp/oauthResourceLifecycle.js +122 -0
  128. package/dist/routes/mcp/runAfterToolCall.js +50 -0
  129. package/dist/routes/notifications/resolveNotificationLinks.js +39 -11
  130. package/dist/routes/page/dynamic/checkDynamicContent.js +81 -0
  131. package/dist/routes/page/dynamic/checkLiteralContent.js +242 -0
  132. package/dist/routes/page/dynamic/createContentError.js +34 -0
  133. package/dist/routes/page/dynamic/flagIconsOutsidePage.js +39 -0
  134. package/dist/routes/page/dynamic/flagTypesOutsidePage.js +44 -0
  135. package/dist/routes/page/dynamic/getPropertiesSchemaErrors.js +35 -0
  136. package/dist/routes/page/dynamic/loadDynamicArtifacts.js +52 -0
  137. package/dist/routes/page/dynamic/policy/checkAction.js +204 -0
  138. package/dist/routes/page/dynamic/policy/checkEvents.js +163 -0
  139. package/dist/routes/page/dynamic/policy/checkPolicy.js +313 -0
  140. package/dist/routes/page/dynamic/policy/checkValue.js +214 -0
  141. package/dist/routes/page/dynamic/policy/collectUrlKinds.js +117 -0
  142. package/dist/routes/page/dynamic/policy/findEmbeddedUrls.js +54 -0
  143. package/dist/routes/page/dynamic/policy/findSerializerKeys.js +50 -0
  144. package/dist/routes/page/dynamic/policy/isAllowedUrl.js +53 -0
  145. package/dist/routes/{auth/createAdapter.js → page/dynamic/policy/isUnderState.js} +5 -9
  146. package/dist/routes/page/dynamic/policy/normalizeUrlText.js +25 -0
  147. package/dist/routes/page/dynamic/resolveDynamicContent.js +89 -37
  148. package/dist/routes/page/dynamic/unescapeOperators.js +21 -4
  149. package/dist/routes/page/dynamic/validateFragment.js +17 -58
  150. package/dist/routes/page/getPageConfig.js +76 -6
  151. package/dist/routes/request/authorizeRequest.js +21 -6
  152. package/dist/routes/request/callRequest.js +22 -10
  153. package/dist/routes/request/callRequestResolver.js +49 -6
  154. package/dist/routes/request/checkConnectionRead.js +1 -1
  155. package/dist/routes/request/checkConnectionWrite.js +1 -1
  156. package/dist/routes/request/createWalledResolver.js +75 -0
  157. package/dist/routes/request/getRequestConfig.js +19 -3
  158. package/dist/routes/request/maskCredentialProperties.js +34 -0
  159. package/dist/routes/request/resolveTenancy.js +223 -0
  160. package/dist/routes/request/validateSchemas.js +7 -6
  161. package/dist/routes/{auth/events/createEventPlugins.js → rootConfig/getAppEvents.js} +7 -6
  162. package/dist/routes/rootConfig/getHomeAndMenus.js +23 -16
  163. package/dist/routes/{auth/events/createSessionEvent.js → rootConfig/getPagePaths.js} +16 -18
  164. package/dist/routes/rootConfig/getRootConfig.js +9 -3
  165. package/dist/routes/rootConfig/menus/filterMenuList.js +5 -2
  166. package/dist/routes/websocket/authorizeWebsocket.js +19 -4
  167. package/dist/routes/websocket/createChannelRegistry.js +20 -9
  168. package/dist/routes/websocket/createWebSocketConnection.js +150 -28
  169. package/dist/routes/websocket/getCallerFingerprint.js +31 -0
  170. package/dist/routes/websocket/prepareChannel.js +29 -1
  171. package/dist/routes/websocket/resolveWebsocketCaller.js +40 -0
  172. package/dist/test/testContext.js +13 -10
  173. package/package.json +28 -12
  174. package/dist/context/createAuthorize.js +0 -45
  175. package/dist/routes/auth/callbacks/addUserFieldsToToken.js +0 -30
  176. package/dist/routes/auth/callbacks/createCallbacks.js +0 -46
  177. package/dist/routes/auth/callbacks/createJWTCallback.js +0 -77
  178. package/dist/routes/auth/callbacks/createRedirectCallback.js +0 -41
  179. package/dist/routes/auth/callbacks/createSessionCallback.js +0 -81
  180. package/dist/routes/auth/callbacks/createSignInCallback.js +0 -40
  181. package/dist/routes/auth/callbacks/validateSessionRoles.js +0 -33
  182. package/dist/routes/auth/createLogger.js +0 -35
  183. package/dist/routes/auth/createPrefixedCookies.js +0 -52
  184. package/dist/routes/auth/createProviders.js +0 -25
  185. package/dist/routes/auth/defaultAuthUrl.js +0 -31
  186. package/dist/routes/auth/events/createCreateUserEvent.js +0 -41
  187. package/dist/routes/auth/events/createEvents.js +0 -56
  188. package/dist/routes/auth/events/createLinkAccountEvent.js +0 -43
  189. package/dist/routes/auth/events/createSignInEvent.js +0 -46
  190. package/dist/routes/auth/events/createSignOutEvent.js +0 -43
  191. package/dist/routes/auth/events/createUpdateUserEvent.js +0 -41
  192. package/dist/routes/auth/getAuthConfig.js +0 -90
  193. package/dist/routes/auth/resolveCookies.js +0 -37
  194. package/dist/routes/log/formatValidationError.js +0 -31
  195. /package/dist/{routes/notifications → email}/resolveThemeLogo.js +0 -0
@@ -0,0 +1,214 @@
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
+ */ import { type } from '@lowdefy/helpers';
16
+ import { getPossibleOperators } from '@lowdefy/operators';
17
+ import findEmbeddedUrls from './findEmbeddedUrls.js';
18
+ import isAllowedUrl from './isAllowedUrl.js';
19
+ import isUnderState from './isUnderState.js';
20
+ // How the client uses a URL value, for values no schema marks. A block schema
21
+ // marks its URL-valued properties with urlKind (collectUrlKinds), whatever they
22
+ // are called, and opts a URL-named property out with urlKind: false; every
23
+ // other value (action params, style, unmarked properties, including every
24
+ // property of a block that marks none) is judged by these key names. url and
25
+ // href navigate, and the engine's link resolver reads a colon-less url as https.
26
+ const URL_KEY_KINDS = {
27
+ action: 'src',
28
+ cover: 'src',
29
+ href: 'href',
30
+ image: 'src',
31
+ poster: 'src',
32
+ src: 'src',
33
+ srcMobile: 'src',
34
+ srcSet: 'srcSet',
35
+ url: 'url'
36
+ };
37
+ const HTML_TAG = /<[a-zA-Z/!]/;
38
+ function joinPath(path, key) {
39
+ return path ? `${path}.${key}` : `${key}`;
40
+ }
41
+ function getUrlKind({ key, path, walk }) {
42
+ if (walk.urlKinds?.has(path)) {
43
+ return walk.urlKinds.get(path);
44
+ }
45
+ return URL_KEY_KINDS[key] ?? null;
46
+ }
47
+ function listUrls({ value, urlKind }) {
48
+ // srcSet lists "url descriptor" pairs.
49
+ if (urlKind === 'srcSet') return value.split(',').map((entry)=>entry.trim().split(/\s+/)[0]);
50
+ return [
51
+ value
52
+ ];
53
+ }
54
+ function checkString({ value, key, path, walk }) {
55
+ const { errors, policy, routes } = walk;
56
+ if (!policy.html && HTML_TAG.test(value)) {
57
+ errors.push({
58
+ path,
59
+ rule: 'policy.html',
60
+ message: `String contains HTML tag syntax. Dynamic blocks policy "${policy.id}" does not allow HTML.`
61
+ });
62
+ }
63
+ if (key === 'pageId' && !policy.links.pages.includes(value)) {
64
+ errors.push({
65
+ path,
66
+ rule: 'policy.links',
67
+ message: `Page "${value}" is not in dynamic blocks policy "${policy.id}" links.pages.`
68
+ });
69
+ }
70
+ const urlKind = getUrlKind({
71
+ key,
72
+ path,
73
+ walk
74
+ });
75
+ if (urlKind !== null) {
76
+ listUrls({
77
+ value,
78
+ urlKind
79
+ }).forEach((url)=>{
80
+ const allowed = isAllowedUrl({
81
+ value: url,
82
+ policy,
83
+ navigation: urlKind === 'url' || urlKind === 'href',
84
+ schemeless: urlKind === 'url',
85
+ routes
86
+ });
87
+ if (!allowed) {
88
+ // A URL in a list (an array item) has no key of its own.
89
+ const label = key === null ? 'URL' : `"${key}" value`;
90
+ errors.push({
91
+ path,
92
+ rule: 'policy.urls',
93
+ message: `${label} "${url}" is not a page or origin dynamic blocks policy "${policy.id}" allows.`
94
+ });
95
+ }
96
+ });
97
+ return;
98
+ }
99
+ // A URL value was judged whole above; other strings are searched.
100
+ findEmbeddedUrls(value).forEach((url)=>{
101
+ if (!isAllowedUrl({
102
+ value: url,
103
+ policy,
104
+ navigation: false,
105
+ schemeless: false,
106
+ routes
107
+ })) {
108
+ errors.push({
109
+ path,
110
+ rule: 'policy.urls',
111
+ message: `URL "${url}" is not an origin dynamic blocks policy "${policy.id}" allows.`
112
+ });
113
+ }
114
+ });
115
+ }
116
+ // A _state read may only name a literal key under the policy's state, so
117
+ // content cannot read other page state and send it on (a CallAPI payload).
118
+ function isAllowedStateRead({ params, state }) {
119
+ if (type.isString(params)) {
120
+ return isUnderState({
121
+ key: params,
122
+ state
123
+ });
124
+ }
125
+ return type.isObject(params) && params.all !== true && type.isString(params.key) && isUnderState({
126
+ key: params.key,
127
+ state
128
+ });
129
+ }
130
+ function checkOperators({ value, key, path, walk }) {
131
+ const { clientOperators, errors, policy } = walk;
132
+ const possible = getPossibleOperators({
133
+ value,
134
+ operators: clientOperators
135
+ });
136
+ possible.forEach(({ key: operatorKey, operator })=>{
137
+ if (!policy.operators.includes(operator)) {
138
+ errors.push({
139
+ path,
140
+ rule: 'policy.operators',
141
+ message: `Operator "${operator}" is not in dynamic blocks policy "${policy.id}" operators.`
142
+ });
143
+ }
144
+ if (operator === '_state' && !type.isNone(policy.state) && !isAllowedStateRead({
145
+ params: value[operatorKey],
146
+ state: policy.state
147
+ })) {
148
+ errors.push({
149
+ path,
150
+ rule: 'policy.state',
151
+ message: `_state must read a literal key under "${policy.state}", the state dynamic blocks policy "${policy.id}" allows.`
152
+ });
153
+ }
154
+ });
155
+ // The client would compute this value at render time, where the policy
156
+ // cannot see it.
157
+ const urlKind = getUrlKind({
158
+ key,
159
+ path,
160
+ walk
161
+ });
162
+ if (possible.length > 0 && (urlKind !== null || key === 'pageId')) {
163
+ // A URL in a list (an array item) has no key of its own.
164
+ const label = key === null ? 'A URL' : `"${key}"`;
165
+ errors.push({
166
+ path,
167
+ rule: key === 'pageId' ? 'policy.links' : 'policy.urls',
168
+ message: `${label} must be a literal string under dynamic blocks policy "${policy.id}", not an operator.`
169
+ });
170
+ }
171
+ }
172
+ // Walks any config value: every operator must be allowed, _state reads must stay
173
+ // under the policy's state, URL-valued and pageId values must be literal and
174
+ // allowed, and strings must obey the html rule. walk carries the policy, the
175
+ // app's client operators, the error list and, inside block properties, the URL
176
+ // kinds the block's schema marks (by path).
177
+ function checkValue({ value, key = null, path, walk }) {
178
+ if (type.isString(value)) {
179
+ checkString({
180
+ value,
181
+ key,
182
+ path,
183
+ walk
184
+ });
185
+ return;
186
+ }
187
+ if (type.isArray(value)) {
188
+ value.forEach((item, index)=>checkValue({
189
+ value: item,
190
+ path: joinPath(path, index),
191
+ walk
192
+ }));
193
+ return;
194
+ }
195
+ if (!type.isObject(value)) {
196
+ return;
197
+ }
198
+ checkOperators({
199
+ value,
200
+ key,
201
+ path,
202
+ walk
203
+ });
204
+ Object.keys(value).forEach((childKey)=>{
205
+ if (childKey.startsWith('~')) return;
206
+ checkValue({
207
+ value: value[childKey],
208
+ key: childKey,
209
+ path: joinPath(path, childKey),
210
+ walk
211
+ });
212
+ });
213
+ }
214
+ export default checkValue;
@@ -0,0 +1,117 @@
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
+ */ import { type } from '@lowdefy/helpers';
16
+ // The schemas that can describe a value: the schema itself and its oneOf,
17
+ // anyOf and allOf branches, however nested.
18
+ function expandSchema(schema) {
19
+ const schemas = [];
20
+ const pending = [
21
+ schema
22
+ ];
23
+ while(pending.length > 0){
24
+ const candidate = pending.pop();
25
+ if (type.isObject(candidate)) {
26
+ schemas.push(candidate);
27
+ [
28
+ ...candidate.oneOf ?? [],
29
+ ...candidate.anyOf ?? [],
30
+ ...candidate.allOf ?? []
31
+ ].forEach((branch)=>pending.push(branch));
32
+ }
33
+ }
34
+ return schemas;
35
+ }
36
+ function childSchemas({ schemas, key }) {
37
+ return schemas.flatMap((schema)=>{
38
+ if (type.isObject(schema.properties?.[key])) {
39
+ return [
40
+ schema.properties[key]
41
+ ];
42
+ }
43
+ if (type.isObject(schema.additionalProperties)) {
44
+ return [
45
+ schema.additionalProperties
46
+ ];
47
+ }
48
+ return [];
49
+ });
50
+ }
51
+ // The urlKind the schemas give a value: a string when one marks it as a URL,
52
+ // null when one opts it out with urlKind: false, undefined when none says.
53
+ function findUrlKind(schemas) {
54
+ const marked = schemas.find((candidate)=>type.isString(candidate.urlKind));
55
+ if (marked) {
56
+ return marked.urlKind;
57
+ }
58
+ if (schemas.some((candidate)=>candidate.urlKind === false)) {
59
+ return null;
60
+ }
61
+ return undefined;
62
+ }
63
+ // Records, by path, the urlKind a block schema gives the value there. A block
64
+ // marks each URL-valued property with urlKind ('url' and 'href' navigate, 'url'
65
+ // the way the engine's link resolver does; 'src' loads; 'srcSet' lists
66
+ // sources), so the policy judges URLs by what a property is, not by what it is
67
+ // called; a marked array holds one URL per item. urlKind: false records null,
68
+ // for a property whose name looks like a URL key but whose value is not a URL.
69
+ // A value no schema marks either way is not recorded, so the policy falls back
70
+ // to judging it by its key name.
71
+ function collectUrlKinds({ value, schema, path }) {
72
+ const kinds = new Map();
73
+ const pending = [
74
+ {
75
+ value,
76
+ schema,
77
+ path
78
+ }
79
+ ];
80
+ while(pending.length > 0){
81
+ const item = pending.pop();
82
+ const schemas = expandSchema(item.schema);
83
+ const urlKind = findUrlKind(schemas);
84
+ if (urlKind === null) {
85
+ // Another schema branch may already have marked it as a URL.
86
+ if (!kinds.has(item.path)) {
87
+ kinds.set(item.path, null);
88
+ }
89
+ } else if (type.isString(urlKind)) {
90
+ if (type.isArray(item.value)) {
91
+ item.value.forEach((_, index)=>kinds.set(`${item.path}.${index}`, urlKind));
92
+ } else {
93
+ kinds.set(item.path, urlKind);
94
+ }
95
+ } else if (type.isArray(item.value)) {
96
+ const itemSchemas = schemas.map((candidate)=>candidate.items).filter(type.isObject);
97
+ item.value.forEach((child, index)=>itemSchemas.forEach((itemSchema)=>pending.push({
98
+ value: child,
99
+ schema: itemSchema,
100
+ path: `${item.path}.${index}`
101
+ })));
102
+ } else if (type.isObject(item.value)) {
103
+ Object.keys(item.value).forEach((key)=>{
104
+ childSchemas({
105
+ schemas,
106
+ key
107
+ }).forEach((childSchema)=>pending.push({
108
+ value: item.value[key],
109
+ schema: childSchema,
110
+ path: `${item.path}.${key}`
111
+ }));
112
+ });
113
+ }
114
+ }
115
+ return kinds;
116
+ }
117
+ export default collectUrlKinds;
@@ -0,0 +1,54 @@
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
+ */ import normalizeUrlText from './normalizeUrlText.js';
16
+ // Returns every URL a string can make the client load or navigate to that is
17
+ // not given by its key: a scheme or protocol-relative value (read as the URL
18
+ // parser reads it), markdown link and image targets, and CSS
19
+ // url()/image-set()/src() arguments after unescaping.
20
+ const SCHEME = /^[a-zA-Z][a-zA-Z0-9+.-]*:\S/;
21
+ // Script schemes run whatever follows them, spaces included.
22
+ const SCRIPT_SCHEME = /^(javascript|vbscript):/i;
23
+ const MARKDOWN_INLINE = /\]\(\s*<?([^)\s>]+)/g;
24
+ const MARKDOWN_REFERENCE = /^\s*\[[^\]]+\]:\s*<?([^\s>]+)/gm;
25
+ const CSS_FUNCTION = /(url|src|image-set)\(([^)]*)\)/gi;
26
+ const QUOTED = /(['"])(.*?)\1/g;
27
+ function unescapeCss(value) {
28
+ return value.replace(/\\([0-9a-fA-F]{1,6})\s?/g, (_, hex)=>String.fromCodePoint(parseInt(hex, 16))).replace(/\\(.)/g, '$1');
29
+ }
30
+ function findEmbeddedUrls(value) {
31
+ const urls = [];
32
+ const text = normalizeUrlText(value);
33
+ const isUrlText = !/\s/.test(text) && (SCHEME.test(text) || text.startsWith('//') || text.startsWith('/\\'));
34
+ if (isUrlText || SCRIPT_SCHEME.test(text)) {
35
+ urls.push(text);
36
+ }
37
+ for (const match of value.matchAll(MARKDOWN_INLINE))urls.push(match[1]);
38
+ for (const match of value.matchAll(MARKDOWN_REFERENCE))urls.push(match[1]);
39
+ if (value.includes('(')) {
40
+ for (const match of unescapeCss(value).matchAll(CSS_FUNCTION)){
41
+ const argument = match[2].trim();
42
+ const quoted = [
43
+ ...argument.matchAll(QUOTED)
44
+ ].map((quote)=>quote[2]);
45
+ if (quoted.length > 0) {
46
+ urls.push(...quoted);
47
+ } else if (match[1].toLowerCase() !== 'image-set') {
48
+ urls.push(argument);
49
+ }
50
+ }
51
+ }
52
+ return urls;
53
+ }
54
+ export default findEmbeddedUrls;
@@ -0,0 +1,50 @@
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
+ */ import { type } from '@lowdefy/helpers';
16
+ // Paths of the "~" keys in serialized content. The client revives each such
17
+ // wrapper ("~e" to an Error, "~d" to a Date, "~arr" to an array) only after it
18
+ // has evaluated the operators inside it, where the policy walk does not look.
19
+ // Block config never needs one, so every "~" key is reported, except the
20
+ // { "~d": <number> } of a date, which holds no config.
21
+ function findSerializerKeys({ value, path, paths = [] }) {
22
+ if (type.isArray(value)) {
23
+ value.forEach((item, index)=>findSerializerKeys({
24
+ value: item,
25
+ path: `${path}.${index}`,
26
+ paths
27
+ }));
28
+ return paths;
29
+ }
30
+ if (!type.isObject(value)) {
31
+ return paths;
32
+ }
33
+ const keys = Object.keys(value);
34
+ if (keys.length === 1 && keys[0] === '~d' && type.isNumber(value['~d'])) {
35
+ return paths;
36
+ }
37
+ keys.forEach((key)=>{
38
+ if (key.startsWith('~')) {
39
+ paths.push(`${path}.${key}`);
40
+ return;
41
+ }
42
+ findSerializerKeys({
43
+ value: value[key],
44
+ path: `${path}.${key}`,
45
+ paths
46
+ });
47
+ });
48
+ return paths;
49
+ }
50
+ export default findSerializerKeys;
@@ -0,0 +1,53 @@
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
+ */ import { matchPagePath } from '@lowdefy/node-utils';
16
+ import normalizeUrlText from './normalizeUrlText.js';
17
+ // A reserved origin: a URL that resolves to it stays on the app.
18
+ const SENTINEL = 'https://app.invalid';
19
+ // Decides one URL. Same-origin values are allowed, except that navigation
20
+ // (href, url, Link) may only name an app path that matches a page the policy
21
+ // lists.
22
+ // Anything else must be http(s) on an origin the policy lists. `schemeless`
23
+ // mirrors the engine's link resolver (engine/src/resolveTarget.js,
24
+ // classifyUrl), which reads a colon-less `url` such as `example.com` as
25
+ // `https://example.com`.
26
+ function isAllowedUrl({ value, policy, navigation, schemeless, routes }) {
27
+ const text = normalizeUrlText(value);
28
+ if (text === '' || text.startsWith('#')) {
29
+ return true;
30
+ }
31
+ const candidate = schemeless && !text.includes(':') && !text.startsWith('/') ? `https://${text}` : text;
32
+ let url;
33
+ try {
34
+ url = new URL(candidate, SENTINEL);
35
+ } catch {
36
+ return false;
37
+ }
38
+ if (url.origin === SENTINEL) {
39
+ if (!navigation) {
40
+ return true;
41
+ }
42
+ const match = matchPagePath({
43
+ routes,
44
+ path: url.pathname.slice(1)
45
+ });
46
+ return match !== null && policy.links.pages.includes(match.pageId);
47
+ }
48
+ if (url.protocol !== 'http:' && url.protocol !== 'https:') {
49
+ return false;
50
+ }
51
+ return policy.links.origins.includes(url.origin);
52
+ }
53
+ export default isAllowedUrl;
@@ -12,13 +12,9 @@
12
12
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
- */ function createAdapter({ authConfig, plugins }) {
16
- const adapterConfig = authConfig.adapter;
17
- if (!adapterConfig) {
18
- return undefined;
19
- }
20
- return plugins.adapters[adapterConfig.type]({
21
- properties: adapterConfig.properties
22
- });
15
+ */ // A state path sits under the policy's state root when it is the root itself
16
+ // or a dot path below it.
17
+ function isUnderState({ key, state }) {
18
+ return key === state || key.startsWith(`${state}.`);
23
19
  }
24
- export default createAdapter;
20
+ export default isUnderState;
@@ -0,0 +1,25 @@
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
+ */ // Leading and trailing C0 control characters and spaces, and tabs and newlines
16
+ // anywhere: what the browser's URL parser drops before it reads a URL.
17
+ // eslint-disable-next-line no-control-regex
18
+ const EDGE_CONTROLS = /^[\x00-\x20]+|[\x00-\x20]+$/g;
19
+ const TABS_AND_NEWLINES = /[\t\n\r]/g;
20
+ // A URL string as the browser's URL parser reads it, so " https://x" and
21
+ // "java\tscript:" are judged as what they are to it.
22
+ function normalizeUrlText(value) {
23
+ return value.replace(EDGE_CONTROLS, '').replace(TABS_AND_NEWLINES, '');
24
+ }
25
+ export default normalizeUrlText;