@lowdefy/api 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.
- package/dist/context/applySystemTrust.js +45 -0
- package/dist/context/createApiContext.js +18 -3
- package/dist/context/createAuthorizeOutcome.js +91 -0
- package/dist/context/createBoundSystemContext.js +33 -0
- package/dist/context/createEvaluateOperators.js +10 -3
- package/dist/context/createRequestSignal.js +47 -0
- package/dist/context/createSystemContext.js +80 -0
- package/dist/context/getCanonicalUrl.js +27 -0
- package/dist/context/getCurrentEnvironment.js +27 -0
- package/dist/context/isSameOriginRequest.js +85 -0
- package/dist/context/isWebSocketOriginAllowed.js +63 -0
- package/dist/context/normalizeInjectedCaller.js +58 -0
- package/dist/context/parseRequestBody.js +32 -0
- package/dist/context/readMemberships.js +71 -0
- package/dist/context/resolveAuthentication.js +758 -0
- package/dist/context/resolveStrategyCaller.js +7 -3
- package/dist/csp/addNonceToElements.js +23 -0
- package/dist/csp/createContentSecurityPolicy.js +45 -0
- package/dist/csp/getWebSocketSources.js +36 -0
- package/dist/email/renderAuthEmail.js +99 -0
- package/dist/index.js +22 -5
- package/dist/response/buildEndpointResult.js +3 -1
- package/dist/response/createWireProjection.js +88 -0
- package/dist/response/formatErrorForAgent.js +35 -0
- package/dist/response/normalizeErrorSources.js +10 -13
- package/dist/response/redactErrorResponse.js +42 -8
- package/dist/response/redactResponse.js +17 -10
- package/dist/routes/agent/authorizeAgent.js +17 -5
- package/dist/routes/agent/callAgent.js +12 -2
- package/dist/routes/agent/collectChatFiles.js +39 -0
- package/dist/routes/agent/prepareAgent.js +29 -4
- package/dist/routes/auth/buildAdminPlugin.js +33 -0
- package/dist/routes/auth/buildCaptchaPlugin.js +61 -0
- package/dist/routes/auth/buildMcpTokenPlugin.js +77 -0
- package/dist/routes/auth/buildOauthPostLogin.js +73 -0
- package/dist/routes/auth/buildPhoneNumberPlugin.js +57 -0
- package/dist/routes/auth/buildProviders.js +61 -0
- package/dist/routes/auth/createAsMetadataHandler.js +27 -0
- package/dist/routes/auth/createAuthLogger.js +31 -0
- package/dist/routes/auth/createMcpAccessTokenClaims.js +52 -0
- package/dist/routes/auth/createOnAPIError.js +41 -0
- package/dist/routes/auth/createSendEmail.js +54 -0
- package/dist/routes/auth/ensureMcpTokenIndexes.js +48 -0
- package/dist/routes/auth/getAuthEnforcement.js +41 -0
- package/dist/routes/auth/getBetterAuth.js +107 -0
- package/dist/routes/auth/getBetterAuthConfig.js +767 -0
- package/dist/routes/auth/handleAuthRequest.js +43 -0
- package/dist/routes/auth/hooks/authHookPoints.js +204 -0
- package/dist/routes/auth/hooks/buildEngineHooks.js +68 -0
- package/dist/routes/auth/hooks/buildHooks.js +112 -0
- package/dist/routes/auth/{callbacks/addUserFieldsToSession.js → hooks/composeAfterSlot.js} +11 -13
- package/dist/routes/auth/hooks/composeBeforeSlot.js +41 -0
- package/dist/routes/auth/hooks/createHookDispatch.js +59 -0
- package/dist/routes/auth/hooks/createUserAfterHook.js +36 -0
- package/dist/routes/auth/hooks/createUserBeforeHook.js +50 -0
- package/dist/routes/auth/hooks/findHookUser.js +27 -0
- package/dist/routes/auth/modelNames.js +30 -0
- package/dist/routes/auth/organizations/OrganizationKeyError.js +28 -0
- package/dist/routes/auth/organizations/authorizeRole.js +27 -0
- package/dist/routes/auth/organizations/buildOrganizationPlugin.js +178 -0
- package/dist/routes/auth/organizations/createAcceptActiveOrgGuardHook.js +57 -0
- package/dist/routes/auth/organizations/createAcceptExistingMemberHook.js +120 -0
- package/dist/routes/auth/organizations/createActiveOrgPolicyHook.js +403 -0
- package/dist/routes/auth/organizations/createAdmissionGateHook.js +50 -0
- package/dist/routes/auth/organizations/createAfterAcceptInvitationHook.js +90 -0
- package/dist/routes/auth/organizations/createAutoJoinHook.js +68 -0
- package/dist/routes/auth/organizations/createEmailSendGate.js +59 -0
- package/dist/routes/auth/organizations/createExpiredInvitationGate.js +76 -0
- package/dist/routes/auth/organizations/ensureAuthIndexes.js +122 -0
- package/dist/routes/auth/organizations/ensureOrganization.js +132 -0
- package/dist/routes/auth/organizations/findPendingInvitation.js +43 -0
- package/dist/routes/auth/organizations/getHookRequestHeaders.js +24 -0
- package/dist/routes/auth/organizations/getOrganizationBinding.js +65 -0
- package/dist/routes/auth/organizations/isEmailAdmitted.js +89 -0
- package/dist/routes/auth/organizations/organizationAccessControl.js +94 -0
- package/dist/routes/auth/organizations/resolvePinnedOrganization.js +62 -0
- package/dist/routes/auth/requestHooks/beginTwoFactorChallenge.js +145 -0
- package/dist/routes/auth/requestHooks/buildRequestHooks.js +288 -0
- package/dist/routes/auth/requestHooks/createOauthPostLoginHook.js +43 -0
- package/dist/routes/auth/requestHooks/createTwoFactorChallengeHook.js +59 -0
- package/dist/routes/auth/requestHooks/dispatchRequestHooks.js +47 -0
- package/dist/routes/auth/requestHooks/redirectToChallenge.js +79 -0
- package/dist/routes/auth/requestHooks/resolveTwoFactorPageUrl.js +31 -0
- package/dist/routes/auth/resolveCookiePrefix.js +30 -0
- package/dist/routes/auth/sanitizeSessionResponse.js +27 -0
- package/dist/routes/auth/strategies/getAuthStrategies.js +1 -1
- package/dist/routes/connections/checkUnwalledReach.js +37 -0
- package/dist/routes/connections/getConnectionConfig.js +6 -1
- package/dist/routes/connections/resolveTenantPreflight.js +226 -0
- package/dist/routes/connections/resolveUnwalledReach.js +144 -0
- package/dist/routes/endpoints/acceptDetachedEndpoint.js +50 -0
- package/dist/routes/endpoints/authorizeApiEndpoint.js +13 -4
- package/dist/routes/endpoints/callEndpoint.js +23 -7
- package/dist/routes/endpoints/control/controlFor.js +4 -7
- package/dist/routes/endpoints/control/controlIf.js +4 -8
- package/dist/routes/endpoints/control/controlLog.js +6 -14
- package/dist/routes/endpoints/control/controlParallelFor.js +65 -29
- package/dist/routes/endpoints/control/controlReject.js +36 -16
- package/dist/routes/endpoints/control/controlReturn.js +32 -9
- package/dist/routes/endpoints/control/controlSetState.js +4 -8
- package/dist/routes/endpoints/control/controlSwitch.js +4 -8
- package/dist/routes/endpoints/control/controlThrow.js +20 -21
- package/dist/routes/endpoints/control/controlTry.js +30 -11
- package/dist/routes/endpoints/control/controlWhile.js +55 -0
- package/dist/routes/endpoints/control/handleControl.js +3 -1
- package/dist/routes/endpoints/createWebhookAnswer.js +78 -0
- package/dist/routes/endpoints/evaluateRoutineOperators.js +27 -0
- package/dist/routes/endpoints/forwardScheduledEndpoint.js +21 -15
- package/dist/routes/endpoints/getEndpointConfig.js +18 -8
- package/dist/routes/endpoints/getEnvironmentSchedules.js +5 -7
- package/dist/routes/endpoints/getSchemaValidator.js +39 -0
- package/dist/routes/endpoints/handleAgentCall.js +25 -9
- package/dist/routes/endpoints/handleAuthStep.js +214 -0
- package/dist/routes/endpoints/handleDescribeDynamicPolicy.js +64 -0
- package/dist/routes/endpoints/handleEndpointCall.js +46 -12
- package/dist/routes/endpoints/handleRenderNotification.js +29 -12
- package/dist/routes/endpoints/handleRequest.js +13 -6
- package/dist/routes/endpoints/handleValidateDynamic.js +106 -0
- package/dist/routes/endpoints/handleValidateSchema.js +22 -22
- package/dist/routes/endpoints/invokeEndpoint.js +27 -3
- package/dist/routes/endpoints/operatorScope.js +28 -0
- package/dist/routes/{auth/events/createEventPlugins.js → endpoints/parseWebhookBody.js} +10 -6
- package/dist/routes/endpoints/resolveCallBinding.js +91 -0
- package/dist/routes/endpoints/resolveCronEnvironment.js +7 -6
- package/dist/routes/endpoints/resolveStepOrganizationId.js +64 -0
- package/dist/routes/endpoints/runDetachedEndpoint.js +64 -18
- package/dist/routes/endpoints/runRoutine.js +64 -5
- package/dist/routes/endpoints/runScheduledEndpoint.js +19 -11
- package/dist/routes/endpoints/runWebhookEndpoint.js +70 -23
- package/dist/routes/endpoints/runWebhookVerify.js +129 -0
- package/dist/routes/endpoints/scheduleBackground.js +10 -0
- package/dist/routes/endpoints/test/runTest.js +48 -11
- package/dist/routes/endpoints/validatePayload.js +144 -0
- package/dist/routes/log/logClientError.js +7 -4
- package/dist/routes/mcp/createMcpServer.js +143 -64
- package/dist/routes/mcp/getMcpJwks.js +93 -0
- package/dist/routes/mcp/getMcpResourceBinding.js +41 -0
- package/dist/routes/mcp/getMcpUri.js +69 -0
- package/dist/routes/mcp/oauthResourceLifecycle.js +122 -0
- package/dist/routes/mcp/runAfterToolCall.js +50 -0
- package/dist/routes/notifications/resolveNotificationLinks.js +39 -11
- package/dist/routes/page/dynamic/checkDynamicContent.js +81 -0
- package/dist/routes/page/dynamic/checkLiteralContent.js +242 -0
- package/dist/routes/page/dynamic/createContentError.js +34 -0
- package/dist/routes/page/dynamic/flagIconsOutsidePage.js +39 -0
- package/dist/routes/page/dynamic/flagTypesOutsidePage.js +44 -0
- package/dist/routes/page/dynamic/getPropertiesSchemaErrors.js +35 -0
- package/dist/routes/page/dynamic/loadDynamicArtifacts.js +52 -0
- package/dist/routes/page/dynamic/policy/checkAction.js +204 -0
- package/dist/routes/page/dynamic/policy/checkEvents.js +163 -0
- package/dist/routes/page/dynamic/policy/checkPolicy.js +313 -0
- package/dist/routes/page/dynamic/policy/checkValue.js +214 -0
- package/dist/routes/page/dynamic/policy/collectUrlKinds.js +117 -0
- package/dist/routes/page/dynamic/policy/findEmbeddedUrls.js +54 -0
- package/dist/routes/page/dynamic/policy/findSerializerKeys.js +50 -0
- package/dist/routes/page/dynamic/policy/isAllowedUrl.js +53 -0
- package/dist/routes/{auth/createAdapter.js → page/dynamic/policy/isUnderState.js} +5 -9
- package/dist/routes/page/dynamic/policy/normalizeUrlText.js +25 -0
- package/dist/routes/page/dynamic/resolveDynamicContent.js +89 -37
- package/dist/routes/page/dynamic/unescapeOperators.js +21 -4
- package/dist/routes/page/dynamic/validateFragment.js +17 -58
- package/dist/routes/page/getPageConfig.js +76 -6
- package/dist/routes/request/authorizeRequest.js +21 -6
- package/dist/routes/request/callRequest.js +21 -3
- package/dist/routes/request/callRequestResolver.js +55 -6
- package/dist/routes/request/checkConnectionRead.js +1 -1
- package/dist/routes/request/checkConnectionWrite.js +1 -1
- package/dist/routes/request/createWalledResolver.js +75 -0
- package/dist/routes/request/evaluateOperators.js +8 -12
- package/dist/routes/request/getRequestConfig.js +19 -3
- package/dist/routes/request/maskCredentialProperties.js +34 -0
- package/dist/routes/request/resolveTenancy.js +223 -0
- package/dist/routes/request/validateSchemas.js +7 -6
- package/dist/routes/{auth/callbacks/createCallbackPlugins.js → rootConfig/getAppEvents.js} +7 -6
- package/dist/routes/rootConfig/getHomeAndMenus.js +23 -16
- package/dist/routes/{auth/events/createSessionEvent.js → rootConfig/getPagePaths.js} +16 -18
- package/dist/routes/rootConfig/getRootConfig.js +9 -3
- package/dist/routes/rootConfig/menus/filterMenuList.js +5 -2
- package/dist/routes/websocket/authorizeWebsocket.js +19 -4
- package/dist/routes/websocket/createChannelRegistry.js +39 -14
- package/dist/routes/websocket/createWebSocketConnection.js +162 -28
- package/dist/routes/websocket/getCallerFingerprint.js +31 -0
- package/dist/routes/websocket/prepareChannel.js +29 -1
- package/dist/routes/websocket/resolveWebsocketCaller.js +40 -0
- package/dist/test/testContext.js +16 -11
- package/package.json +28 -12
- package/dist/context/createAuthorize.js +0 -45
- package/dist/response/omitErrorProps.js +0 -51
- package/dist/routes/auth/callbacks/addUserFieldsToToken.js +0 -30
- package/dist/routes/auth/callbacks/createCallbacks.js +0 -46
- package/dist/routes/auth/callbacks/createJWTCallback.js +0 -77
- package/dist/routes/auth/callbacks/createRedirectCallback.js +0 -41
- package/dist/routes/auth/callbacks/createSessionCallback.js +0 -81
- package/dist/routes/auth/callbacks/createSignInCallback.js +0 -40
- package/dist/routes/auth/callbacks/validateSessionRoles.js +0 -33
- package/dist/routes/auth/createLogger.js +0 -35
- package/dist/routes/auth/createPrefixedCookies.js +0 -52
- package/dist/routes/auth/createProviders.js +0 -25
- package/dist/routes/auth/events/createCreateUserEvent.js +0 -41
- package/dist/routes/auth/events/createEvents.js +0 -56
- package/dist/routes/auth/events/createLinkAccountEvent.js +0 -43
- package/dist/routes/auth/events/createSignInEvent.js +0 -46
- package/dist/routes/auth/events/createSignOutEvent.js +0 -43
- package/dist/routes/auth/events/createUpdateUserEvent.js +0 -41
- package/dist/routes/auth/getAuthConfig.js +0 -86
- package/dist/routes/auth/resolveCookies.js +0 -37
- package/dist/routes/log/formatValidationError.js +0 -31
- /package/dist/{routes/notifications → email}/resolveThemeLogo.js +0 -0
|
@@ -12,26 +12,96 @@
|
|
|
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
|
-
*/ import { serializer } from '@lowdefy/helpers';
|
|
15
|
+
*/ import { serializer, type } from '@lowdefy/helpers';
|
|
16
|
+
import { matchPagePath } from '@lowdefy/node-utils';
|
|
16
17
|
import resolveDynamicContent from './dynamic/resolveDynamicContent.js';
|
|
17
|
-
|
|
18
|
+
// Matches the request path (basePath and the leading "/" removed) to a page,
|
|
19
|
+
// then returns a status object so the page route can fork on the auth outcome.
|
|
20
|
+
// A matched path also returns the page's pageId and pathParams.
|
|
21
|
+
// - ok: render the page.
|
|
22
|
+
// - not_found and unauthorized both collapse to the opaque /404 redirect -
|
|
23
|
+
// wrong roles never reveal that the page exists.
|
|
24
|
+
// - unauthenticated: a logged-out human gets the authPages.signIn redirect
|
|
25
|
+
// with a callbackUrl back to the requested page.
|
|
26
|
+
// - enrol_required: an authorised caller who has not enrolled a second factor
|
|
27
|
+
// under auth.twoFactor.required gets the authPages.twoFactorEnrol redirect.
|
|
28
|
+
//
|
|
29
|
+
// An unauthenticated caller never learns whether a page exists (Decision 7):
|
|
30
|
+
// in an app whose unlisted ids default to protected, an unmatched path answers
|
|
31
|
+
// unauthenticated, exactly as a protected one does. So in a protected app, a 404
|
|
32
|
+
// means you are signed in - and every app's 404 page stops needing to serve a
|
|
33
|
+
// signed-out visitor who was refused a protected page.
|
|
34
|
+
//
|
|
35
|
+
// The default is a build-resolved boolean, coarser than the glob lists that
|
|
36
|
+
// produced it on purpose: an id absent from the build could still match a
|
|
37
|
+
// declared pattern, and matching globs on the request path to change the answer
|
|
38
|
+
// for URLs that are wrong anyway is a cost the artifact exists to avoid.
|
|
39
|
+
async function getPageConfig(context, { path, urlQuery }) {
|
|
40
|
+
const routes = await context.readConfigFile('routes.json');
|
|
41
|
+
const match = matchPagePath({
|
|
42
|
+
routes,
|
|
43
|
+
path
|
|
44
|
+
});
|
|
45
|
+
if (match === null) {
|
|
46
|
+
if (type.isNone(context.user) && context.authEnforcement?.pagesProtectedByDefault === true) {
|
|
47
|
+
return {
|
|
48
|
+
status: 'unauthenticated'
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
status: 'not_found'
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
const { pageId, pathParams } = match;
|
|
18
56
|
const pageConfig = await context.readConfigFile(`pages/${pageId}.json`);
|
|
19
|
-
|
|
57
|
+
const outcome = context.authorizeOutcome(pageConfig, {
|
|
58
|
+
pageId
|
|
59
|
+
});
|
|
60
|
+
if (outcome === 'allow') {
|
|
20
61
|
// eslint-disable-next-line no-unused-vars
|
|
21
62
|
const { auth, ...rest } = pageConfig;
|
|
22
63
|
if (rest.dynamic !== true) {
|
|
23
64
|
// Use serializer.serialize to ensure ~k keys (non-enumerable after deserialize)
|
|
24
65
|
// are made enumerable again for JSON transfer to client
|
|
25
|
-
return
|
|
66
|
+
return {
|
|
67
|
+
status: 'ok',
|
|
68
|
+
pageId,
|
|
69
|
+
pathParams,
|
|
70
|
+
pageConfig: serializer.serialize(rest)
|
|
71
|
+
};
|
|
26
72
|
}
|
|
27
73
|
// readConfigFile caches parsed artifacts — deep copy before resolution so
|
|
28
74
|
// one request's resolved content never reaches another via the cache.
|
|
29
75
|
const resolved = await resolveDynamicContent(context, {
|
|
30
76
|
pageConfig: serializer.copy(rest),
|
|
77
|
+
pathParams,
|
|
31
78
|
urlQuery
|
|
32
79
|
});
|
|
33
|
-
return
|
|
80
|
+
return {
|
|
81
|
+
status: 'ok',
|
|
82
|
+
pageId,
|
|
83
|
+
pathParams,
|
|
84
|
+
pageConfig: serializer.serialize(resolved)
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
if (outcome === 'enrol_required') {
|
|
88
|
+
return {
|
|
89
|
+
status: 'enrol_required',
|
|
90
|
+
pageId,
|
|
91
|
+
pathParams
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
if (type.isNone(context.user)) {
|
|
95
|
+
return {
|
|
96
|
+
status: 'unauthenticated',
|
|
97
|
+
pageId,
|
|
98
|
+
pathParams
|
|
99
|
+
};
|
|
34
100
|
}
|
|
35
|
-
return
|
|
101
|
+
return {
|
|
102
|
+
status: 'unauthorized',
|
|
103
|
+
pageId,
|
|
104
|
+
pathParams
|
|
105
|
+
};
|
|
36
106
|
}
|
|
37
107
|
export default getPageConfig;
|
|
@@ -12,18 +12,33 @@
|
|
|
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
|
-
*/ import {
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
*/ import { AuthenticationError, AuthorizationError, TwoFactorEnrolmentRequiredError } from '@lowdefy/errors';
|
|
16
|
+
import { type } from '@lowdefy/helpers';
|
|
17
|
+
function authorizeRequest({ authorizeOutcome: authorize, logger, pageId, user }, { requestConfig }) {
|
|
18
|
+
const outcome = authorize(requestConfig, {
|
|
19
|
+
pageId
|
|
20
|
+
});
|
|
21
|
+
if (outcome !== 'allow') {
|
|
18
22
|
logger.debug({
|
|
19
23
|
event: 'debug_request_authorize',
|
|
20
24
|
authorized: false,
|
|
25
|
+
outcome,
|
|
21
26
|
auth_config: requestConfig.auth
|
|
22
27
|
});
|
|
28
|
+
if (outcome === 'enrol_required') {
|
|
29
|
+
// Reached only after the role check passed, so the caller is authorised and
|
|
30
|
+
// this reveals nothing about what exists (Decision 6). A distinct code, not
|
|
31
|
+
// a 401 - a 401 reads to the client as a dead session and bounces the user
|
|
32
|
+
// to sign-in, which is the loop by another route.
|
|
33
|
+
throw new TwoFactorEnrolmentRequiredError(`Two-factor enrolment required for request "${requestConfig.requestId}".`);
|
|
34
|
+
}
|
|
35
|
+
// Unauthenticated on a protected request - 401 tells the caller to fix
|
|
36
|
+
// its credentials. Wrong roles stay opaque below.
|
|
37
|
+
if (type.isNone(user)) {
|
|
38
|
+
throw new AuthenticationError(`Authentication required for request "${requestConfig.requestId}".`);
|
|
39
|
+
}
|
|
23
40
|
// Throw does not exist error to avoid leaking information that request exists to unauthorized users
|
|
24
|
-
throw new
|
|
25
|
-
configKey: requestConfig['~k']
|
|
26
|
-
});
|
|
41
|
+
throw new AuthorizationError(`Request "${requestConfig.requestId}" does not exist.`);
|
|
27
42
|
}
|
|
28
43
|
logger.debug({
|
|
29
44
|
event: 'debug_request_authorize',
|
|
@@ -22,6 +22,7 @@ import getConnection from '../connections/getConnection.js';
|
|
|
22
22
|
import getConnectionConfig from '../connections/getConnectionConfig.js';
|
|
23
23
|
import getRequestConfig from './getRequestConfig.js';
|
|
24
24
|
import getRequestResolver from './getRequestResolver.js';
|
|
25
|
+
import resolveTenancy from './resolveTenancy.js';
|
|
25
26
|
import validateSchemas from './validateSchemas.js';
|
|
26
27
|
import createEvaluateOperators from '../../context/createEvaluateOperators.js';
|
|
27
28
|
import redactResponse from '../../response/redactResponse.js';
|
|
@@ -29,6 +30,7 @@ async function callRequest(context, { blockId, pageId, payload, requestId }) {
|
|
|
29
30
|
const { logger } = context;
|
|
30
31
|
context.blockId = blockId;
|
|
31
32
|
context.pageId = pageId;
|
|
33
|
+
context.requestId = requestId;
|
|
32
34
|
const requestPayload = serializer.deserialize(payload);
|
|
33
35
|
context.payload = requestPayload;
|
|
34
36
|
context.evaluateOperators = createEvaluateOperators(context);
|
|
@@ -57,12 +59,25 @@ async function callRequest(context, { blockId, pageId, payload, requestId }) {
|
|
|
57
59
|
connection,
|
|
58
60
|
requestConfig
|
|
59
61
|
});
|
|
60
|
-
const {
|
|
62
|
+
const { tenant, tenantGuard } = resolveTenancy(context, {
|
|
63
|
+
connection,
|
|
61
64
|
connectionConfig,
|
|
62
|
-
payload: requestPayload,
|
|
63
65
|
requestConfig,
|
|
66
|
+
writes: requestResolver.meta.checkWrite === true
|
|
67
|
+
});
|
|
68
|
+
// A page request runs outside any routine, so it evaluates against an empty frame.
|
|
69
|
+
const routineContext = {
|
|
70
|
+
arrayIndices: [],
|
|
71
|
+
error: null,
|
|
72
|
+
items: {},
|
|
73
|
+
payload: requestPayload,
|
|
64
74
|
state: {},
|
|
65
75
|
steps: {}
|
|
76
|
+
};
|
|
77
|
+
const { connectionProperties, requestProperties } = evaluateOperators(context, {
|
|
78
|
+
connectionConfig,
|
|
79
|
+
requestConfig,
|
|
80
|
+
routineContext
|
|
66
81
|
});
|
|
67
82
|
checkConnectionRead(context, {
|
|
68
83
|
connectionConfig,
|
|
@@ -88,7 +103,10 @@ async function callRequest(context, { blockId, pageId, payload, requestId }) {
|
|
|
88
103
|
endpointDepth: 0,
|
|
89
104
|
requestConfig,
|
|
90
105
|
requestProperties,
|
|
91
|
-
requestResolver
|
|
106
|
+
requestResolver,
|
|
107
|
+
routineContext,
|
|
108
|
+
tenant,
|
|
109
|
+
tenantGuard
|
|
92
110
|
});
|
|
93
111
|
return {
|
|
94
112
|
id: requestConfig.id,
|
|
@@ -12,10 +12,13 @@
|
|
|
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
|
-
*/ import { RequestError, ServiceError } from '@lowdefy/errors';
|
|
15
|
+
*/ import { RequestError, ServiceError, UserError } from '@lowdefy/errors';
|
|
16
|
+
import getCurrentEnvironment from '../../context/getCurrentEnvironment.js';
|
|
17
|
+
import createWalledResolver from './createWalledResolver.js';
|
|
16
18
|
import invokeEndpoint from '../endpoints/invokeEndpoint.js';
|
|
17
|
-
|
|
18
|
-
|
|
19
|
+
import maskCredentialProperties from './maskCredentialProperties.js';
|
|
20
|
+
async function callRequestResolver(context, { agent = null, connectionProperties, endpointDepth, requestConfig, requestProperties, requestResolver, routineContext, tenant, tenantGuard }) {
|
|
21
|
+
const { blockId, endpointId, logger, pageId, payload, signal } = context;
|
|
19
22
|
// stepId for endpoint steps (after build), requestId for page requests
|
|
20
23
|
const stepOrRequestId = requestConfig.stepId ?? requestConfig.requestId;
|
|
21
24
|
const callApi = async ({ endpointId: targetEndpointId, payload: targetPayload } = {})=>{
|
|
@@ -26,6 +29,7 @@ async function callRequestResolver(context, { connectionProperties, endpointDept
|
|
|
26
29
|
endpointId: targetEndpointId
|
|
27
30
|
});
|
|
28
31
|
const result = await invokeEndpoint(context, {
|
|
32
|
+
agent,
|
|
29
33
|
endpointId: targetEndpointId,
|
|
30
34
|
payload: targetPayload,
|
|
31
35
|
endpointDepth
|
|
@@ -49,10 +53,37 @@ async function callRequestResolver(context, { connectionProperties, endpointDept
|
|
|
49
53
|
connection: connectionProperties,
|
|
50
54
|
connectionId: requestConfig.connectionId,
|
|
51
55
|
endpointId,
|
|
56
|
+
// The deployment's current environment ({ name, url, email, ... }) or null, so connection
|
|
57
|
+
// types can apply environment settings such as the email delivery filter.
|
|
58
|
+
environment: getCurrentEnvironment({
|
|
59
|
+
config: context.config
|
|
60
|
+
}),
|
|
52
61
|
pageId,
|
|
53
62
|
payload,
|
|
54
63
|
request: requestProperties,
|
|
55
|
-
requestId: stepOrRequestId
|
|
64
|
+
requestId: stepOrRequestId,
|
|
65
|
+
// Aborts when the request that started this work closes: the client
|
|
66
|
+
// disconnected or the server's request timeout answered. Undefined for work
|
|
67
|
+
// nobody waits on (background runs, agent chat). Connection types hand it to
|
|
68
|
+
// their upstream calls so a closed request stops them.
|
|
69
|
+
signal,
|
|
70
|
+
// Names a walled MongoDB connection and returns the bound arguments of
|
|
71
|
+
// getWalledCollection (@lowdefy/connection-mongodb/walled): the only way a
|
|
72
|
+
// plugin reaches a walled database.
|
|
73
|
+
walled: createWalledResolver(context, {
|
|
74
|
+
requestConfig,
|
|
75
|
+
routineContext
|
|
76
|
+
}),
|
|
77
|
+
// The tenant verdict ({ field, value } or null/undefined) computed by
|
|
78
|
+
// resolveTenancy - connection types implementing the scoping contract
|
|
79
|
+
// enforce it (stamp writes, merge filters, inject pipeline matches).
|
|
80
|
+
tenant: tenant ?? null,
|
|
81
|
+
// The unscoped guard ({ field, readOnly } or null) computed by
|
|
82
|
+
// resolveTenancy: readOnly for tenant: none requests, which scoping
|
|
83
|
+
// connection types refuse to write under, and the write guard for shared
|
|
84
|
+
// connections over a walled collection, under which they assert every
|
|
85
|
+
// row the request writes still carries a real organization id.
|
|
86
|
+
tenantGuard: tenantGuard ?? null
|
|
56
87
|
});
|
|
57
88
|
return response;
|
|
58
89
|
} catch (error) {
|
|
@@ -72,7 +103,22 @@ async function callRequestResolver(context, { connectionProperties, endpointDept
|
|
|
72
103
|
}, error.message);
|
|
73
104
|
throw error;
|
|
74
105
|
}
|
|
75
|
-
//
|
|
106
|
+
// The client left before the resolver finished and the resolver stopped on the
|
|
107
|
+
// signal: the cancellation is the caller's doing, not a fault in the request.
|
|
108
|
+
if (signal?.reason?.name === 'AbortError' && error.name === 'AbortError') {
|
|
109
|
+
const cancelled = new UserError(signal.reason.message, {
|
|
110
|
+
cause: error
|
|
111
|
+
});
|
|
112
|
+
logger.debug({
|
|
113
|
+
params: {
|
|
114
|
+
id: stepOrRequestId,
|
|
115
|
+
type: requestConfig.type
|
|
116
|
+
},
|
|
117
|
+
err: cancelled
|
|
118
|
+
}, cancelled.message);
|
|
119
|
+
throw cancelled;
|
|
120
|
+
}
|
|
121
|
+
// Check if this is a service error (network, timeout, 5xx, 429)
|
|
76
122
|
if (ServiceError.isServiceError(error)) {
|
|
77
123
|
const serviceError = new ServiceError(undefined, {
|
|
78
124
|
cause: error,
|
|
@@ -92,7 +138,10 @@ async function callRequestResolver(context, { connectionProperties, endpointDept
|
|
|
92
138
|
const requestError = new RequestError(error.message, {
|
|
93
139
|
cause: error,
|
|
94
140
|
typeName: requestConfig.type,
|
|
95
|
-
received:
|
|
141
|
+
received: maskCredentialProperties({
|
|
142
|
+
requestProperties,
|
|
143
|
+
requestResolver
|
|
144
|
+
}),
|
|
96
145
|
location: `${requestConfig.connectionId}/${stepOrRequestId}`,
|
|
97
146
|
configKey: requestConfig['~k']
|
|
98
147
|
});
|
|
@@ -22,7 +22,7 @@ function checkConnectionRead({ logger }, { connectionConfig, connectionPropertie
|
|
|
22
22
|
logger.debug({
|
|
23
23
|
params: {
|
|
24
24
|
connectionId: connectionConfig.connectionId,
|
|
25
|
-
requestId: requestConfig.requestId,
|
|
25
|
+
requestId: requestConfig.stepId ?? requestConfig.requestId,
|
|
26
26
|
configKey
|
|
27
27
|
},
|
|
28
28
|
err
|
|
@@ -22,7 +22,7 @@ function checkConnectionWrite({ logger }, { connectionConfig, connectionProperti
|
|
|
22
22
|
logger.debug({
|
|
23
23
|
params: {
|
|
24
24
|
connectionId: connectionConfig.connectionId,
|
|
25
|
-
requestId: requestConfig.requestId,
|
|
25
|
+
requestId: requestConfig.stepId ?? requestConfig.requestId,
|
|
26
26
|
configKey
|
|
27
27
|
},
|
|
28
28
|
err
|
|
@@ -0,0 +1,75 @@
|
|
|
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 { ConfigError } from '@lowdefy/errors';
|
|
16
|
+
import evaluateRoutineOperators from '../endpoints/evaluateRoutineOperators.js';
|
|
17
|
+
import getConnection from '../connections/getConnection.js';
|
|
18
|
+
import getConnectionConfig from '../connections/getConnectionConfig.js';
|
|
19
|
+
import resolveTenancy from './resolveTenancy.js';
|
|
20
|
+
// The seam a plugin uses to reach a walled MongoDB collection: the resolver
|
|
21
|
+
// receives `walled(connectionId)`. It resolves the named connection from the
|
|
22
|
+
// app config, evaluates its properties against the calling request's frame,
|
|
23
|
+
// and computes the tenant verdict and unscoped-write guard for THIS request
|
|
24
|
+
// (the same resolveTenancy the request layer runs, so tenant: none and shared
|
|
25
|
+
// connections behave exactly as they do for a stock request). What it returns
|
|
26
|
+
// is the argument bag of getWalledCollection in
|
|
27
|
+
// @lowdefy/connection-mongodb/walled, so the plugin never sees the raw client:
|
|
28
|
+
//
|
|
29
|
+
// const collection = getWalledCollection(await walled('mongo'));
|
|
30
|
+
//
|
|
31
|
+
// Only a connection type that implements the scoping contract (meta.tenant)
|
|
32
|
+
// can be named; anything else has no wall to apply.
|
|
33
|
+
function createWalledResolver(context, { requestConfig, routineContext }) {
|
|
34
|
+
const frame = routineContext ?? {
|
|
35
|
+
arrayIndices: [],
|
|
36
|
+
error: null,
|
|
37
|
+
items: {},
|
|
38
|
+
payload: context.payload ?? {},
|
|
39
|
+
state: {},
|
|
40
|
+
steps: {}
|
|
41
|
+
};
|
|
42
|
+
return async function walled(connectionId) {
|
|
43
|
+
const configKey = requestConfig['~k'];
|
|
44
|
+
const connectionConfig = await getConnectionConfig(context, {
|
|
45
|
+
connectionId,
|
|
46
|
+
configKey
|
|
47
|
+
});
|
|
48
|
+
const connection = getConnection(context, {
|
|
49
|
+
connectionConfig
|
|
50
|
+
});
|
|
51
|
+
if (connection.meta?.tenant !== true) {
|
|
52
|
+
throw new ConfigError(`Connection "${connectionId}" (${connectionConfig.type}) can not be used as a walled MongoDB client: its type does not implement the tenant scoping contract.`, {
|
|
53
|
+
configKey
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
const { tenant, tenantGuard } = resolveTenancy(context, {
|
|
57
|
+
connection,
|
|
58
|
+
connectionConfig,
|
|
59
|
+
requestConfig
|
|
60
|
+
});
|
|
61
|
+
const properties = evaluateRoutineOperators(context, frame, {
|
|
62
|
+
input: connectionConfig.properties || {},
|
|
63
|
+
location: connectionId
|
|
64
|
+
});
|
|
65
|
+
return {
|
|
66
|
+
connection: properties,
|
|
67
|
+
connectionId,
|
|
68
|
+
endpointId: context.endpointId,
|
|
69
|
+
requestId: requestConfig.stepId ?? requestConfig.requestId,
|
|
70
|
+
tenant,
|
|
71
|
+
tenantGuard
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
export default createWalledResolver;
|
|
@@ -12,21 +12,17 @@
|
|
|
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
|
-
*/
|
|
16
|
-
|
|
15
|
+
*/ import evaluateRoutineOperators from '../endpoints/evaluateRoutineOperators.js';
|
|
16
|
+
// Connection and request properties evaluate against the same frame, so `_item` in a connection's
|
|
17
|
+
// properties resolves inside a `:for` loop just like it does in the request's.
|
|
18
|
+
function evaluateOperators(context, { connectionConfig, requestConfig, routineContext }) {
|
|
19
|
+
const connectionProperties = evaluateRoutineOperators(context, routineContext, {
|
|
17
20
|
input: connectionConfig.properties || {},
|
|
18
|
-
location: connectionConfig.connectionId
|
|
19
|
-
payload,
|
|
20
|
-
state,
|
|
21
|
-
steps
|
|
21
|
+
location: connectionConfig.connectionId
|
|
22
22
|
});
|
|
23
|
-
const requestProperties =
|
|
23
|
+
const requestProperties = evaluateRoutineOperators(context, routineContext, {
|
|
24
24
|
input: requestConfig.properties || {},
|
|
25
|
-
|
|
26
|
-
location: requestConfig.stepId ?? requestConfig.requestId,
|
|
27
|
-
payload,
|
|
28
|
-
state,
|
|
29
|
-
steps
|
|
25
|
+
location: requestConfig.stepId ?? requestConfig.requestId
|
|
30
26
|
});
|
|
31
27
|
return {
|
|
32
28
|
connectionProperties,
|
|
@@ -12,11 +12,27 @@
|
|
|
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
|
-
*/ import { ConfigError } from '@lowdefy/errors';
|
|
16
|
-
|
|
15
|
+
*/ import { AuthenticationError, ConfigError } from '@lowdefy/errors';
|
|
16
|
+
import { type } from '@lowdefy/helpers';
|
|
17
|
+
// A missing request id answers the same as a protected one for an anonymous
|
|
18
|
+
// human on an auth'd app: before you authenticate, the only answer is
|
|
19
|
+
// authenticate. Otherwise a logged-out caller enumerates request ids by
|
|
20
|
+
// response difference - 500 for a miss (the handler special-cases only
|
|
21
|
+
// AuthenticationError) versus 401 for a protected one. An authenticated caller
|
|
22
|
+
// gets today's opaque "does not exist" for both a miss and a role refusal,
|
|
23
|
+
// unchanged.
|
|
24
|
+
//
|
|
25
|
+
// Guarded on authEnforcement != null (auth configured) and system !== true (not
|
|
26
|
+
// the engine talking to itself), exactly as the page fork is: "no user" is not
|
|
27
|
+
// "anonymous human who should sign in" - it is also "no auth here" and "this is
|
|
28
|
+
// a system run", and neither should be told to authenticate against nothing.
|
|
29
|
+
// This is NOT a per-entity apiProtectedByDefault default; no such glob surface
|
|
30
|
+
// exists.
|
|
31
|
+
async function getRequestConfig({ authEnforcement, logger, readConfigFile, system, user }, { pageId, requestId }) {
|
|
17
32
|
const request = await readConfigFile(`pages/${pageId}/requests/${requestId}.json`);
|
|
18
33
|
if (!request) {
|
|
19
|
-
const
|
|
34
|
+
const unauthenticatedHuman = type.isNone(user) && !type.isNone(authEnforcement) && system !== true;
|
|
35
|
+
const err = unauthenticatedHuman ? new AuthenticationError(`Authentication required for request "${requestId}".`) : new ConfigError(`Request "${requestId}" does not exist.`);
|
|
20
36
|
logger.debug({
|
|
21
37
|
params: {
|
|
22
38
|
pageId,
|
|
@@ -0,0 +1,34 @@
|
|
|
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
|
+
*/ // A request type names in meta.credentialProperties the request properties that carry a
|
|
16
|
+
// credential the secret scrub cannot recognise, such as a presigned link, whose signature is
|
|
17
|
+
// derived from a secret rather than being one. Their values are masked in the request properties
|
|
18
|
+
// an error keeps as received, so the error log never holds them.
|
|
19
|
+
function maskCredentialProperties({ requestProperties, requestResolver }) {
|
|
20
|
+
const credentialProperties = requestResolver.meta.credentialProperties ?? [];
|
|
21
|
+
if (credentialProperties.length === 0) {
|
|
22
|
+
return requestProperties;
|
|
23
|
+
}
|
|
24
|
+
const masked = {
|
|
25
|
+
...requestProperties
|
|
26
|
+
};
|
|
27
|
+
credentialProperties.forEach((property)=>{
|
|
28
|
+
if (Object.hasOwn(masked, property)) {
|
|
29
|
+
masked[property] = '[REDACTED]';
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
return masked;
|
|
33
|
+
}
|
|
34
|
+
export default maskCredentialProperties;
|