@salesforce/b2c-tooling-sdk 1.0.1 → 1.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.
- package/dist/cjs/cli/base-command.d.ts +33 -8
- package/dist/cjs/cli/base-command.js +92 -35
- package/dist/cjs/cli/base-command.js.map +1 -1
- package/dist/cjs/clients/middleware.d.ts +15 -10
- package/dist/cjs/clients/middleware.js +22 -15
- package/dist/cjs/clients/middleware.js.map +1 -1
- package/dist/cjs/clients/webdav.d.ts +22 -0
- package/dist/cjs/clients/webdav.js +46 -0
- package/dist/cjs/clients/webdav.js.map +1 -1
- package/dist/cjs/config/dw-json.d.ts +32 -0
- package/dist/cjs/config/dw-json.js.map +1 -1
- package/dist/cjs/config/mapping.js +55 -0
- package/dist/cjs/config/mapping.js.map +1 -1
- package/dist/cjs/config/sources/env-source.js +3 -1
- package/dist/cjs/config/sources/env-source.js.map +1 -1
- package/dist/cjs/config/types.d.ts +15 -0
- package/dist/cjs/index.d.ts +3 -2
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/safety/index.d.ts +7 -2
- package/dist/cjs/safety/index.js +5 -1
- package/dist/cjs/safety/index.js.map +1 -1
- package/dist/cjs/safety/safety-guard.d.ts +92 -0
- package/dist/cjs/safety/safety-guard.js +270 -0
- package/dist/cjs/safety/safety-guard.js.map +1 -0
- package/dist/cjs/safety/safety-middleware.d.ts +86 -1
- package/dist/cjs/safety/safety-middleware.js +165 -16
- package/dist/cjs/safety/safety-middleware.js.map +1 -1
- package/dist/cjs/safety/types.d.ts +81 -0
- package/dist/cjs/safety/types.js +16 -0
- package/dist/cjs/safety/types.js.map +1 -0
- package/dist/cjs/safety/with-confirmation.d.ts +58 -0
- package/dist/cjs/safety/with-confirmation.js +67 -0
- package/dist/cjs/safety/with-confirmation.js.map +1 -0
- package/dist/cjs/ux/confirm.d.ts +14 -0
- package/dist/cjs/ux/confirm.js +36 -0
- package/dist/cjs/ux/confirm.js.map +1 -0
- package/dist/esm/cli/base-command.d.ts +33 -8
- package/dist/esm/cli/base-command.js +92 -35
- package/dist/esm/cli/base-command.js.map +1 -1
- package/dist/esm/clients/middleware.d.ts +15 -10
- package/dist/esm/clients/middleware.js +22 -15
- package/dist/esm/clients/middleware.js.map +1 -1
- package/dist/esm/clients/webdav.d.ts +22 -0
- package/dist/esm/clients/webdav.js +46 -0
- package/dist/esm/clients/webdav.js.map +1 -1
- package/dist/esm/config/dw-json.d.ts +32 -0
- package/dist/esm/config/dw-json.js.map +1 -1
- package/dist/esm/config/mapping.js +55 -0
- package/dist/esm/config/mapping.js.map +1 -1
- package/dist/esm/config/sources/env-source.js +3 -1
- package/dist/esm/config/sources/env-source.js.map +1 -1
- package/dist/esm/config/types.d.ts +15 -0
- package/dist/esm/index.d.ts +3 -2
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/safety/index.d.ts +7 -2
- package/dist/esm/safety/index.js +5 -1
- package/dist/esm/safety/index.js.map +1 -1
- package/dist/esm/safety/safety-guard.d.ts +92 -0
- package/dist/esm/safety/safety-guard.js +270 -0
- package/dist/esm/safety/safety-guard.js.map +1 -0
- package/dist/esm/safety/safety-middleware.d.ts +86 -1
- package/dist/esm/safety/safety-middleware.js +165 -16
- package/dist/esm/safety/safety-middleware.js.map +1 -1
- package/dist/esm/safety/types.d.ts +81 -0
- package/dist/esm/safety/types.js +16 -0
- package/dist/esm/safety/types.js.map +1 -0
- package/dist/esm/safety/with-confirmation.d.ts +58 -0
- package/dist/esm/safety/with-confirmation.js +67 -0
- package/dist/esm/safety/with-confirmation.js.map +1 -0
- package/dist/esm/ux/confirm.d.ts +14 -0
- package/dist/esm/ux/confirm.js +36 -0
- package/dist/esm/ux/confirm.js.map +1 -0
- package/package.json +1 -1
package/dist/esm/index.js
CHANGED
|
@@ -31,7 +31,7 @@ export { getRole, listRoles } from './operations/roles/index.js';
|
|
|
31
31
|
// Operations - Organizations
|
|
32
32
|
export { getOrg, getOrgByName, listOrgs } from './operations/orgs/index.js';
|
|
33
33
|
// Safety - Protection against destructive operations
|
|
34
|
-
export { getSafetyLevel, describeSafetyLevel, checkSafetyViolation, SafetyBlockedError } from './safety/index.js';
|
|
34
|
+
export { getSafetyLevel, describeSafetyLevel, checkSafetyViolation, checkLevelViolation, SafetyBlockedError, SafetyConfirmationRequired, SafetyGuard, extractJobIdFromPath, maxSafetyLevel, isValidSafetyLevel, parseSafetyLevelString, resolveEffectiveSafetyConfig, loadGlobalSafetyConfig, isValidSafetyAction, VALID_SAFETY_ACTIONS, withSafetyConfirmation, } from './safety/index.js';
|
|
35
35
|
// Defaults
|
|
36
36
|
export { DEFAULT_ACCOUNT_MANAGER_HOST, DEFAULT_ODS_HOST, DEFAULT_PUBLIC_CLIENT_ID, getDefaultPublicClientId, } from './defaults.js';
|
|
37
37
|
// Version info
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAC,YAAY,EAAE,eAAe,EAAE,SAAS,EAAE,WAAW,EAAE,kBAAkB,EAAC,MAAM,oBAAoB,CAAC;AAG7G,OAAO;AACP,OAAO,EAAC,CAAC,EAAE,WAAW,EAAE,WAAW,EAAE,eAAe,EAAE,oBAAoB,EAAE,aAAa,EAAC,MAAM,iBAAiB,CAAC;AAGlH,SAAS;AACT,OAAO,EAAC,UAAU,EAAE,UAAU,EAAE,aAAa,EAAE,cAAc,EAAE,oBAAoB,EAAC,MAAM,mBAAmB,CAAC;AAe9G,yCAAyC;AACzC,OAAO,EACL,iBAAiB,EACjB,aAAa,EACb,qBAAqB,EACrB,cAAc,EACd,SAAS,EACT,mBAAmB,EACnB,yBAAyB,EACzB,gBAAgB,GACjB,MAAM,iBAAiB,CAAC;AAiBzB,2BAA2B;AAC3B,OAAO,EAAC,WAAW,EAAC,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B,gBAAgB,GACjB,MAAM,2BAA2B,CAAC;AAKnC,mBAAmB;AACnB,OAAO,EACL,iBAAiB,EACjB,gBAAgB,EAChB,gBAAgB,EAChB,kBAAkB,EAClB,cAAc,EACd,aAAa,GACd,MAAM,2BAA2B,CAAC;AAiBnC,qBAAqB;AACrB,OAAO,EACL,OAAO,EACP,cAAc,EACd,SAAS,EACT,UAAU,EACV,UAAU,EACV,UAAU,EACV,SAAS,EACT,SAAS,EACT,SAAS,EACT,UAAU,GACX,MAAM,6BAA6B,CAAC;AAErC,qBAAqB;AACrB,OAAO,EAAC,OAAO,EAAE,SAAS,EAAC,MAAM,6BAA6B,CAAC;AAE/D,6BAA6B;AAC7B,OAAO,EAAC,MAAM,EAAE,YAAY,EAAE,QAAQ,EAAC,MAAM,4BAA4B,CAAC;AAE1E,qDAAqD;AACrD,OAAO,EACL,cAAc,EACd,mBAAmB,EACnB,oBAAoB,EACpB,mBAAmB,EACnB,kBAAkB,EAClB,0BAA0B,EAC1B,WAAW,EACX,oBAAoB,EACpB,cAAc,EACd,kBAAkB,EAClB,sBAAsB,EACtB,4BAA4B,EAC5B,sBAAsB,EACtB,mBAAmB,EACnB,oBAAoB,EACpB,sBAAsB,GACvB,MAAM,mBAAmB,CAAC;AAI3B,WAAW;AACX,OAAO,EACL,4BAA4B,EAC5B,gBAAgB,EAChB,wBAAwB,EACxB,wBAAwB,GACzB,MAAM,eAAe,CAAC;AAEvB,eAAe;AACf,OAAO,EAAC,QAAQ,EAAE,WAAW,EAAE,cAAc,EAAC,MAAM,cAAc,CAAC"}
|
|
@@ -3,5 +3,10 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module safety
|
|
5
5
|
*/
|
|
6
|
-
export type {
|
|
7
|
-
export {
|
|
6
|
+
export type { SafetyAction, SafetyRule, SafetyOperation, SafetyEvaluation } from './types.js';
|
|
7
|
+
export { isValidSafetyAction, VALID_SAFETY_ACTIONS } from './types.js';
|
|
8
|
+
export type { SafetyLevel, SafetyConfig, SafetyConfigFragment } from './safety-middleware.js';
|
|
9
|
+
export { SafetyBlockedError, SafetyConfirmationRequired, checkSafetyViolation, checkLevelViolation, getSafetyLevel, describeSafetyLevel, maxSafetyLevel, isValidSafetyLevel, parseSafetyLevelString, resolveEffectiveSafetyConfig, loadGlobalSafetyConfig, } from './safety-middleware.js';
|
|
10
|
+
export { SafetyGuard, extractJobIdFromPath } from './safety-guard.js';
|
|
11
|
+
export type { ConfirmHandler } from './with-confirmation.js';
|
|
12
|
+
export { withSafetyConfirmation } from './with-confirmation.js';
|
package/dist/esm/safety/index.js
CHANGED
|
@@ -3,5 +3,9 @@
|
|
|
3
3
|
* SPDX-License-Identifier: Apache-2
|
|
4
4
|
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
|
|
5
5
|
*/
|
|
6
|
-
export {
|
|
6
|
+
export { isValidSafetyAction, VALID_SAFETY_ACTIONS } from './types.js';
|
|
7
|
+
export { SafetyBlockedError, SafetyConfirmationRequired, checkSafetyViolation, checkLevelViolation, getSafetyLevel, describeSafetyLevel, maxSafetyLevel, isValidSafetyLevel, parseSafetyLevelString, resolveEffectiveSafetyConfig, loadGlobalSafetyConfig, } from './safety-middleware.js';
|
|
8
|
+
// SafetyGuard
|
|
9
|
+
export { SafetyGuard, extractJobIdFromPath } from './safety-guard.js';
|
|
10
|
+
export { withSafetyConfirmation } from './with-confirmation.js';
|
|
7
11
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/safety/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/safety/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAUH,OAAO,EAAC,mBAAmB,EAAE,oBAAoB,EAAC,MAAM,YAAY,CAAC;AAIrE,OAAO,EACL,kBAAkB,EAClB,0BAA0B,EAC1B,oBAAoB,EACpB,mBAAmB,EACnB,cAAc,EACd,mBAAmB,EACnB,cAAc,EACd,kBAAkB,EAClB,sBAAsB,EACtB,4BAA4B,EAC5B,sBAAsB,GACvB,MAAM,wBAAwB,CAAC;AAEhC,cAAc;AACd,OAAO,EAAC,WAAW,EAAE,oBAAoB,EAAC,MAAM,mBAAmB,CAAC;AAIpE,OAAO,EAAC,sBAAsB,EAAC,MAAM,wBAAwB,CAAC"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { SafetyConfig } from './safety-middleware.js';
|
|
2
|
+
import type { SafetyEvaluation, SafetyOperation, SafetyRule } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Extract a job ID from a URL path if it's a job execution endpoint.
|
|
5
|
+
*
|
|
6
|
+
* Matches patterns like:
|
|
7
|
+
* - `/s/-/dw/data/v24_5/jobs/sfcc-site-archive-import/executions`
|
|
8
|
+
* - `/jobs/sfcc-site-archive-export/executions`
|
|
9
|
+
*/
|
|
10
|
+
export declare function extractJobIdFromPath(path: string): string | undefined;
|
|
11
|
+
/**
|
|
12
|
+
* SafetyGuard evaluates operations against safety rules and levels.
|
|
13
|
+
*
|
|
14
|
+
* The guard provides three levels of API:
|
|
15
|
+
* - {@link evaluate} — returns a {@link SafetyEvaluation} describing what should happen
|
|
16
|
+
* - {@link assert} — throws {@link SafetyBlockedError} or {@link SafetyConfirmationRequired}
|
|
17
|
+
* - {@link temporarilyAllow} — creates a scoped exemption for confirmed operations
|
|
18
|
+
*
|
|
19
|
+
* The HTTP middleware uses the guard internally so all HTTP requests are
|
|
20
|
+
* evaluated automatically. CLI commands and other consumers can use the
|
|
21
|
+
* guard directly for richer safety interaction (command-level checks,
|
|
22
|
+
* confirmation flows).
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* const guard = new SafetyGuard({
|
|
27
|
+
* level: 'NO_UPDATE',
|
|
28
|
+
* confirm: true,
|
|
29
|
+
* rules: [{ job: 'sfcc-site-archive-export', action: 'allow' }],
|
|
30
|
+
* });
|
|
31
|
+
*
|
|
32
|
+
* const evaluation = guard.evaluate({ type: 'job', jobId: 'sfcc-site-archive-export' });
|
|
33
|
+
* // evaluation.action === 'allow'
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare class SafetyGuard {
|
|
37
|
+
readonly config: SafetyConfig;
|
|
38
|
+
private temporaryAllows;
|
|
39
|
+
private readonly logger;
|
|
40
|
+
constructor(config: SafetyConfig);
|
|
41
|
+
/**
|
|
42
|
+
* Evaluate an operation against safety rules and level.
|
|
43
|
+
*
|
|
44
|
+
* Evaluation order:
|
|
45
|
+
* 1. Temporary allows (from confirmed retries) — if matched, allow
|
|
46
|
+
* 2. Config rules in order — first matching rule's action wins
|
|
47
|
+
* 3. Level-based default — confirm if `confirm: true`, otherwise block/allow
|
|
48
|
+
*
|
|
49
|
+
* All evaluations are trace-logged for diagnostics.
|
|
50
|
+
*/
|
|
51
|
+
evaluate(operation: SafetyOperation): SafetyEvaluation;
|
|
52
|
+
/**
|
|
53
|
+
* Assert that an operation is allowed.
|
|
54
|
+
*
|
|
55
|
+
* @throws {SafetyBlockedError} if the operation is blocked
|
|
56
|
+
* @throws {SafetyConfirmationRequired} if the operation needs confirmation
|
|
57
|
+
*/
|
|
58
|
+
assert(operation: SafetyOperation): void;
|
|
59
|
+
/**
|
|
60
|
+
* Create a temporary exemption for a confirmed operation.
|
|
61
|
+
*
|
|
62
|
+
* Returns a cleanup function that removes the exemption. Use this
|
|
63
|
+
* to retry an operation after the user has confirmed.
|
|
64
|
+
*/
|
|
65
|
+
temporarilyAllow(operation: SafetyOperation): () => void;
|
|
66
|
+
/**
|
|
67
|
+
* Add a temporary safety rule for a scoped exemption.
|
|
68
|
+
*
|
|
69
|
+
* Unlike {@link temporarilyAllow} which derives a rule from an operation,
|
|
70
|
+
* this accepts an arbitrary rule — useful for granting broad temporary
|
|
71
|
+
* access (e.g., allowing WebDAV DELETE on Impex paths during a job export).
|
|
72
|
+
*
|
|
73
|
+
* Returns a cleanup function that removes the rule.
|
|
74
|
+
*/
|
|
75
|
+
temporarilyAddRule(rule: SafetyRule): () => void;
|
|
76
|
+
/**
|
|
77
|
+
* Evaluate an operation using only the safety level (no rules).
|
|
78
|
+
*/
|
|
79
|
+
private evaluateByLevel;
|
|
80
|
+
/**
|
|
81
|
+
* Convert an operation to a temporary allow rule for retry.
|
|
82
|
+
*/
|
|
83
|
+
private operationToRule;
|
|
84
|
+
/**
|
|
85
|
+
* Describe why a rule matched, for user-facing messages.
|
|
86
|
+
*/
|
|
87
|
+
private describeRuleMatch;
|
|
88
|
+
/**
|
|
89
|
+
* Describe why the level blocked an operation, for user-facing messages.
|
|
90
|
+
*/
|
|
91
|
+
private describeLevelBlock;
|
|
92
|
+
}
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025, Salesforce, Inc.
|
|
3
|
+
* SPDX-License-Identifier: Apache-2
|
|
4
|
+
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* SafetyGuard: SDK-level safety evaluation engine.
|
|
8
|
+
*
|
|
9
|
+
* Evaluates operations against safety rules and levels, producing typed
|
|
10
|
+
* evaluations (allow/block/confirm). Used by both the HTTP middleware
|
|
11
|
+
* (automatic) and command-level checks (opt-in).
|
|
12
|
+
*
|
|
13
|
+
* @module safety/safety-guard
|
|
14
|
+
*/
|
|
15
|
+
import { Minimatch } from 'minimatch';
|
|
16
|
+
import { getLogger } from '../logging/index.js';
|
|
17
|
+
import { SafetyBlockedError, SafetyConfirmationRequired, checkLevelViolation, describeSafetyLevel, } from './safety-middleware.js';
|
|
18
|
+
/** Regex to extract job ID from OCAPI job execution URLs. */
|
|
19
|
+
const JOB_EXECUTION_PATTERN = /\/jobs\/([^/]+)\/executions/;
|
|
20
|
+
/**
|
|
21
|
+
* Extract a job ID from a URL path if it's a job execution endpoint.
|
|
22
|
+
*
|
|
23
|
+
* Matches patterns like:
|
|
24
|
+
* - `/s/-/dw/data/v24_5/jobs/sfcc-site-archive-import/executions`
|
|
25
|
+
* - `/jobs/sfcc-site-archive-export/executions`
|
|
26
|
+
*/
|
|
27
|
+
export function extractJobIdFromPath(path) {
|
|
28
|
+
const match = JOB_EXECUTION_PATTERN.exec(path);
|
|
29
|
+
return match?.[1];
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Test whether a string matches a glob pattern.
|
|
33
|
+
* Uses minimatch with dot matching enabled.
|
|
34
|
+
*/
|
|
35
|
+
function matchGlob(value, pattern) {
|
|
36
|
+
const matcher = new Minimatch(pattern, { dot: true, nocase: true });
|
|
37
|
+
return matcher.match(value);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Check if a rule matches an operation.
|
|
41
|
+
*/
|
|
42
|
+
function ruleMatchesOperation(rule, operation) {
|
|
43
|
+
// Command matcher
|
|
44
|
+
if (rule.command !== undefined) {
|
|
45
|
+
if (!operation.commandId)
|
|
46
|
+
return false;
|
|
47
|
+
return matchGlob(operation.commandId, rule.command);
|
|
48
|
+
}
|
|
49
|
+
// Job matcher
|
|
50
|
+
if (rule.job !== undefined) {
|
|
51
|
+
const jobId = operation.jobId ?? (operation.path ? extractJobIdFromPath(operation.path) : undefined);
|
|
52
|
+
if (!jobId)
|
|
53
|
+
return false;
|
|
54
|
+
return matchGlob(jobId, rule.job);
|
|
55
|
+
}
|
|
56
|
+
// HTTP method + path matcher
|
|
57
|
+
if (rule.path !== undefined) {
|
|
58
|
+
if (!operation.path)
|
|
59
|
+
return false;
|
|
60
|
+
if (!matchGlob(operation.path, rule.path))
|
|
61
|
+
return false;
|
|
62
|
+
// If method is specified, it must also match
|
|
63
|
+
if (rule.method !== undefined) {
|
|
64
|
+
if (!operation.method)
|
|
65
|
+
return false;
|
|
66
|
+
return matchGlob(operation.method.toUpperCase(), rule.method.toUpperCase());
|
|
67
|
+
}
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
// Method-only matcher (no path)
|
|
71
|
+
if (rule.method !== undefined) {
|
|
72
|
+
if (!operation.method)
|
|
73
|
+
return false;
|
|
74
|
+
return matchGlob(operation.method.toUpperCase(), rule.method.toUpperCase());
|
|
75
|
+
}
|
|
76
|
+
// Rule has no matchers — does not match anything
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* SafetyGuard evaluates operations against safety rules and levels.
|
|
81
|
+
*
|
|
82
|
+
* The guard provides three levels of API:
|
|
83
|
+
* - {@link evaluate} — returns a {@link SafetyEvaluation} describing what should happen
|
|
84
|
+
* - {@link assert} — throws {@link SafetyBlockedError} or {@link SafetyConfirmationRequired}
|
|
85
|
+
* - {@link temporarilyAllow} — creates a scoped exemption for confirmed operations
|
|
86
|
+
*
|
|
87
|
+
* The HTTP middleware uses the guard internally so all HTTP requests are
|
|
88
|
+
* evaluated automatically. CLI commands and other consumers can use the
|
|
89
|
+
* guard directly for richer safety interaction (command-level checks,
|
|
90
|
+
* confirmation flows).
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```typescript
|
|
94
|
+
* const guard = new SafetyGuard({
|
|
95
|
+
* level: 'NO_UPDATE',
|
|
96
|
+
* confirm: true,
|
|
97
|
+
* rules: [{ job: 'sfcc-site-archive-export', action: 'allow' }],
|
|
98
|
+
* });
|
|
99
|
+
*
|
|
100
|
+
* const evaluation = guard.evaluate({ type: 'job', jobId: 'sfcc-site-archive-export' });
|
|
101
|
+
* // evaluation.action === 'allow'
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
export class SafetyGuard {
|
|
105
|
+
config;
|
|
106
|
+
temporaryAllows = [];
|
|
107
|
+
logger;
|
|
108
|
+
constructor(config) {
|
|
109
|
+
this.config = config;
|
|
110
|
+
this.logger = getLogger();
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Evaluate an operation against safety rules and level.
|
|
114
|
+
*
|
|
115
|
+
* Evaluation order:
|
|
116
|
+
* 1. Temporary allows (from confirmed retries) — if matched, allow
|
|
117
|
+
* 2. Config rules in order — first matching rule's action wins
|
|
118
|
+
* 3. Level-based default — confirm if `confirm: true`, otherwise block/allow
|
|
119
|
+
*
|
|
120
|
+
* All evaluations are trace-logged for diagnostics.
|
|
121
|
+
*/
|
|
122
|
+
evaluate(operation) {
|
|
123
|
+
// 1. Check temporary allows (confirmed operations)
|
|
124
|
+
for (const rule of this.temporaryAllows) {
|
|
125
|
+
if (ruleMatchesOperation(rule, operation)) {
|
|
126
|
+
const evaluation = {
|
|
127
|
+
action: 'allow',
|
|
128
|
+
reason: 'Temporarily allowed after confirmation',
|
|
129
|
+
operation,
|
|
130
|
+
rule,
|
|
131
|
+
};
|
|
132
|
+
this.logger.trace({ operation, evaluation }, '[SafetyGuard] Allowed by temporary exemption');
|
|
133
|
+
return evaluation;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
// 2. Check config rules (first match wins)
|
|
137
|
+
if (this.config.rules) {
|
|
138
|
+
for (const rule of this.config.rules) {
|
|
139
|
+
if (ruleMatchesOperation(rule, operation)) {
|
|
140
|
+
const evaluation = {
|
|
141
|
+
action: rule.action,
|
|
142
|
+
reason: this.describeRuleMatch(rule, operation),
|
|
143
|
+
operation,
|
|
144
|
+
rule,
|
|
145
|
+
};
|
|
146
|
+
this.logger.trace({ operation, rule, action: rule.action }, '[SafetyGuard] Matched rule');
|
|
147
|
+
return evaluation;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
// 3. Fall back to level-based evaluation
|
|
152
|
+
return this.evaluateByLevel(operation);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Assert that an operation is allowed.
|
|
156
|
+
*
|
|
157
|
+
* @throws {SafetyBlockedError} if the operation is blocked
|
|
158
|
+
* @throws {SafetyConfirmationRequired} if the operation needs confirmation
|
|
159
|
+
*/
|
|
160
|
+
assert(operation) {
|
|
161
|
+
const evaluation = this.evaluate(operation);
|
|
162
|
+
switch (evaluation.action) {
|
|
163
|
+
case 'allow':
|
|
164
|
+
return;
|
|
165
|
+
case 'block':
|
|
166
|
+
throw new SafetyBlockedError(evaluation.reason, operation.method ?? '', operation.url ?? '', this.config.level);
|
|
167
|
+
case 'confirm':
|
|
168
|
+
throw new SafetyConfirmationRequired(evaluation);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Create a temporary exemption for a confirmed operation.
|
|
173
|
+
*
|
|
174
|
+
* Returns a cleanup function that removes the exemption. Use this
|
|
175
|
+
* to retry an operation after the user has confirmed.
|
|
176
|
+
*/
|
|
177
|
+
temporarilyAllow(operation) {
|
|
178
|
+
const rule = this.operationToRule(operation);
|
|
179
|
+
return this.temporarilyAddRule(rule);
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Add a temporary safety rule for a scoped exemption.
|
|
183
|
+
*
|
|
184
|
+
* Unlike {@link temporarilyAllow} which derives a rule from an operation,
|
|
185
|
+
* this accepts an arbitrary rule — useful for granting broad temporary
|
|
186
|
+
* access (e.g., allowing WebDAV DELETE on Impex paths during a job export).
|
|
187
|
+
*
|
|
188
|
+
* Returns a cleanup function that removes the rule.
|
|
189
|
+
*/
|
|
190
|
+
temporarilyAddRule(rule) {
|
|
191
|
+
this.temporaryAllows.push(rule);
|
|
192
|
+
this.logger.trace({ rule }, '[SafetyGuard] Added temporary rule');
|
|
193
|
+
return () => {
|
|
194
|
+
const idx = this.temporaryAllows.indexOf(rule);
|
|
195
|
+
if (idx >= 0) {
|
|
196
|
+
this.temporaryAllows.splice(idx, 1);
|
|
197
|
+
this.logger.trace({ rule }, '[SafetyGuard] Removed temporary rule');
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Evaluate an operation using only the safety level (no rules).
|
|
203
|
+
*/
|
|
204
|
+
evaluateByLevel(operation) {
|
|
205
|
+
// For HTTP operations, check the level
|
|
206
|
+
if (operation.method && operation.path) {
|
|
207
|
+
const violation = checkLevelViolation(operation.method, operation.path, this.config.level);
|
|
208
|
+
if (violation) {
|
|
209
|
+
const action = this.config.confirm ? 'confirm' : 'block';
|
|
210
|
+
const evaluation = {
|
|
211
|
+
action,
|
|
212
|
+
reason: this.describeLevelBlock(operation),
|
|
213
|
+
operation,
|
|
214
|
+
};
|
|
215
|
+
this.logger.trace({ operation, action, level: this.config.level }, '[SafetyGuard] Level evaluation');
|
|
216
|
+
return evaluation;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
// For command operations, no level-based blocking (levels are HTTP-level)
|
|
220
|
+
// Commands opt into safety via rules or assertDestructiveOperationAllowed()
|
|
221
|
+
const evaluation = {
|
|
222
|
+
action: 'allow',
|
|
223
|
+
reason: 'No matching rule and level allows this operation',
|
|
224
|
+
operation,
|
|
225
|
+
};
|
|
226
|
+
this.logger.trace({ operation, level: this.config.level }, '[SafetyGuard] Allowed by level');
|
|
227
|
+
return evaluation;
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Convert an operation to a temporary allow rule for retry.
|
|
231
|
+
*/
|
|
232
|
+
operationToRule(operation) {
|
|
233
|
+
if (operation.commandId) {
|
|
234
|
+
return { command: operation.commandId, action: 'allow' };
|
|
235
|
+
}
|
|
236
|
+
if (operation.jobId) {
|
|
237
|
+
return { job: operation.jobId, action: 'allow' };
|
|
238
|
+
}
|
|
239
|
+
// HTTP operation — match exact method + path
|
|
240
|
+
return {
|
|
241
|
+
method: operation.method,
|
|
242
|
+
path: operation.path,
|
|
243
|
+
action: 'allow',
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Describe why a rule matched, for user-facing messages.
|
|
248
|
+
*/
|
|
249
|
+
describeRuleMatch(rule, operation) {
|
|
250
|
+
if (rule.command) {
|
|
251
|
+
return `Command "${operation.commandId}" matched safety rule (command: "${rule.command}", action: ${rule.action})`;
|
|
252
|
+
}
|
|
253
|
+
if (rule.job) {
|
|
254
|
+
return `Job "${operation.jobId}" matched safety rule (job: "${rule.job}", action: ${rule.action})`;
|
|
255
|
+
}
|
|
256
|
+
const method = operation.method ?? 'unknown';
|
|
257
|
+
const path = operation.path ?? 'unknown';
|
|
258
|
+
return `${method} ${path} matched safety rule (action: ${rule.action})`;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Describe why the level blocked an operation, for user-facing messages.
|
|
262
|
+
*/
|
|
263
|
+
describeLevelBlock(operation) {
|
|
264
|
+
const method = operation.method ?? 'unknown';
|
|
265
|
+
const path = operation.path ?? 'unknown';
|
|
266
|
+
const levelDesc = describeSafetyLevel(this.config.level);
|
|
267
|
+
return `${method} ${path} blocked by safety level ${this.config.level} — ${levelDesc}`;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
//# sourceMappingURL=safety-guard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"safety-guard.js","sourceRoot":"","sources":["../../../src/safety/safety-guard.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;;;;;GAQG;AAEH,OAAO,EAAC,SAAS,EAAC,MAAM,WAAW,CAAC;AACpC,OAAO,EAAC,SAAS,EAAc,MAAM,qBAAqB,CAAC;AAE3D,OAAO,EACL,kBAAkB,EAClB,0BAA0B,EAC1B,mBAAmB,EACnB,mBAAmB,GACpB,MAAM,wBAAwB,CAAC;AAGhC,6DAA6D;AAC7D,MAAM,qBAAqB,GAAG,6BAA6B,CAAC;AAE5D;;;;;;GAMG;AACH,MAAM,UAAU,oBAAoB,CAAC,IAAY;IAC/C,MAAM,KAAK,GAAG,qBAAqB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC/C,OAAO,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC;AACpB,CAAC;AAED;;;GAGG;AACH,SAAS,SAAS,CAAC,KAAa,EAAE,OAAe;IAC/C,MAA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G,CAAC;IAC1E,CAAC;IAED;;OAEG;IACK,kBAAkB,CAAC,SAA0B;QACnD,MAAM,MAAM,GAAG,SAAS,CAAC,MAAM,IAAI,SAAS,CAAC;QAC7C,MAAM,IAAI,GAAG,SAAS,CAAC,IAAI,IAAI,SAAS,CAAC;QACzC,MAAM,SAAS,GAAG,mBAAmB,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACzD,OAAO,GAAG,MAAM,IAAI,IAAI,4BAA4B,IAAI,CAAC,MAAM,CAAC,KAAK,MAAM,SAAS,EAAE,CAAC;IACzF,CAAC;CACF"}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { SafetyRule } from './types.js';
|
|
2
|
+
import type { SafetyEvaluation } from './types.js';
|
|
1
3
|
/**
|
|
2
4
|
* Safety levels for preventing destructive operations.
|
|
3
5
|
*
|
|
@@ -7,11 +9,34 @@
|
|
|
7
9
|
* - READ_ONLY: Block all write operations (only GET allowed)
|
|
8
10
|
*/
|
|
9
11
|
export type SafetyLevel = 'NONE' | 'NO_DELETE' | 'NO_UPDATE' | 'READ_ONLY';
|
|
12
|
+
/**
|
|
13
|
+
* Safety configuration.
|
|
14
|
+
*
|
|
15
|
+
* Supports both simple level-based blocking and granular per-rule actions.
|
|
16
|
+
*/
|
|
10
17
|
export interface SafetyConfig {
|
|
18
|
+
/** The base safety level. */
|
|
11
19
|
level: SafetyLevel;
|
|
20
|
+
/** When true, operations that the level would block require confirmation instead of hard-blocking. */
|
|
21
|
+
confirm?: boolean;
|
|
22
|
+
/** Ordered list of rules. First matching rule wins. */
|
|
23
|
+
rules?: SafetyRule[];
|
|
12
24
|
}
|
|
13
25
|
/**
|
|
14
|
-
*
|
|
26
|
+
* Returns the more restrictive of two safety levels.
|
|
27
|
+
*/
|
|
28
|
+
export declare function maxSafetyLevel(a: SafetyLevel, b: SafetyLevel): SafetyLevel;
|
|
29
|
+
/**
|
|
30
|
+
* Check if a string is a valid SafetyLevel.
|
|
31
|
+
*/
|
|
32
|
+
export declare function isValidSafetyLevel(value: string): value is SafetyLevel;
|
|
33
|
+
/**
|
|
34
|
+
* Parse a string to a SafetyLevel, returning undefined for invalid values.
|
|
35
|
+
* Accepts case-insensitive input and converts dashes to underscores.
|
|
36
|
+
*/
|
|
37
|
+
export declare function parseSafetyLevelString(value: string | undefined): SafetyLevel | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* Safety error thrown when an operation is blocked by safety configuration.
|
|
15
40
|
*/
|
|
16
41
|
export declare class SafetyBlockedError extends Error {
|
|
17
42
|
readonly method: string;
|
|
@@ -19,6 +44,28 @@ export declare class SafetyBlockedError extends Error {
|
|
|
19
44
|
readonly safetyLevel: SafetyLevel;
|
|
20
45
|
constructor(message: string, method: string, url: string, safetyLevel: SafetyLevel);
|
|
21
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* Error thrown when an operation requires interactive confirmation.
|
|
49
|
+
*
|
|
50
|
+
* Callers can catch this error, prompt the user, and retry the operation
|
|
51
|
+
* using {@link withSafetyConfirmation}.
|
|
52
|
+
*/
|
|
53
|
+
export declare class SafetyConfirmationRequired extends Error {
|
|
54
|
+
readonly evaluation: SafetyEvaluation;
|
|
55
|
+
constructor(evaluation: SafetyEvaluation);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Checks if an HTTP operation should be blocked based on a safety level.
|
|
59
|
+
*
|
|
60
|
+
* This is the low-level level check. For full rule-based evaluation,
|
|
61
|
+
* use {@link SafetyGuard.evaluate}.
|
|
62
|
+
*
|
|
63
|
+
* @param method - HTTP method (GET, POST, PUT, PATCH, DELETE)
|
|
64
|
+
* @param path - URL pathname
|
|
65
|
+
* @param level - Safety level to check against
|
|
66
|
+
* @returns Error message if blocked, undefined if allowed
|
|
67
|
+
*/
|
|
68
|
+
export declare function checkLevelViolation(method: string, path: string, level: SafetyLevel): string | undefined;
|
|
22
69
|
/**
|
|
23
70
|
* Checks if an HTTP operation should be blocked based on safety configuration.
|
|
24
71
|
*
|
|
@@ -26,6 +73,7 @@ export declare class SafetyBlockedError extends Error {
|
|
|
26
73
|
* @param url - Request URL
|
|
27
74
|
* @param config - Safety configuration
|
|
28
75
|
* @returns Error message if blocked, undefined if allowed
|
|
76
|
+
* @deprecated Use {@link SafetyGuard.evaluate} for full rule-based evaluation.
|
|
29
77
|
*/
|
|
30
78
|
export declare function checkSafetyViolation(method: string, url: string, config: SafetyConfig): string | undefined;
|
|
31
79
|
/**
|
|
@@ -43,3 +91,40 @@ export declare function getSafetyLevel(defaultLevel?: SafetyLevel): SafetyLevel;
|
|
|
43
91
|
* Get a user-friendly description of the safety level.
|
|
44
92
|
*/
|
|
45
93
|
export declare function describeSafetyLevel(level: SafetyLevel): string;
|
|
94
|
+
/** Validated safety config fragment (shared by global and per-instance). */
|
|
95
|
+
export interface SafetyConfigFragment {
|
|
96
|
+
level?: SafetyLevel;
|
|
97
|
+
confirm?: boolean;
|
|
98
|
+
rules?: SafetyRule[];
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Load global safety configuration from a JSON file.
|
|
102
|
+
*
|
|
103
|
+
* Resolution order:
|
|
104
|
+
* 1. `SFCC_SAFETY_CONFIG` env var — explicit path to a safety config file
|
|
105
|
+
* 2. `{configDir}/safety.json` — oclif config directory (e.g., `~/.config/b2c/safety.json`)
|
|
106
|
+
*
|
|
107
|
+
* The file has the same shape as the `safety` object in dw.json:
|
|
108
|
+
* ```json
|
|
109
|
+
* { "level": "NO_DELETE", "confirm": true, "rules": [...] }
|
|
110
|
+
* ```
|
|
111
|
+
*
|
|
112
|
+
* @param configDir - oclif config directory path (e.g., `this.config.configDir`)
|
|
113
|
+
* @returns Validated safety config fragment, or undefined if no file found
|
|
114
|
+
*/
|
|
115
|
+
export declare function loadGlobalSafetyConfig(configDir?: string): SafetyConfigFragment | undefined;
|
|
116
|
+
/**
|
|
117
|
+
* Compute effective safety config by merging environment variables, global
|
|
118
|
+
* safety config, and per-instance config.
|
|
119
|
+
*
|
|
120
|
+
* Merge strategy:
|
|
121
|
+
* - **Level**: `max(env, global, instance)` — most restrictive wins
|
|
122
|
+
* - **Confirm**: OR across all sources
|
|
123
|
+
* - **Rules**: instance rules first, then global rules (first-match-wins,
|
|
124
|
+
* so instance rules can override global policy)
|
|
125
|
+
*
|
|
126
|
+
* @param instanceSafety - Per-instance safety config from dw.json
|
|
127
|
+
* @param globalSafety - Global safety config from safety.json
|
|
128
|
+
* @returns Merged SafetyConfig
|
|
129
|
+
*/
|
|
130
|
+
export declare function resolveEffectiveSafetyConfig(instanceSafety?: SafetyConfigFragment, globalSafety?: SafetyConfigFragment): SafetyConfig;
|