@onlyflows/servicenow-mcp 1.0.0 → 2.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/.env.example +56 -0
- package/CHANGELOG.md +153 -0
- package/README.md +939 -101
- package/dist/auth.d.ts +88 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +396 -0
- package/dist/auth.js.map +1 -0
- package/dist/client.d.ts +111 -17
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +554 -79
- package/dist/client.js.map +1 -1
- package/dist/config.d.ts +41 -4
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +44 -34
- package/dist/config.js.map +1 -1
- package/dist/encoded-query-policy.d.ts +72 -0
- package/dist/encoded-query-policy.d.ts.map +1 -0
- package/dist/encoded-query-policy.js +409 -0
- package/dist/encoded-query-policy.js.map +1 -0
- package/dist/execution-context.d.ts +235 -0
- package/dist/execution-context.d.ts.map +1 -0
- package/dist/execution-context.js +439 -0
- package/dist/execution-context.js.map +1 -0
- package/dist/field-policy.d.ts +117 -0
- package/dist/field-policy.d.ts.map +1 -0
- package/dist/field-policy.js +1684 -0
- package/dist/field-policy.js.map +1 -0
- package/dist/http-auth.d.ts +131 -0
- package/dist/http-auth.d.ts.map +1 -0
- package/dist/http-auth.js +347 -0
- package/dist/http-auth.js.map +1 -0
- package/dist/http-entrypoint.d.ts +26 -0
- package/dist/http-entrypoint.d.ts.map +1 -0
- package/dist/http-entrypoint.js +255 -0
- package/dist/http-entrypoint.js.map +1 -0
- package/dist/http-observability.d.ts +147 -0
- package/dist/http-observability.d.ts.map +1 -0
- package/dist/http-observability.js +703 -0
- package/dist/http-observability.js.map +1 -0
- package/dist/http-request-policy.d.ts +72 -0
- package/dist/http-request-policy.d.ts.map +1 -0
- package/dist/http-request-policy.js +463 -0
- package/dist/http-request-policy.js.map +1 -0
- package/dist/http-request-signal.d.ts +12 -0
- package/dist/http-request-signal.d.ts.map +1 -0
- package/dist/http-request-signal.js +39 -0
- package/dist/http-request-signal.js.map +1 -0
- package/dist/http-runtime.d.ts +77 -0
- package/dist/http-runtime.d.ts.map +1 -0
- package/dist/http-runtime.js +1321 -0
- package/dist/http-runtime.js.map +1 -0
- package/dist/incident-journal-policy.d.ts +22 -0
- package/dist/incident-journal-policy.d.ts.map +1 -0
- package/dist/incident-journal-policy.js +115 -0
- package/dist/incident-journal-policy.js.map +1 -0
- package/dist/incident-write-policy.d.ts +20 -0
- package/dist/incident-write-policy.d.ts.map +1 -0
- package/dist/incident-write-policy.js +196 -0
- package/dist/incident-write-policy.js.map +1 -0
- package/dist/index.d.ts +14 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +140 -36
- package/dist/index.js.map +1 -1
- package/dist/metadata-cache.d.ts +71 -0
- package/dist/metadata-cache.d.ts.map +1 -0
- package/dist/metadata-cache.js +397 -0
- package/dist/metadata-cache.js.map +1 -0
- package/dist/profile-admin.d.ts +64 -0
- package/dist/profile-admin.d.ts.map +1 -0
- package/dist/profile-admin.js +351 -0
- package/dist/profile-admin.js.map +1 -0
- package/dist/profile-credentials.d.ts +68 -0
- package/dist/profile-credentials.d.ts.map +1 -0
- package/dist/profile-credentials.js +329 -0
- package/dist/profile-credentials.js.map +1 -0
- package/dist/profile-manager.d.ts +187 -0
- package/dist/profile-manager.d.ts.map +1 -0
- package/dist/profile-manager.js +1026 -0
- package/dist/profile-manager.js.map +1 -0
- package/dist/record-identifier-policy.d.ts +39 -0
- package/dist/record-identifier-policy.d.ts.map +1 -0
- package/dist/record-identifier-policy.js +203 -0
- package/dist/record-identifier-policy.js.map +1 -0
- package/dist/runtime-environment.d.ts +23 -0
- package/dist/runtime-environment.d.ts.map +1 -0
- package/dist/runtime-environment.js +58 -0
- package/dist/runtime-environment.js.map +1 -0
- package/dist/server.d.ts +33 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +73 -0
- package/dist/server.js.map +1 -0
- package/dist/servicenow-identifiers.d.ts +8 -0
- package/dist/servicenow-identifiers.d.ts.map +1 -0
- package/dist/servicenow-identifiers.js +22 -0
- package/dist/servicenow-identifiers.js.map +1 -0
- package/dist/setup-prompts.d.ts +7 -0
- package/dist/setup-prompts.d.ts.map +1 -0
- package/dist/setup-prompts.js +123 -0
- package/dist/setup-prompts.js.map +1 -0
- package/dist/setup.d.ts +109 -0
- package/dist/setup.d.ts.map +1 -0
- package/dist/setup.js +1903 -0
- package/dist/setup.js.map +1 -0
- package/dist/startup.d.ts +43 -0
- package/dist/startup.d.ts.map +1 -0
- package/dist/startup.js +75 -0
- package/dist/startup.js.map +1 -0
- package/dist/stdio-runtime.d.ts +106 -0
- package/dist/stdio-runtime.d.ts.map +1 -0
- package/dist/stdio-runtime.js +231 -0
- package/dist/stdio-runtime.js.map +1 -0
- package/dist/structured-query.d.ts +187 -0
- package/dist/structured-query.d.ts.map +1 -0
- package/dist/structured-query.js +503 -0
- package/dist/structured-query.js.map +1 -0
- package/dist/table-defaults.d.ts +28 -0
- package/dist/table-defaults.d.ts.map +1 -0
- package/dist/table-defaults.js +85 -0
- package/dist/table-defaults.js.map +1 -0
- package/dist/table-policy.d.ts +71 -0
- package/dist/table-policy.d.ts.map +1 -0
- package/dist/table-policy.js +290 -0
- package/dist/table-policy.js.map +1 -0
- package/dist/tool-error.d.ts +48 -0
- package/dist/tool-error.d.ts.map +1 -0
- package/dist/tool-error.js +139 -0
- package/dist/tool-error.js.map +1 -0
- package/dist/tool-table-access.d.ts +19 -0
- package/dist/tool-table-access.d.ts.map +1 -0
- package/dist/tool-table-access.js +388 -0
- package/dist/tool-table-access.js.map +1 -0
- package/dist/tools/aggregate.d.ts +22 -42
- package/dist/tools/aggregate.d.ts.map +1 -1
- package/dist/tools/aggregate.js +103 -47
- package/dist/tools/aggregate.js.map +1 -1
- package/dist/tools/all-fields-cap.d.ts +45 -0
- package/dist/tools/all-fields-cap.d.ts.map +1 -0
- package/dist/tools/all-fields-cap.js +151 -0
- package/dist/tools/all-fields-cap.js.map +1 -0
- package/dist/tools/atf.d.ts +21 -60
- package/dist/tools/atf.d.ts.map +1 -1
- package/dist/tools/atf.js +180 -116
- package/dist/tools/atf.js.map +1 -1
- package/dist/tools/attach.d.ts +31 -45
- package/dist/tools/attach.d.ts.map +1 -1
- package/dist/tools/attach.js +175 -137
- package/dist/tools/attach.js.map +1 -1
- package/dist/tools/batch.d.ts +78 -42
- package/dist/tools/batch.d.ts.map +1 -1
- package/dist/tools/batch.js +172 -68
- package/dist/tools/batch.js.map +1 -1
- package/dist/tools/catalog.d.ts +6 -0
- package/dist/tools/catalog.d.ts.map +1 -0
- package/dist/tools/catalog.js +203 -0
- package/dist/tools/catalog.js.map +1 -0
- package/dist/tools/codesearch.d.ts +14 -25
- package/dist/tools/codesearch.d.ts.map +1 -1
- package/dist/tools/codesearch.js +60 -48
- package/dist/tools/codesearch.js.map +1 -1
- package/dist/tools/create-module.d.ts +48 -0
- package/dist/tools/create-module.d.ts.map +1 -0
- package/dist/tools/create-module.js +94 -0
- package/dist/tools/create-module.js.map +1 -0
- package/dist/tools/create.d.ts +2 -38
- package/dist/tools/create.d.ts.map +1 -1
- package/dist/tools/create.js +9 -41
- package/dist/tools/create.js.map +1 -1
- package/dist/tools/delete.d.ts +13 -23
- package/dist/tools/delete.d.ts.map +1 -1
- package/dist/tools/delete.js +30 -33
- package/dist/tools/delete.js.map +1 -1
- package/dist/tools/discover.d.ts +15 -27
- package/dist/tools/discover.d.ts.map +1 -1
- package/dist/tools/discover.js +103 -60
- package/dist/tools/discover.js.map +1 -1
- package/dist/tools/get-module.d.ts +85 -0
- package/dist/tools/get-module.d.ts.map +1 -0
- package/dist/tools/get-module.js +221 -0
- package/dist/tools/get-module.js.map +1 -0
- package/dist/tools/get.d.ts +2 -51
- package/dist/tools/get.d.ts.map +1 -1
- package/dist/tools/get.js +9 -49
- package/dist/tools/get.js.map +1 -1
- package/dist/tools/handler-result.d.ts +14 -0
- package/dist/tools/handler-result.d.ts.map +1 -0
- package/dist/tools/handler-result.js +177 -0
- package/dist/tools/handler-result.js.map +1 -0
- package/dist/tools/health.d.ts +12 -15
- package/dist/tools/health.d.ts.map +1 -1
- package/dist/tools/health.js +55 -36
- package/dist/tools/health.js.map +1 -1
- package/dist/tools/incident-journal-module.d.ts +12 -0
- package/dist/tools/incident-journal-module.d.ts.map +1 -0
- package/dist/tools/incident-journal-module.js +61 -0
- package/dist/tools/incident-journal-module.js.map +1 -0
- package/dist/tools/incident-write-shared.d.ts +6 -0
- package/dist/tools/incident-write-shared.d.ts.map +1 -0
- package/dist/tools/incident-write-shared.js +64 -0
- package/dist/tools/incident-write-shared.js.map +1 -0
- package/dist/tools/index.d.ts +47 -36
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +795 -35
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/nl.d.ts +10 -24
- package/dist/tools/nl.d.ts.map +1 -1
- package/dist/tools/nl.js +37 -147
- package/dist/tools/nl.js.map +1 -1
- package/dist/tools/profile.d.ts +37 -0
- package/dist/tools/profile.d.ts.map +1 -0
- package/dist/tools/profile.js +39 -0
- package/dist/tools/profile.js.map +1 -0
- package/dist/tools/query-module.d.ts +119 -0
- package/dist/tools/query-module.d.ts.map +1 -0
- package/dist/tools/query-module.js +324 -0
- package/dist/tools/query-module.js.map +1 -0
- package/dist/tools/query.d.ts +2 -72
- package/dist/tools/query.d.ts.map +1 -1
- package/dist/tools/query.js +8 -72
- package/dist/tools/query.js.map +1 -1
- package/dist/tools/relationships.d.ts +21 -42
- package/dist/tools/relationships.d.ts.map +1 -1
- package/dist/tools/relationships.js +70 -56
- package/dist/tools/relationships.js.map +1 -1
- package/dist/tools/result-envelope.d.ts +603 -0
- package/dist/tools/result-envelope.d.ts.map +1 -0
- package/dist/tools/result-envelope.js +767 -0
- package/dist/tools/result-envelope.js.map +1 -0
- package/dist/tools/schema-module.d.ts +64 -0
- package/dist/tools/schema-module.d.ts.map +1 -0
- package/dist/tools/schema-module.js +367 -0
- package/dist/tools/schema-module.js.map +1 -0
- package/dist/tools/schema.d.ts +2 -37
- package/dist/tools/schema.d.ts.map +1 -1
- package/dist/tools/schema.js +6 -57
- package/dist/tools/schema.js.map +1 -1
- package/dist/tools/script.d.ts +33 -27
- package/dist/tools/script.d.ts.map +1 -1
- package/dist/tools/script.js +43 -37
- package/dist/tools/script.js.map +1 -1
- package/dist/tools/syslog.d.ts +19 -46
- package/dist/tools/syslog.d.ts.map +1 -1
- package/dist/tools/syslog.js +101 -67
- package/dist/tools/syslog.js.map +1 -1
- package/dist/tools/tool-module.d.ts +143 -0
- package/dist/tools/tool-module.d.ts.map +1 -0
- package/dist/tools/tool-module.js +1095 -0
- package/dist/tools/tool-module.js.map +1 -0
- package/dist/tools/update-module.d.ts +51 -0
- package/dist/tools/update-module.d.ts.map +1 -0
- package/dist/tools/update-module.js +107 -0
- package/dist/tools/update-module.js.map +1 -0
- package/dist/tools/update.d.ts +2 -45
- package/dist/tools/update.d.ts.map +1 -1
- package/dist/tools/update.js +9 -41
- package/dist/tools/update.js.map +1 -1
- package/dist/utils.d.ts +68 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +114 -16
- package/dist/utils.js.map +1 -1
- package/dist/version.d.ts +10 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +13 -0
- package/dist/version.js.map +1 -0
- package/docs/ADDING-TOOLS.md +227 -0
- package/docs/CLIENT-SETUP.md +914 -0
- package/docs/CONTAINER-DEPLOYMENT.md +310 -0
- package/docs/CROSS-CLIENT-RELEASE-MATRIX.md +63 -0
- package/docs/ENTERPRISE-RELEASE-BOUNDARY.md +149 -0
- package/docs/OPERATIONS-RUNBOOK.md +383 -0
- package/docs/PRIVATE-CHATGPT-CONNECTIVITY.md +347 -0
- package/docs/PRODUCTION-SECURITY.md +368 -0
- package/docs/PROFILE-CREDENTIALS.md +126 -0
- package/docs/RELEASE-VALIDATION.md +140 -0
- package/docs/TESTING-OAUTH.md +101 -0
- package/docs/V2-MIGRATION.md +493 -0
- package/package.json +60 -13
- package/scripts/inspector.mjs +140 -0
- package/scripts/release-validate.mjs +201 -0
- package/scripts/smoke-test.mjs +274 -0
- package/tools/inspector/package-lock.json +3812 -0
- package/tools/inspector/package.json +12 -0
|
@@ -0,0 +1,1684 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Fail-closed ServiceNow field authorization and response filtering.
|
|
4
|
+
*
|
|
5
|
+
* Table authorization answers whether a tool may reach a table. This module
|
|
6
|
+
* supplies the second, independent boundary: which fields may be requested,
|
|
7
|
+
* written, or returned for the supported V2 table domains. Built-in defaults
|
|
8
|
+
* can be extended or overridden with SN_FIELD_POLICY_DEFINITIONS, including a
|
|
9
|
+
* generic "*" table fallback for operator-approved custom-table access.
|
|
10
|
+
*
|
|
11
|
+
* @module field-policy
|
|
12
|
+
*/
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.FieldPolicyError = exports.SAFE_DEFAULT_FIELDS = exports.MAX_TOOL_ARGUMENT_OWN_KEYS = exports.MAX_FIELD_OBJECT_OWN_KEYS = exports.MAX_FIELD_ARRAY_LENGTH = exports.MAX_FIELD_VALUE_NODES = exports.MAX_FIELD_VALUE_DEPTH = exports.MAX_FIELDS_PER_OPERATION = void 0;
|
|
15
|
+
exports.fieldPolicyDenialMessage = fieldPolicyDenialMessage;
|
|
16
|
+
exports.isFieldPolicyError = isFieldPolicyError;
|
|
17
|
+
exports.snapshotPlainDataArguments = snapshotPlainDataArguments;
|
|
18
|
+
exports.withFieldPolicyArgumentValues = withFieldPolicyArgumentValues;
|
|
19
|
+
exports.resolveReadableFields = resolveReadableFields;
|
|
20
|
+
exports.validateWritableFields = validateWritableFields;
|
|
21
|
+
exports.prepareReadFieldArguments = prepareReadFieldArguments;
|
|
22
|
+
exports.prepareAdditionalReadFieldArguments = prepareAdditionalReadFieldArguments;
|
|
23
|
+
exports.prepareWriteFieldArguments = prepareWriteFieldArguments;
|
|
24
|
+
exports.preparedReadableFields = preparedReadableFields;
|
|
25
|
+
exports.filterReadableRecord = filterReadableRecord;
|
|
26
|
+
exports.filterSchemaEntries = filterSchemaEntries;
|
|
27
|
+
exports.filterAggregateResult = filterAggregateResult;
|
|
28
|
+
exports.runWithFieldPolicyConfiguration = runWithFieldPolicyConfiguration;
|
|
29
|
+
exports.isAllFieldSelection = isAllFieldSelection;
|
|
30
|
+
exports.fieldSelectionToSysparmFields = fieldSelectionToSysparmFields;
|
|
31
|
+
exports.isSensitiveFieldName = isSensitiveFieldName;
|
|
32
|
+
const node_async_hooks_1 = require("node:async_hooks");
|
|
33
|
+
const node_util_1 = require("node:util");
|
|
34
|
+
const FIELD_NAME = /^[a-z][a-z0-9_]{0,79}$/u;
|
|
35
|
+
/** @deprecated Field authorization no longer caps the number of selected fields. */
|
|
36
|
+
exports.MAX_FIELDS_PER_OPERATION = 10_000;
|
|
37
|
+
/** Hard bounds for hostile values crossing the field-policy boundary. */
|
|
38
|
+
exports.MAX_FIELD_VALUE_DEPTH = 24;
|
|
39
|
+
exports.MAX_FIELD_VALUE_NODES = 50_000;
|
|
40
|
+
exports.MAX_FIELD_ARRAY_LENGTH = 10_000;
|
|
41
|
+
exports.MAX_FIELD_OBJECT_OWN_KEYS = 256;
|
|
42
|
+
exports.MAX_TOOL_ARGUMENT_OWN_KEYS = 64;
|
|
43
|
+
const MAX_CANONICAL_ARGUMENT_KEYS = 8;
|
|
44
|
+
const FIELD_POLICY_DEFINITIONS_ENV = "SN_FIELD_POLICY_DEFINITIONS";
|
|
45
|
+
const MAX_FIELD_POLICY_DEFINITIONS_TEXT_LENGTH = 65_536;
|
|
46
|
+
const FIELD_POLICY_WILDCARD = "*";
|
|
47
|
+
const ALL_FIELDS_SELECTION = Object.freeze([FIELD_POLICY_WILDCARD]);
|
|
48
|
+
/** Bounded projection for a table whose readable set is a wildcard but whose
|
|
49
|
+
* defaults the operator did not state. An unstated default must stay finite:
|
|
50
|
+
* "may read any field" is not "return every field on every query". */
|
|
51
|
+
const GENERIC_DEFAULT_FIELDS = Object.freeze(["sys_id"]);
|
|
52
|
+
const INCIDENT_DEFAULTS = [
|
|
53
|
+
"sys_id",
|
|
54
|
+
"number",
|
|
55
|
+
"short_description",
|
|
56
|
+
"state",
|
|
57
|
+
"priority",
|
|
58
|
+
"assigned_to",
|
|
59
|
+
"assignment_group",
|
|
60
|
+
"caller_id",
|
|
61
|
+
"opened_at",
|
|
62
|
+
"sys_updated_on",
|
|
63
|
+
"active",
|
|
64
|
+
];
|
|
65
|
+
const POLICY_DEFINITIONS = {
|
|
66
|
+
incident: {
|
|
67
|
+
defaults: INCIDENT_DEFAULTS,
|
|
68
|
+
readable: [
|
|
69
|
+
...INCIDENT_DEFAULTS,
|
|
70
|
+
"category",
|
|
71
|
+
"subcategory",
|
|
72
|
+
"description",
|
|
73
|
+
"impact",
|
|
74
|
+
"urgency",
|
|
75
|
+
"contact_type",
|
|
76
|
+
"opened_by",
|
|
77
|
+
"resolved_at",
|
|
78
|
+
"resolved_by",
|
|
79
|
+
"close_code",
|
|
80
|
+
"close_notes",
|
|
81
|
+
],
|
|
82
|
+
writable: [
|
|
83
|
+
"short_description",
|
|
84
|
+
"description",
|
|
85
|
+
"caller_id",
|
|
86
|
+
"assignment_group",
|
|
87
|
+
"assigned_to",
|
|
88
|
+
"impact",
|
|
89
|
+
"urgency",
|
|
90
|
+
"priority",
|
|
91
|
+
"category",
|
|
92
|
+
"subcategory",
|
|
93
|
+
"state",
|
|
94
|
+
"close_code",
|
|
95
|
+
"close_notes",
|
|
96
|
+
"contact_type",
|
|
97
|
+
],
|
|
98
|
+
},
|
|
99
|
+
change_request: {
|
|
100
|
+
defaults: [
|
|
101
|
+
"sys_id",
|
|
102
|
+
"number",
|
|
103
|
+
"short_description",
|
|
104
|
+
"state",
|
|
105
|
+
"priority",
|
|
106
|
+
"type",
|
|
107
|
+
"risk",
|
|
108
|
+
"assigned_to",
|
|
109
|
+
"assignment_group",
|
|
110
|
+
"start_date",
|
|
111
|
+
"end_date",
|
|
112
|
+
"sys_updated_on",
|
|
113
|
+
],
|
|
114
|
+
readable: [
|
|
115
|
+
"sys_id",
|
|
116
|
+
"number",
|
|
117
|
+
"short_description",
|
|
118
|
+
"description",
|
|
119
|
+
"state",
|
|
120
|
+
"priority",
|
|
121
|
+
"type",
|
|
122
|
+
"risk",
|
|
123
|
+
"impact",
|
|
124
|
+
"assigned_to",
|
|
125
|
+
"assignment_group",
|
|
126
|
+
"requested_by",
|
|
127
|
+
"start_date",
|
|
128
|
+
"end_date",
|
|
129
|
+
"sys_updated_on",
|
|
130
|
+
],
|
|
131
|
+
writable: [],
|
|
132
|
+
},
|
|
133
|
+
problem: {
|
|
134
|
+
defaults: [
|
|
135
|
+
"sys_id",
|
|
136
|
+
"number",
|
|
137
|
+
"short_description",
|
|
138
|
+
"state",
|
|
139
|
+
"priority",
|
|
140
|
+
"assigned_to",
|
|
141
|
+
"assignment_group",
|
|
142
|
+
"known_error",
|
|
143
|
+
"opened_at",
|
|
144
|
+
"sys_updated_on",
|
|
145
|
+
],
|
|
146
|
+
readable: [
|
|
147
|
+
"sys_id",
|
|
148
|
+
"number",
|
|
149
|
+
"short_description",
|
|
150
|
+
"description",
|
|
151
|
+
"state",
|
|
152
|
+
"priority",
|
|
153
|
+
"assigned_to",
|
|
154
|
+
"assignment_group",
|
|
155
|
+
"known_error",
|
|
156
|
+
"opened_at",
|
|
157
|
+
"resolved_at",
|
|
158
|
+
"sys_updated_on",
|
|
159
|
+
],
|
|
160
|
+
writable: [],
|
|
161
|
+
},
|
|
162
|
+
sc_request: {
|
|
163
|
+
defaults: [
|
|
164
|
+
"sys_id",
|
|
165
|
+
"number",
|
|
166
|
+
"short_description",
|
|
167
|
+
"state",
|
|
168
|
+
"request_state",
|
|
169
|
+
"priority",
|
|
170
|
+
"requested_for",
|
|
171
|
+
"approval",
|
|
172
|
+
"opened_at",
|
|
173
|
+
"sys_updated_on",
|
|
174
|
+
],
|
|
175
|
+
readable: [
|
|
176
|
+
"sys_id",
|
|
177
|
+
"number",
|
|
178
|
+
"short_description",
|
|
179
|
+
"state",
|
|
180
|
+
"request_state",
|
|
181
|
+
"priority",
|
|
182
|
+
"requested_for",
|
|
183
|
+
"requested_by",
|
|
184
|
+
"approval",
|
|
185
|
+
"opened_at",
|
|
186
|
+
"sys_updated_on",
|
|
187
|
+
],
|
|
188
|
+
writable: [],
|
|
189
|
+
},
|
|
190
|
+
sc_req_item: {
|
|
191
|
+
defaults: [
|
|
192
|
+
"sys_id",
|
|
193
|
+
"number",
|
|
194
|
+
"short_description",
|
|
195
|
+
"state",
|
|
196
|
+
"priority",
|
|
197
|
+
"cat_item",
|
|
198
|
+
"request",
|
|
199
|
+
"assigned_to",
|
|
200
|
+
"stage",
|
|
201
|
+
"opened_at",
|
|
202
|
+
"sys_updated_on",
|
|
203
|
+
],
|
|
204
|
+
readable: [
|
|
205
|
+
"sys_id",
|
|
206
|
+
"number",
|
|
207
|
+
"short_description",
|
|
208
|
+
"state",
|
|
209
|
+
"priority",
|
|
210
|
+
"cat_item",
|
|
211
|
+
"request",
|
|
212
|
+
"requested_for",
|
|
213
|
+
"assigned_to",
|
|
214
|
+
"assignment_group",
|
|
215
|
+
"stage",
|
|
216
|
+
"opened_at",
|
|
217
|
+
"sys_updated_on",
|
|
218
|
+
],
|
|
219
|
+
writable: [],
|
|
220
|
+
},
|
|
221
|
+
sc_task: {
|
|
222
|
+
defaults: [
|
|
223
|
+
"sys_id",
|
|
224
|
+
"number",
|
|
225
|
+
"short_description",
|
|
226
|
+
"state",
|
|
227
|
+
"priority",
|
|
228
|
+
"assigned_to",
|
|
229
|
+
"assignment_group",
|
|
230
|
+
"request_item",
|
|
231
|
+
"opened_at",
|
|
232
|
+
"sys_updated_on",
|
|
233
|
+
],
|
|
234
|
+
readable: [
|
|
235
|
+
"sys_id",
|
|
236
|
+
"number",
|
|
237
|
+
"short_description",
|
|
238
|
+
"state",
|
|
239
|
+
"priority",
|
|
240
|
+
"assigned_to",
|
|
241
|
+
"assignment_group",
|
|
242
|
+
"request_item",
|
|
243
|
+
"opened_at",
|
|
244
|
+
"sys_updated_on",
|
|
245
|
+
],
|
|
246
|
+
writable: [],
|
|
247
|
+
},
|
|
248
|
+
sys_user: {
|
|
249
|
+
defaults: [
|
|
250
|
+
"sys_id",
|
|
251
|
+
"user_name",
|
|
252
|
+
"name",
|
|
253
|
+
"email",
|
|
254
|
+
"title",
|
|
255
|
+
"department",
|
|
256
|
+
"manager",
|
|
257
|
+
"location",
|
|
258
|
+
"active",
|
|
259
|
+
"sys_updated_on",
|
|
260
|
+
],
|
|
261
|
+
readable: [
|
|
262
|
+
"sys_id",
|
|
263
|
+
"user_name",
|
|
264
|
+
"name",
|
|
265
|
+
"email",
|
|
266
|
+
"title",
|
|
267
|
+
"department",
|
|
268
|
+
"manager",
|
|
269
|
+
"location",
|
|
270
|
+
"active",
|
|
271
|
+
"sys_updated_on",
|
|
272
|
+
],
|
|
273
|
+
writable: [],
|
|
274
|
+
},
|
|
275
|
+
sys_user_group: {
|
|
276
|
+
defaults: [
|
|
277
|
+
"sys_id",
|
|
278
|
+
"name",
|
|
279
|
+
"description",
|
|
280
|
+
"manager",
|
|
281
|
+
"parent",
|
|
282
|
+
"email",
|
|
283
|
+
"active",
|
|
284
|
+
"sys_updated_on",
|
|
285
|
+
],
|
|
286
|
+
readable: [
|
|
287
|
+
"sys_id",
|
|
288
|
+
"name",
|
|
289
|
+
"description",
|
|
290
|
+
"manager",
|
|
291
|
+
"parent",
|
|
292
|
+
"email",
|
|
293
|
+
"active",
|
|
294
|
+
"sys_updated_on",
|
|
295
|
+
],
|
|
296
|
+
writable: [],
|
|
297
|
+
},
|
|
298
|
+
// Group membership grants every role the group holds. A write here escalates
|
|
299
|
+
// the integration account to instance admin as surely as a sys_script write,
|
|
300
|
+
// so the writable set is empty for the same reason. sys_user_role,
|
|
301
|
+
// sys_user_has_role, and sys_group_has_role are hard-denied at the table
|
|
302
|
+
// boundary (table-policy.ts); this table is not, so it is denied here.
|
|
303
|
+
sys_user_grmember: {
|
|
304
|
+
defaults: ["sys_id", "user", "group"],
|
|
305
|
+
readable: ["sys_id", "user", "group", "sys_created_on", "sys_updated_on"],
|
|
306
|
+
writable: [],
|
|
307
|
+
},
|
|
308
|
+
cmdb_ci: {
|
|
309
|
+
defaults: [
|
|
310
|
+
"sys_id",
|
|
311
|
+
"name",
|
|
312
|
+
"sys_class_name",
|
|
313
|
+
"operational_status",
|
|
314
|
+
"install_status",
|
|
315
|
+
"category",
|
|
316
|
+
"owned_by",
|
|
317
|
+
"support_group",
|
|
318
|
+
"sys_updated_on",
|
|
319
|
+
],
|
|
320
|
+
readable: [
|
|
321
|
+
"sys_id",
|
|
322
|
+
"name",
|
|
323
|
+
"sys_class_name",
|
|
324
|
+
"operational_status",
|
|
325
|
+
"install_status",
|
|
326
|
+
"category",
|
|
327
|
+
"subcategory",
|
|
328
|
+
"owned_by",
|
|
329
|
+
"managed_by",
|
|
330
|
+
"support_group",
|
|
331
|
+
"location",
|
|
332
|
+
"sys_updated_on",
|
|
333
|
+
],
|
|
334
|
+
writable: [],
|
|
335
|
+
},
|
|
336
|
+
cmdb_ci_server: {
|
|
337
|
+
defaults: [
|
|
338
|
+
"sys_id",
|
|
339
|
+
"name",
|
|
340
|
+
"host_name",
|
|
341
|
+
"ip_address",
|
|
342
|
+
"os",
|
|
343
|
+
"os_version",
|
|
344
|
+
"classification",
|
|
345
|
+
"operational_status",
|
|
346
|
+
"install_status",
|
|
347
|
+
"support_group",
|
|
348
|
+
"sys_updated_on",
|
|
349
|
+
],
|
|
350
|
+
readable: [
|
|
351
|
+
"sys_id",
|
|
352
|
+
"name",
|
|
353
|
+
"host_name",
|
|
354
|
+
"ip_address",
|
|
355
|
+
"os",
|
|
356
|
+
"os_version",
|
|
357
|
+
"classification",
|
|
358
|
+
"operational_status",
|
|
359
|
+
"install_status",
|
|
360
|
+
"owned_by",
|
|
361
|
+
"support_group",
|
|
362
|
+
"location",
|
|
363
|
+
"sys_updated_on",
|
|
364
|
+
],
|
|
365
|
+
writable: [],
|
|
366
|
+
},
|
|
367
|
+
cmdb_ci_computer: {
|
|
368
|
+
defaults: [
|
|
369
|
+
"sys_id",
|
|
370
|
+
"name",
|
|
371
|
+
"host_name",
|
|
372
|
+
"ip_address",
|
|
373
|
+
"os",
|
|
374
|
+
"manufacturer",
|
|
375
|
+
"model_id",
|
|
376
|
+
"operational_status",
|
|
377
|
+
"assigned_to",
|
|
378
|
+
"sys_updated_on",
|
|
379
|
+
],
|
|
380
|
+
readable: [
|
|
381
|
+
"sys_id",
|
|
382
|
+
"name",
|
|
383
|
+
"host_name",
|
|
384
|
+
"ip_address",
|
|
385
|
+
"os",
|
|
386
|
+
"os_version",
|
|
387
|
+
"manufacturer",
|
|
388
|
+
"model_id",
|
|
389
|
+
"serial_number",
|
|
390
|
+
"operational_status",
|
|
391
|
+
"install_status",
|
|
392
|
+
"assigned_to",
|
|
393
|
+
"support_group",
|
|
394
|
+
"location",
|
|
395
|
+
"sys_updated_on",
|
|
396
|
+
],
|
|
397
|
+
writable: [],
|
|
398
|
+
},
|
|
399
|
+
kb_knowledge: {
|
|
400
|
+
defaults: [
|
|
401
|
+
"sys_id",
|
|
402
|
+
"number",
|
|
403
|
+
"short_description",
|
|
404
|
+
"workflow_state",
|
|
405
|
+
"kb_knowledge_base",
|
|
406
|
+
"kb_category",
|
|
407
|
+
"author",
|
|
408
|
+
"published",
|
|
409
|
+
"sys_view_count",
|
|
410
|
+
"sys_updated_on",
|
|
411
|
+
],
|
|
412
|
+
readable: [
|
|
413
|
+
"sys_id",
|
|
414
|
+
"number",
|
|
415
|
+
"short_description",
|
|
416
|
+
"workflow_state",
|
|
417
|
+
"kb_knowledge_base",
|
|
418
|
+
"kb_category",
|
|
419
|
+
"author",
|
|
420
|
+
"published",
|
|
421
|
+
"sys_view_count",
|
|
422
|
+
"sys_updated_on",
|
|
423
|
+
],
|
|
424
|
+
writable: [],
|
|
425
|
+
},
|
|
426
|
+
task: {
|
|
427
|
+
defaults: [
|
|
428
|
+
"sys_id",
|
|
429
|
+
"number",
|
|
430
|
+
"short_description",
|
|
431
|
+
"state",
|
|
432
|
+
"priority",
|
|
433
|
+
"assigned_to",
|
|
434
|
+
"assignment_group",
|
|
435
|
+
"sys_class_name",
|
|
436
|
+
"opened_at",
|
|
437
|
+
"sys_updated_on",
|
|
438
|
+
],
|
|
439
|
+
readable: [
|
|
440
|
+
"sys_id",
|
|
441
|
+
"number",
|
|
442
|
+
"short_description",
|
|
443
|
+
"description",
|
|
444
|
+
"state",
|
|
445
|
+
"priority",
|
|
446
|
+
"assigned_to",
|
|
447
|
+
"assignment_group",
|
|
448
|
+
"sys_class_name",
|
|
449
|
+
"opened_at",
|
|
450
|
+
"sys_updated_on",
|
|
451
|
+
],
|
|
452
|
+
writable: [],
|
|
453
|
+
},
|
|
454
|
+
syslog: {
|
|
455
|
+
defaults: ["sys_id", "level", "source", "message", "sys_created_on"],
|
|
456
|
+
readable: [
|
|
457
|
+
"sys_id",
|
|
458
|
+
"level",
|
|
459
|
+
"source",
|
|
460
|
+
"message",
|
|
461
|
+
"sys_created_on",
|
|
462
|
+
"sys_updated_on",
|
|
463
|
+
],
|
|
464
|
+
writable: [],
|
|
465
|
+
},
|
|
466
|
+
sys_script: {
|
|
467
|
+
defaults: ["sys_id", "name", "script"],
|
|
468
|
+
readable: ["sys_id", "name", "script", "active", "collection", "filter", "when", "order", "sys_updated_on"],
|
|
469
|
+
writable: [],
|
|
470
|
+
},
|
|
471
|
+
sys_script_include: {
|
|
472
|
+
defaults: ["sys_id", "name", "script"],
|
|
473
|
+
readable: ["sys_id", "name", "script", "active", "api_name", "client_callable", "access", "sys_updated_on"],
|
|
474
|
+
writable: [],
|
|
475
|
+
},
|
|
476
|
+
sys_ui_script: {
|
|
477
|
+
defaults: ["sys_id", "name", "script"],
|
|
478
|
+
readable: ["sys_id", "name", "script", "active", "global", "sys_updated_on"],
|
|
479
|
+
writable: [],
|
|
480
|
+
},
|
|
481
|
+
sys_script_client: {
|
|
482
|
+
defaults: ["sys_id", "name", "script"],
|
|
483
|
+
readable: ["sys_id", "name", "script", "active", "table", "type", "condition", "sys_updated_on"],
|
|
484
|
+
writable: [],
|
|
485
|
+
},
|
|
486
|
+
sys_ws_operation: {
|
|
487
|
+
defaults: ["sys_id", "name", "operation_script"],
|
|
488
|
+
readable: ["sys_id", "name", "operation_script", "active", "http_method", "relative_path", "sys_updated_on"],
|
|
489
|
+
writable: [],
|
|
490
|
+
},
|
|
491
|
+
sys_db_object: {
|
|
492
|
+
defaults: ["sys_id", "name", "label", "super_class", "sys_scope", "is_extendable"],
|
|
493
|
+
readable: ["sys_id", "name", "label", "super_class", "sys_scope", "is_extendable"],
|
|
494
|
+
writable: [],
|
|
495
|
+
},
|
|
496
|
+
sys_app: {
|
|
497
|
+
defaults: ["sys_id", "name", "version", "scope", "active"],
|
|
498
|
+
readable: ["sys_id", "name", "version", "scope", "active"],
|
|
499
|
+
writable: [],
|
|
500
|
+
},
|
|
501
|
+
sys_store_app: {
|
|
502
|
+
defaults: ["sys_id", "name", "version", "scope", "active"],
|
|
503
|
+
readable: ["sys_id", "name", "version", "scope", "active"],
|
|
504
|
+
writable: [],
|
|
505
|
+
},
|
|
506
|
+
v_plugin: {
|
|
507
|
+
defaults: ["sys_id", "name", "active"],
|
|
508
|
+
readable: ["sys_id", "name", "active"],
|
|
509
|
+
writable: [],
|
|
510
|
+
},
|
|
511
|
+
sys_atf_test: {
|
|
512
|
+
defaults: ["sys_id", "name", "description", "active", "sys_updated_on"],
|
|
513
|
+
readable: ["sys_id", "name", "description", "active", "sys_updated_on"],
|
|
514
|
+
writable: [],
|
|
515
|
+
},
|
|
516
|
+
sys_atf_test_suite: {
|
|
517
|
+
defaults: ["sys_id", "name", "description", "active"],
|
|
518
|
+
readable: ["sys_id", "name", "description", "active", "sys_updated_on"],
|
|
519
|
+
writable: [],
|
|
520
|
+
},
|
|
521
|
+
sys_atf_test_suite_test: {
|
|
522
|
+
defaults: ["test"],
|
|
523
|
+
readable: ["sys_id", "test", "test_suite"],
|
|
524
|
+
writable: [],
|
|
525
|
+
},
|
|
526
|
+
sys_atf_test_result: {
|
|
527
|
+
defaults: ["sys_id", "test", "status", "output", "duration", "start_time", "end_time"],
|
|
528
|
+
readable: ["sys_id", "test", "test_suite", "execution", "parent", "status", "output", "duration", "start_time", "end_time", "sys_created_on"],
|
|
529
|
+
writable: [],
|
|
530
|
+
},
|
|
531
|
+
};
|
|
532
|
+
/**
|
|
533
|
+
* The policy base.
|
|
534
|
+
*
|
|
535
|
+
* The built-in entries contribute their `defaults` -- the bounded projection
|
|
536
|
+
* used when a caller names no fields -- and nothing else. Their `readable` and
|
|
537
|
+
* `writable` lists are deliberately NOT carried over: field policy does not
|
|
538
|
+
* deny at table granularity. Reachability is decided by the profile's
|
|
539
|
+
* `tableAccess` grant and by ServiceNow's per-user ACLs; a table that reaches
|
|
540
|
+
* this module has already been granted, so denying its fields here would only
|
|
541
|
+
* contradict the operator's own configuration.
|
|
542
|
+
*
|
|
543
|
+
* The "*" entry makes every other table resolvable with the same posture, so
|
|
544
|
+
* a granted custom table needs no field-policy entry to be usable.
|
|
545
|
+
*/
|
|
546
|
+
const FIELD_POLICY_BASE = Object.freeze({
|
|
547
|
+
...Object.fromEntries(Object.entries(POLICY_DEFINITIONS).map(([table, definition]) => [
|
|
548
|
+
table,
|
|
549
|
+
Object.freeze({
|
|
550
|
+
defaults: definition.defaults,
|
|
551
|
+
readable: FIELD_POLICY_WILDCARD,
|
|
552
|
+
writable: FIELD_POLICY_WILDCARD,
|
|
553
|
+
}),
|
|
554
|
+
])),
|
|
555
|
+
[FIELD_POLICY_WILDCARD]: Object.freeze({
|
|
556
|
+
defaults: GENERIC_DEFAULT_FIELDS,
|
|
557
|
+
readable: FIELD_POLICY_WILDCARD,
|
|
558
|
+
writable: FIELD_POLICY_WILDCARD,
|
|
559
|
+
}),
|
|
560
|
+
});
|
|
561
|
+
const BUILT_IN_TABLE_FIELD_POLICIES = buildFieldPolicyMap(FIELD_POLICY_BASE);
|
|
562
|
+
const GENERIC_RECORD_TABLES = new Set([
|
|
563
|
+
"incident",
|
|
564
|
+
"change_request",
|
|
565
|
+
"problem",
|
|
566
|
+
"sc_request",
|
|
567
|
+
"sc_req_item",
|
|
568
|
+
"sc_task",
|
|
569
|
+
"sys_user",
|
|
570
|
+
"sys_user_group",
|
|
571
|
+
"cmdb_ci",
|
|
572
|
+
"cmdb_ci_server",
|
|
573
|
+
"cmdb_ci_computer",
|
|
574
|
+
"kb_knowledge",
|
|
575
|
+
"task",
|
|
576
|
+
]);
|
|
577
|
+
/** Safe defaults exported for legacy concise-response helpers without drift. */
|
|
578
|
+
exports.SAFE_DEFAULT_FIELDS = Object.freeze(Object.fromEntries([...BUILT_IN_TABLE_FIELD_POLICIES]
|
|
579
|
+
.filter(([table]) => GENERIC_RECORD_TABLES.has(table))
|
|
580
|
+
.map(([table, policy]) => [table, policy.defaults])));
|
|
581
|
+
const FIELD_POLICY_CONFIGURATION_SCOPE = new node_async_hooks_1.AsyncLocalStorage();
|
|
582
|
+
const PREPARED_FIELD_ACCESS = Symbol("PreparedFieldAccess");
|
|
583
|
+
const ISSUED_FIELD_POLICY_ERRORS = new WeakSet();
|
|
584
|
+
const ISSUED_PREPARED_FIELD_ACCESS = new WeakSet();
|
|
585
|
+
/**
|
|
586
|
+
* Bounded policy rejection that never contains a field *value* or the policy
|
|
587
|
+
* contents. The table and field *names* are carried when known so the denial
|
|
588
|
+
* can be explained to an operator; both are caller-supplied identifiers that
|
|
589
|
+
* have already passed the FIELD_NAME regex, so echoing them discloses nothing
|
|
590
|
+
* the caller did not send.
|
|
591
|
+
*/
|
|
592
|
+
class FieldPolicyError extends Error {
|
|
593
|
+
reason;
|
|
594
|
+
table;
|
|
595
|
+
field;
|
|
596
|
+
constructor(reason, detail = {}) {
|
|
597
|
+
super("Field access denied by policy");
|
|
598
|
+
this.reason = reason;
|
|
599
|
+
this.name = "FieldPolicyError";
|
|
600
|
+
this.table = detail.table;
|
|
601
|
+
this.field = detail.field;
|
|
602
|
+
ISSUED_FIELD_POLICY_ERRORS.add(this);
|
|
603
|
+
Object.freeze(this);
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
exports.FieldPolicyError = FieldPolicyError;
|
|
607
|
+
/**
|
|
608
|
+
* Operator-facing explanation for a field-policy denial.
|
|
609
|
+
*
|
|
610
|
+
* Names the table, the offending field, and the configuration key that would
|
|
611
|
+
* grant it, so a denial is fixable without reading source. Two deliberate
|
|
612
|
+
* limits:
|
|
613
|
+
*
|
|
614
|
+
* - Field *values* never appear -- only names the caller itself supplied.
|
|
615
|
+
* - A `sensitive_field` denial names nothing. The field name *is* the signal
|
|
616
|
+
* there, so echoing it both reflects caller input into a response and hands
|
|
617
|
+
* back an oracle for the sensitive-name blocklist. That case is not operator-
|
|
618
|
+
* actionable anyway: a sensitive name cannot be granted by configuration.
|
|
619
|
+
*
|
|
620
|
+
* Every message keeps the phrase "denied by policy" so callers that classify
|
|
621
|
+
* denials on that substring keep working.
|
|
622
|
+
*/
|
|
623
|
+
function fieldPolicyDenialMessage(error) {
|
|
624
|
+
const { table, field } = error;
|
|
625
|
+
switch (error.reason) {
|
|
626
|
+
case "non_writable_field":
|
|
627
|
+
return table && field
|
|
628
|
+
? `Write denied by policy: field "${field}" is not writable on "${table}" under the ` +
|
|
629
|
+
`configured field policy. Add it to fieldPolicy.${table}.writable to allow this write.`
|
|
630
|
+
: "Write denied by policy: a field in the payload is not writable under the configured field policy.";
|
|
631
|
+
case "unreadable_field":
|
|
632
|
+
return table && field
|
|
633
|
+
? `Read denied by policy: field "${field}" is not readable on "${table}" under the ` +
|
|
634
|
+
`configured field policy. Add it to fieldPolicy.${table}.readable to allow this read.`
|
|
635
|
+
: "Read denied by policy: a requested field is not readable under the configured field policy.";
|
|
636
|
+
case "unsupported_table":
|
|
637
|
+
return table
|
|
638
|
+
? `Access denied by policy: table "${table}" has no field policy entry. Add ` +
|
|
639
|
+
`fieldPolicy.${table}, or a fieldPolicy."*" fallback, to allow it.`
|
|
640
|
+
: "Access denied by policy: the requested table has no field policy entry.";
|
|
641
|
+
case "sensitive_field":
|
|
642
|
+
return "Access denied by policy: a requested field is denied by name as sensitive and cannot be granted by configuration.";
|
|
643
|
+
default:
|
|
644
|
+
return `Access denied by policy (${error.reason}).`;
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
function isFieldPolicyError(error) {
|
|
648
|
+
try {
|
|
649
|
+
return ((typeof error !== "object" ||
|
|
650
|
+
error === null ||
|
|
651
|
+
!node_util_1.types.isProxy(error)) &&
|
|
652
|
+
error instanceof FieldPolicyError &&
|
|
653
|
+
Object.isFrozen(error) &&
|
|
654
|
+
ISSUED_FIELD_POLICY_ERRORS.has(error));
|
|
655
|
+
}
|
|
656
|
+
catch {
|
|
657
|
+
return false;
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
/**
|
|
661
|
+
* Snapshot untrusted tool arguments without evaluating accessors. Proxy
|
|
662
|
+
* failures, symbols, exotic prototypes, hidden properties, and excessive key
|
|
663
|
+
* sets are rejected before any argument value is traversed.
|
|
664
|
+
*/
|
|
665
|
+
function snapshotPlainDataArguments(candidate) {
|
|
666
|
+
return copyPlainDataArguments(candidate, false);
|
|
667
|
+
}
|
|
668
|
+
/** Copy already-snapshotted arguments and replace canonical values safely. */
|
|
669
|
+
function withFieldPolicyArgumentValues(args, overrides = {}) {
|
|
670
|
+
const output = copyPlainDataArguments(args, true);
|
|
671
|
+
const canonicalOverrides = copyPlainDataArguments(overrides, false);
|
|
672
|
+
const keys = argumentOwnKeys(canonicalOverrides, exports.MAX_TOOL_ARGUMENT_OWN_KEYS);
|
|
673
|
+
for (const key of keys) {
|
|
674
|
+
const descriptor = ownDataDescriptor(canonicalOverrides, key);
|
|
675
|
+
if (!descriptor)
|
|
676
|
+
throw new FieldPolicyError("invalid_argument_shape");
|
|
677
|
+
safeDefineArgument(output, key, descriptor.value);
|
|
678
|
+
}
|
|
679
|
+
return output;
|
|
680
|
+
}
|
|
681
|
+
/** Resolve a caller selection to a policy-approved field selection. */
|
|
682
|
+
function resolveReadableFields(tableCandidate, input = {}) {
|
|
683
|
+
const policy = requirePolicy(tableCandidate);
|
|
684
|
+
const canonicalInput = snapshotPlainDataArguments(input);
|
|
685
|
+
const responseFormat = normalizeResponseFormat(canonicalInput.responseFormat);
|
|
686
|
+
const requested = canonicalInput.fields;
|
|
687
|
+
if (requested === undefined) {
|
|
688
|
+
return requireNonEmptySelection(responseFormat === "detailed" ? readableSelection(policy) : policy.defaults);
|
|
689
|
+
}
|
|
690
|
+
if (typeof requested !== "string") {
|
|
691
|
+
throw new FieldPolicyError("invalid_field_selection");
|
|
692
|
+
}
|
|
693
|
+
const trimmed = requested.trim();
|
|
694
|
+
if (trimmed.toLowerCase() === "all") {
|
|
695
|
+
return requireNonEmptySelection(readableSelection(policy));
|
|
696
|
+
}
|
|
697
|
+
if (!trimmed)
|
|
698
|
+
throw new FieldPolicyError("invalid_field_selection");
|
|
699
|
+
const selected = [];
|
|
700
|
+
const seen = new Set();
|
|
701
|
+
for (const candidate of trimmed.split(",")) {
|
|
702
|
+
const field = normalizeFieldName(candidate);
|
|
703
|
+
if (seen.has(field))
|
|
704
|
+
throw new FieldPolicyError("invalid_field_selection");
|
|
705
|
+
if (isSensitiveFieldName(field)) {
|
|
706
|
+
throw new FieldPolicyError("sensitive_field", { table: policy.table });
|
|
707
|
+
}
|
|
708
|
+
if (!fieldSetAllows(policy.readable, policy.allowAnyReadable, field)) {
|
|
709
|
+
throw new FieldPolicyError("unreadable_field", { table: policy.table, field });
|
|
710
|
+
}
|
|
711
|
+
seen.add(field);
|
|
712
|
+
selected.push(field);
|
|
713
|
+
}
|
|
714
|
+
return Object.freeze(selected);
|
|
715
|
+
}
|
|
716
|
+
/** Validate and clone one Table API write map using the separate write set. */
|
|
717
|
+
function validateWritableFields(tableCandidate, payloadCandidate) {
|
|
718
|
+
const policy = requirePolicy(tableCandidate);
|
|
719
|
+
if (typeof payloadCandidate !== "object" ||
|
|
720
|
+
payloadCandidate === null ||
|
|
721
|
+
node_util_1.types.isProxy(payloadCandidate) ||
|
|
722
|
+
Array.isArray(payloadCandidate)) {
|
|
723
|
+
throw new FieldPolicyError("invalid_write_payload");
|
|
724
|
+
}
|
|
725
|
+
assertPlainWriteObject(payloadCandidate);
|
|
726
|
+
const entries = writeOwnDataEntries(payloadCandidate, exports.MAX_FIELD_OBJECT_OWN_KEYS);
|
|
727
|
+
if (entries.length === 0)
|
|
728
|
+
throw new FieldPolicyError("invalid_write_payload");
|
|
729
|
+
const output = {};
|
|
730
|
+
const traversal = {
|
|
731
|
+
nodes: 0,
|
|
732
|
+
seen: new WeakSet(),
|
|
733
|
+
};
|
|
734
|
+
for (const [candidate, descriptor] of entries) {
|
|
735
|
+
if (!("value" in descriptor)) {
|
|
736
|
+
throw new FieldPolicyError("invalid_write_payload");
|
|
737
|
+
}
|
|
738
|
+
const field = normalizeFieldName(candidate);
|
|
739
|
+
if (Object.hasOwn(output, field)) {
|
|
740
|
+
throw new FieldPolicyError("invalid_write_payload");
|
|
741
|
+
}
|
|
742
|
+
if (isSensitiveFieldName(field)) {
|
|
743
|
+
throw new FieldPolicyError("sensitive_field", { table: policy.table });
|
|
744
|
+
}
|
|
745
|
+
if (!fieldSetAllows(policy.writable, policy.allowAnyWritable, field)) {
|
|
746
|
+
throw new FieldPolicyError("non_writable_field", { table: policy.table, field });
|
|
747
|
+
}
|
|
748
|
+
safeDefine(output, field, cloneJsonSafeWriteValue(descriptor.value, traversal, 0));
|
|
749
|
+
}
|
|
750
|
+
return Object.freeze(output);
|
|
751
|
+
}
|
|
752
|
+
/**
|
|
753
|
+
* Attach a non-JSON marker after request-field validation. The symbol survives
|
|
754
|
+
* internal object spreads but can never be serialized into ServiceNow data.
|
|
755
|
+
*/
|
|
756
|
+
function prepareReadFieldArguments(args, table, options) {
|
|
757
|
+
const canonicalArgs = withFieldPolicyArgumentValues(args);
|
|
758
|
+
const canonicalOptions = snapshotPlainDataArguments(options);
|
|
759
|
+
const readableFields = resolveReadableFields(table, {
|
|
760
|
+
fields: canonicalOptions.allReadable ? "all" : canonicalArgs.fields,
|
|
761
|
+
responseFormat: canonicalArgs.response_format,
|
|
762
|
+
});
|
|
763
|
+
const overrides = { table };
|
|
764
|
+
if (canonicalOptions.exposeFieldsArgument) {
|
|
765
|
+
safeDefineArgument(overrides, "fields", isAllFieldSelection(readableFields) ? "all" : readableFields.join(","));
|
|
766
|
+
}
|
|
767
|
+
const prepared = withFieldPolicyArgumentValues(canonicalArgs, overrides);
|
|
768
|
+
safeDefineArgument(prepared, PREPARED_FIELD_ACCESS, issuePreparedAccess(canonicalArgs, table, readableFields));
|
|
769
|
+
return prepared;
|
|
770
|
+
}
|
|
771
|
+
/** Add a validated fixed/derived projection without changing public arguments. */
|
|
772
|
+
function prepareAdditionalReadFieldArguments(args, table, fields) {
|
|
773
|
+
const canonicalArgs = withFieldPolicyArgumentValues(args);
|
|
774
|
+
const readableFields = resolveReadableFields(table, { fields });
|
|
775
|
+
const prepared = withFieldPolicyArgumentValues(canonicalArgs);
|
|
776
|
+
safeDefineArgument(prepared, PREPARED_FIELD_ACCESS, issuePreparedAccess(canonicalArgs, table, readableFields));
|
|
777
|
+
return prepared;
|
|
778
|
+
}
|
|
779
|
+
/** Validate write keys and prepare the corresponding safe response projection. */
|
|
780
|
+
function prepareWriteFieldArguments(args, table) {
|
|
781
|
+
const canonicalArgs = withFieldPolicyArgumentValues(args);
|
|
782
|
+
const fields = validateWritableFields(table, canonicalArgs.fields);
|
|
783
|
+
const readableFields = resolveReadableFields(table, {
|
|
784
|
+
fields: "all",
|
|
785
|
+
responseFormat: "detailed",
|
|
786
|
+
});
|
|
787
|
+
const prepared = withFieldPolicyArgumentValues(canonicalArgs, { table, fields });
|
|
788
|
+
safeDefineArgument(prepared, PREPARED_FIELD_ACCESS, issuePreparedAccess(canonicalArgs, table, readableFields));
|
|
789
|
+
return prepared;
|
|
790
|
+
}
|
|
791
|
+
/** Recover only a marker issued by this module for the same canonical table. */
|
|
792
|
+
function preparedReadableFields(args, tableCandidate) {
|
|
793
|
+
try {
|
|
794
|
+
if (typeof args !== "object" ||
|
|
795
|
+
args === null ||
|
|
796
|
+
node_util_1.types.isProxy(args)) {
|
|
797
|
+
return undefined;
|
|
798
|
+
}
|
|
799
|
+
const marker = safeOwnDataValue(args, PREPARED_FIELD_ACCESS);
|
|
800
|
+
if (typeof marker !== "object" ||
|
|
801
|
+
marker === null ||
|
|
802
|
+
node_util_1.types.isProxy(marker) ||
|
|
803
|
+
!ISSUED_PREPARED_FIELD_ACCESS.has(marker) ||
|
|
804
|
+
!Object.isFrozen(marker)) {
|
|
805
|
+
return undefined;
|
|
806
|
+
}
|
|
807
|
+
const table = normalizeTableName(tableCandidate);
|
|
808
|
+
const entries = safeOwnDataValue(marker, "entries");
|
|
809
|
+
if (!Array.isArray(entries) || !Object.isFrozen(entries))
|
|
810
|
+
return undefined;
|
|
811
|
+
const entry = entries.find((candidate) => typeof candidate === "object" &&
|
|
812
|
+
candidate !== null &&
|
|
813
|
+
Object.isFrozen(candidate) &&
|
|
814
|
+
safeOwnDataValue(candidate, "table") === table);
|
|
815
|
+
if (!entry)
|
|
816
|
+
return undefined;
|
|
817
|
+
const fields = safeOwnDataValue(entry, "readableFields");
|
|
818
|
+
const policy = requirePolicy(table);
|
|
819
|
+
if (!Array.isArray(fields) || !Object.isFrozen(fields))
|
|
820
|
+
return undefined;
|
|
821
|
+
if (isAllFieldSelection(fields)) {
|
|
822
|
+
return policy.allowAnyReadable ? ALL_FIELDS_SELECTION : undefined;
|
|
823
|
+
}
|
|
824
|
+
if (fields.some((field) => typeof field !== "string" ||
|
|
825
|
+
!fieldSetAllows(policy.readable, policy.allowAnyReadable, field))) {
|
|
826
|
+
return undefined;
|
|
827
|
+
}
|
|
828
|
+
return fields;
|
|
829
|
+
}
|
|
830
|
+
catch {
|
|
831
|
+
return undefined;
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
/** Keep approved top-level record fields and scrub sensitive nested keys. */
|
|
835
|
+
function filterReadableRecord(candidate, readableFields) {
|
|
836
|
+
if (node_util_1.types.isProxy(readableFields))
|
|
837
|
+
return null;
|
|
838
|
+
const state = responseTraversalState();
|
|
839
|
+
if (isAllFieldSelection(readableFields)) {
|
|
840
|
+
return sanitizeNestedValue(candidate, state, 0);
|
|
841
|
+
}
|
|
842
|
+
const approved = new Set(readableFields.map(normalizeFieldName));
|
|
843
|
+
return filterRecordNode(candidate, approved, state, 0);
|
|
844
|
+
}
|
|
845
|
+
/** Remove metadata entries for fields the caller is not permitted to discover. */
|
|
846
|
+
function filterSchemaEntries(candidate, readableFields) {
|
|
847
|
+
if (typeof candidate !== "object" ||
|
|
848
|
+
candidate === null ||
|
|
849
|
+
node_util_1.types.isProxy(candidate) ||
|
|
850
|
+
!Array.isArray(candidate)) {
|
|
851
|
+
return [];
|
|
852
|
+
}
|
|
853
|
+
if (node_util_1.types.isProxy(readableFields))
|
|
854
|
+
return [];
|
|
855
|
+
const allReadable = isAllFieldSelection(readableFields);
|
|
856
|
+
const approved = allReadable ? undefined : new Set(readableFields.map(normalizeFieldName));
|
|
857
|
+
const output = [];
|
|
858
|
+
const state = responseTraversalState();
|
|
859
|
+
const view = responseArrayView(candidate);
|
|
860
|
+
if (!view)
|
|
861
|
+
return output;
|
|
862
|
+
for (let index = 0; index < view.length; index += 1) {
|
|
863
|
+
const key = String(index);
|
|
864
|
+
if (!view.presentKeys.has(key))
|
|
865
|
+
continue;
|
|
866
|
+
const entryDescriptor = ownDataDescriptor(candidate, key);
|
|
867
|
+
const entry = entryDescriptor?.value;
|
|
868
|
+
if (typeof entry !== "object" ||
|
|
869
|
+
entry === null ||
|
|
870
|
+
node_util_1.types.isProxy(entry) ||
|
|
871
|
+
Array.isArray(entry)) {
|
|
872
|
+
continue;
|
|
873
|
+
}
|
|
874
|
+
const keys = responseOwnKeys(entry, exports.MAX_FIELD_OBJECT_OWN_KEYS);
|
|
875
|
+
if (!keys?.includes("element"))
|
|
876
|
+
continue;
|
|
877
|
+
const rawField = safeOwnDataValue(entry, "element");
|
|
878
|
+
let field;
|
|
879
|
+
try {
|
|
880
|
+
field = normalizeFieldName(rawField);
|
|
881
|
+
}
|
|
882
|
+
catch {
|
|
883
|
+
continue;
|
|
884
|
+
}
|
|
885
|
+
// A wildcard readable set leaves `approved` undefined; the sensitive-name
|
|
886
|
+
// check is then the only thing keeping user_password and its siblings out
|
|
887
|
+
// of schema metadata.
|
|
888
|
+
if ((approved && !approved.has(field)) || isSensitiveFieldName(field))
|
|
889
|
+
continue;
|
|
890
|
+
const sanitized = sanitizeNestedValue(entry, state, 1);
|
|
891
|
+
if (typeof sanitized !== "object" ||
|
|
892
|
+
sanitized === null ||
|
|
893
|
+
Array.isArray(sanitized)) {
|
|
894
|
+
continue;
|
|
895
|
+
}
|
|
896
|
+
output.push(sanitized);
|
|
897
|
+
}
|
|
898
|
+
return output;
|
|
899
|
+
}
|
|
900
|
+
/** Filter the documented Stats API envelope and approved aggregate fields. */
|
|
901
|
+
function filterAggregateResult(candidate, readableFields) {
|
|
902
|
+
if (node_util_1.types.isProxy(readableFields))
|
|
903
|
+
return null;
|
|
904
|
+
const state = responseTraversalState();
|
|
905
|
+
if (isAllFieldSelection(readableFields)) {
|
|
906
|
+
return sanitizeNestedValue(candidate, state, 0);
|
|
907
|
+
}
|
|
908
|
+
const approved = new Set(readableFields.map(normalizeFieldName));
|
|
909
|
+
return filterAggregateNode(candidate, approved, state, 0);
|
|
910
|
+
}
|
|
911
|
+
function runWithFieldPolicyConfiguration(configuration, callback) {
|
|
912
|
+
return FIELD_POLICY_CONFIGURATION_SCOPE.run(configuration ?? Object.freeze({}), callback);
|
|
913
|
+
}
|
|
914
|
+
/** True when a field selection means ServiceNow should return all fields. */
|
|
915
|
+
function isAllFieldSelection(selection) {
|
|
916
|
+
return selection.length === 1 && selection[0] === FIELD_POLICY_WILDCARD;
|
|
917
|
+
}
|
|
918
|
+
/** Convert a policy selection into a Table API sysparm_fields value. */
|
|
919
|
+
function fieldSelectionToSysparmFields(selection) {
|
|
920
|
+
return isAllFieldSelection(selection) ? undefined : selection.join(",");
|
|
921
|
+
}
|
|
922
|
+
/** Sensitive names are denied even if accidentally added to a table policy. */
|
|
923
|
+
function isSensitiveFieldName(candidate) {
|
|
924
|
+
if (typeof candidate !== "string")
|
|
925
|
+
return true;
|
|
926
|
+
const normalized = candidate.trim().toLowerCase();
|
|
927
|
+
if (!FIELD_NAME.test(normalized))
|
|
928
|
+
return true;
|
|
929
|
+
return (/(?:^|_)(?:password|passwd|credential|credentials|secret|token|api_key|private_key|encryption_key|ssn)(?:_|$)/u.test(normalized) ||
|
|
930
|
+
normalized === "social_security_number");
|
|
931
|
+
}
|
|
932
|
+
function requirePolicy(tableCandidate) {
|
|
933
|
+
const table = normalizeTableName(tableCandidate);
|
|
934
|
+
const policies = currentTableFieldPolicies();
|
|
935
|
+
const policy = policies.get(table);
|
|
936
|
+
if (policy)
|
|
937
|
+
return policy;
|
|
938
|
+
const fallback = policies.get(FIELD_POLICY_WILDCARD);
|
|
939
|
+
if (!fallback)
|
|
940
|
+
throw new FieldPolicyError("unsupported_table", { table });
|
|
941
|
+
return Object.freeze({ ...fallback, table });
|
|
942
|
+
}
|
|
943
|
+
function fieldSetAllows(fields, allowAny, field) {
|
|
944
|
+
return allowAny || fields.includes(field);
|
|
945
|
+
}
|
|
946
|
+
function currentTableFieldPolicies() {
|
|
947
|
+
const configured = readConfiguredFieldPolicyDefinitions();
|
|
948
|
+
if (!configured)
|
|
949
|
+
return BUILT_IN_TABLE_FIELD_POLICIES;
|
|
950
|
+
// Configuration narrows the base; it is the only thing that denies a field.
|
|
951
|
+
return buildFieldPolicyMap(mergeFieldPolicyDefinitions(FIELD_POLICY_BASE, configured.definitions));
|
|
952
|
+
}
|
|
953
|
+
function mergeFieldPolicyDefinitions(base, configured) {
|
|
954
|
+
const merged = { ...base };
|
|
955
|
+
for (const [table, definition] of Object.entries(configured)) {
|
|
956
|
+
const existing = merged[table];
|
|
957
|
+
merged[table] = {
|
|
958
|
+
defaults: definition.defaults ??
|
|
959
|
+
inheritedDefaults(existing?.defaults, definition.readable),
|
|
960
|
+
// An entry that states only `defaults` reads exactly those fields.
|
|
961
|
+
readable: definition.readable ?? existing?.readable ?? definition.defaults,
|
|
962
|
+
// Omission no longer denies. An operator who states nothing about
|
|
963
|
+
// writes has not asked for a restriction, and tableAccess already
|
|
964
|
+
// decided whether this table is reachable at all.
|
|
965
|
+
writable: definition.writable ?? existing?.writable,
|
|
966
|
+
};
|
|
967
|
+
}
|
|
968
|
+
return merged;
|
|
969
|
+
}
|
|
970
|
+
/**
|
|
971
|
+
* Carry inherited defaults across a configured `readable` replacement.
|
|
972
|
+
*
|
|
973
|
+
* Inheriting them wholesale lets a narrowing override keep defaults the new
|
|
974
|
+
* readable set no longer permits, which the consistency check in
|
|
975
|
+
* buildFieldPolicyMap then rejects on every request.
|
|
976
|
+
*/
|
|
977
|
+
function inheritedDefaults(existingDefaults, configuredReadable) {
|
|
978
|
+
if (configuredReadable === undefined)
|
|
979
|
+
return existingDefaults;
|
|
980
|
+
if (configuredReadable === FIELD_POLICY_WILDCARD) {
|
|
981
|
+
// Widening what may be read says nothing about what to return by default.
|
|
982
|
+
// Keep the inherited projection; where there is none, fall through to the
|
|
983
|
+
// bounded generic default rather than manufacturing a wildcard.
|
|
984
|
+
return existingDefaults === FIELD_POLICY_WILDCARD ? undefined : existingDefaults;
|
|
985
|
+
}
|
|
986
|
+
if (existingDefaults === undefined || existingDefaults === FIELD_POLICY_WILDCARD) {
|
|
987
|
+
return configuredReadable;
|
|
988
|
+
}
|
|
989
|
+
const permitted = existingDefaults.filter((field) => configuredReadable.includes(field));
|
|
990
|
+
return permitted.length > 0 ? permitted : configuredReadable;
|
|
991
|
+
}
|
|
992
|
+
function buildFieldPolicyMap(definitions) {
|
|
993
|
+
return new Map(Object.entries(definitions).map(([tableCandidate, definition]) => {
|
|
994
|
+
const table = normalizePolicyTableName(tableCandidate);
|
|
995
|
+
// An unstated set is open. Only an explicit operator list narrows.
|
|
996
|
+
const readable = normalizePolicyFieldSet(definition.readable ?? FIELD_POLICY_WILDCARD);
|
|
997
|
+
const writable = normalizePolicyFieldSet(definition.writable ?? FIELD_POLICY_WILDCARD);
|
|
998
|
+
const defaultsCandidate = normalizePolicyFieldSet(definition.defaults ??
|
|
999
|
+
(readable === FIELD_POLICY_WILDCARD ? GENERIC_DEFAULT_FIELDS : readable));
|
|
1000
|
+
const defaults = defaultsCandidate === FIELD_POLICY_WILDCARD ? ALL_FIELDS_SELECTION : defaultsCandidate;
|
|
1001
|
+
const readableFields = readable === FIELD_POLICY_WILDCARD ? [] : readable;
|
|
1002
|
+
const writableFields = writable === FIELD_POLICY_WILDCARD ? [] : writable;
|
|
1003
|
+
if (!isAllFieldSelection(defaults)) {
|
|
1004
|
+
for (const field of defaults) {
|
|
1005
|
+
if (!fieldSetAllows(readableFields, readable === FIELD_POLICY_WILDCARD, field)) {
|
|
1006
|
+
// Branded so a contradictory operator policy is classified as a
|
|
1007
|
+
// policy denial instead of escaping the request path as a raw
|
|
1008
|
+
// TypeError.
|
|
1009
|
+
throw new FieldPolicyError("invalid_argument_shape");
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
return [
|
|
1014
|
+
table,
|
|
1015
|
+
Object.freeze({
|
|
1016
|
+
table,
|
|
1017
|
+
defaults,
|
|
1018
|
+
readable: readableFields,
|
|
1019
|
+
writable: writableFields,
|
|
1020
|
+
allowAnyReadable: readable === FIELD_POLICY_WILDCARD,
|
|
1021
|
+
allowAnyWritable: writable === FIELD_POLICY_WILDCARD,
|
|
1022
|
+
}),
|
|
1023
|
+
];
|
|
1024
|
+
}));
|
|
1025
|
+
}
|
|
1026
|
+
function readConfiguredFieldPolicyDefinitions() {
|
|
1027
|
+
const scoped = FIELD_POLICY_CONFIGURATION_SCOPE.getStore();
|
|
1028
|
+
if (scoped && (scoped.fieldPolicy !== undefined || scoped.readableTableFields !== undefined || scoped.writableTableFields !== undefined)) {
|
|
1029
|
+
return normalizeConfiguredFieldPolicy(scoped);
|
|
1030
|
+
}
|
|
1031
|
+
const text = process.env[FIELD_POLICY_DEFINITIONS_ENV];
|
|
1032
|
+
if (text === undefined || text.trim() === "")
|
|
1033
|
+
return undefined;
|
|
1034
|
+
if (text.length > MAX_FIELD_POLICY_DEFINITIONS_TEXT_LENGTH) {
|
|
1035
|
+
throw new FieldPolicyError("invalid_argument_shape");
|
|
1036
|
+
}
|
|
1037
|
+
let parsed;
|
|
1038
|
+
try {
|
|
1039
|
+
parsed = JSON.parse(text);
|
|
1040
|
+
}
|
|
1041
|
+
catch {
|
|
1042
|
+
throw new FieldPolicyError("invalid_argument_shape");
|
|
1043
|
+
}
|
|
1044
|
+
return normalizeConfiguredFieldPolicy(parsed);
|
|
1045
|
+
}
|
|
1046
|
+
function normalizeConfiguredFieldPolicy(parsed) {
|
|
1047
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed) || node_util_1.types.isProxy(parsed)) {
|
|
1048
|
+
throw new FieldPolicyError("invalid_argument_shape");
|
|
1049
|
+
}
|
|
1050
|
+
const outerRecord = definedOwnProperties(parsed);
|
|
1051
|
+
const policySource = Object.hasOwn(outerRecord, "fieldPolicy")
|
|
1052
|
+
? outerRecord.fieldPolicy
|
|
1053
|
+
: outerRecord;
|
|
1054
|
+
if (Object.hasOwn(outerRecord, "readableTableFields") ||
|
|
1055
|
+
Object.hasOwn(outerRecord, "writableTableFields")) {
|
|
1056
|
+
const definitions = {};
|
|
1057
|
+
applyTableFieldList(definitions, outerRecord.readableTableFields, "readable");
|
|
1058
|
+
applyTableFieldList(definitions, outerRecord.writableTableFields, "writable");
|
|
1059
|
+
return Object.freeze({ definitions });
|
|
1060
|
+
}
|
|
1061
|
+
if (policySource === undefined)
|
|
1062
|
+
return undefined;
|
|
1063
|
+
if (typeof policySource !== "object" || policySource === null || Array.isArray(policySource) || node_util_1.types.isProxy(policySource)) {
|
|
1064
|
+
throw new FieldPolicyError("invalid_argument_shape");
|
|
1065
|
+
}
|
|
1066
|
+
const record = policySource;
|
|
1067
|
+
const definitions = {};
|
|
1068
|
+
for (const key of Object.keys(record)) {
|
|
1069
|
+
const table = normalizePolicyTableName(key);
|
|
1070
|
+
const value = record[key];
|
|
1071
|
+
if (typeof value !== "object" || value === null || Array.isArray(value) || node_util_1.types.isProxy(value)) {
|
|
1072
|
+
throw new FieldPolicyError("invalid_argument_shape");
|
|
1073
|
+
}
|
|
1074
|
+
const candidate = definedOwnProperties(value);
|
|
1075
|
+
const definition = {};
|
|
1076
|
+
if (Object.hasOwn(candidate, "defaults")) {
|
|
1077
|
+
definition.defaults = normalizePolicyFieldSet(candidate.defaults);
|
|
1078
|
+
}
|
|
1079
|
+
if (Object.hasOwn(candidate, "readable")) {
|
|
1080
|
+
definition.readable = normalizePolicyFieldSet(candidate.readable);
|
|
1081
|
+
}
|
|
1082
|
+
if (Object.hasOwn(candidate, "writable")) {
|
|
1083
|
+
definition.writable = normalizePolicyFieldSet(candidate.writable);
|
|
1084
|
+
}
|
|
1085
|
+
definitions[table] = Object.freeze(definition);
|
|
1086
|
+
}
|
|
1087
|
+
return Object.freeze({ definitions });
|
|
1088
|
+
}
|
|
1089
|
+
/**
|
|
1090
|
+
* Drop own keys whose value is `undefined`.
|
|
1091
|
+
*
|
|
1092
|
+
* Policy shape is selected by key presence, and a key materialized with an
|
|
1093
|
+
* `undefined` value is indistinguishable from a stated one to Object.hasOwn.
|
|
1094
|
+
* Normalizing here keeps an object built with optional keys spread in
|
|
1095
|
+
* identical to one that never carried the key at all, so the same policy
|
|
1096
|
+
* cannot mean different things depending on how the caller constructed it.
|
|
1097
|
+
*/
|
|
1098
|
+
function definedOwnProperties(record) {
|
|
1099
|
+
const defined = {};
|
|
1100
|
+
for (const [key, value] of Object.entries(record)) {
|
|
1101
|
+
if (value !== undefined)
|
|
1102
|
+
defined[key] = value;
|
|
1103
|
+
}
|
|
1104
|
+
return defined;
|
|
1105
|
+
}
|
|
1106
|
+
function applyTableFieldList(definitions, candidate, kind) {
|
|
1107
|
+
if (candidate === undefined)
|
|
1108
|
+
return;
|
|
1109
|
+
if (!Array.isArray(candidate) || node_util_1.types.isProxy(candidate)) {
|
|
1110
|
+
throw new FieldPolicyError("invalid_argument_shape");
|
|
1111
|
+
}
|
|
1112
|
+
for (const entry of candidate) {
|
|
1113
|
+
const normalized = normalizeTableFieldListDefinition(entry);
|
|
1114
|
+
const previous = definitions[normalized.table] ?? {};
|
|
1115
|
+
definitions[normalized.table] = Object.freeze({
|
|
1116
|
+
...previous,
|
|
1117
|
+
[kind]: normalized.fields,
|
|
1118
|
+
});
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
function normalizeTableFieldListDefinition(candidate) {
|
|
1122
|
+
if (typeof candidate !== "object" || candidate === null || Array.isArray(candidate) || node_util_1.types.isProxy(candidate)) {
|
|
1123
|
+
throw new FieldPolicyError("invalid_argument_shape");
|
|
1124
|
+
}
|
|
1125
|
+
const record = candidate;
|
|
1126
|
+
const table = normalizePolicyTableName(record.table);
|
|
1127
|
+
const fields = normalizePolicyFieldSet(record.fields);
|
|
1128
|
+
return Object.freeze({ table, fields });
|
|
1129
|
+
}
|
|
1130
|
+
function readableSelection(policy) {
|
|
1131
|
+
return policy.allowAnyReadable ? ALL_FIELDS_SELECTION : policy.readable;
|
|
1132
|
+
}
|
|
1133
|
+
/**
|
|
1134
|
+
* A table policy granting no readable field denies the read outright. An
|
|
1135
|
+
* empty selection would otherwise serialize to an empty sysparm_fields, which
|
|
1136
|
+
* ServiceNow reads as "every field".
|
|
1137
|
+
*/
|
|
1138
|
+
function requireNonEmptySelection(selection) {
|
|
1139
|
+
if (selection.length === 0)
|
|
1140
|
+
throw new FieldPolicyError("unreadable_field");
|
|
1141
|
+
return selection;
|
|
1142
|
+
}
|
|
1143
|
+
function normalizePolicyTableName(candidate) {
|
|
1144
|
+
if (candidate === FIELD_POLICY_WILDCARD)
|
|
1145
|
+
return FIELD_POLICY_WILDCARD;
|
|
1146
|
+
return normalizeTableName(candidate);
|
|
1147
|
+
}
|
|
1148
|
+
function normalizeTableName(candidate) {
|
|
1149
|
+
if (typeof candidate !== "string") {
|
|
1150
|
+
throw new FieldPolicyError("unsupported_table");
|
|
1151
|
+
}
|
|
1152
|
+
const normalized = candidate.trim().toLowerCase();
|
|
1153
|
+
if (!FIELD_NAME.test(normalized)) {
|
|
1154
|
+
throw new FieldPolicyError("unsupported_table");
|
|
1155
|
+
}
|
|
1156
|
+
return normalized;
|
|
1157
|
+
}
|
|
1158
|
+
function normalizeFieldName(candidate) {
|
|
1159
|
+
if (typeof candidate !== "string") {
|
|
1160
|
+
throw new FieldPolicyError("invalid_field_selection");
|
|
1161
|
+
}
|
|
1162
|
+
const normalized = candidate.trim().toLowerCase();
|
|
1163
|
+
if (!FIELD_NAME.test(normalized)) {
|
|
1164
|
+
throw new FieldPolicyError("invalid_field_selection");
|
|
1165
|
+
}
|
|
1166
|
+
return normalized;
|
|
1167
|
+
}
|
|
1168
|
+
function normalizeResponseFormat(candidate) {
|
|
1169
|
+
if (candidate === undefined || candidate === "concise")
|
|
1170
|
+
return "concise";
|
|
1171
|
+
if (candidate === "detailed")
|
|
1172
|
+
return "detailed";
|
|
1173
|
+
throw new FieldPolicyError("invalid_field_selection");
|
|
1174
|
+
}
|
|
1175
|
+
function normalizePolicyFieldSet(candidates) {
|
|
1176
|
+
if (candidates === FIELD_POLICY_WILDCARD)
|
|
1177
|
+
return FIELD_POLICY_WILDCARD;
|
|
1178
|
+
return normalizePolicyFields(candidates);
|
|
1179
|
+
}
|
|
1180
|
+
function normalizePolicyFields(candidates) {
|
|
1181
|
+
// Operator policy is normalized on the request path, so a malformed set
|
|
1182
|
+
// must surface as a branded policy denial rather than a raw TypeError.
|
|
1183
|
+
if (!Array.isArray(candidates) || node_util_1.types.isProxy(candidates)) {
|
|
1184
|
+
throw new FieldPolicyError("invalid_argument_shape");
|
|
1185
|
+
}
|
|
1186
|
+
const fields = candidates.map((field) => normalizeFieldName(field));
|
|
1187
|
+
if (new Set(fields).size !== fields.length) {
|
|
1188
|
+
throw new FieldPolicyError("invalid_argument_shape");
|
|
1189
|
+
}
|
|
1190
|
+
return Object.freeze(fields);
|
|
1191
|
+
}
|
|
1192
|
+
function issuePreparedAccess(args, table, readableFields) {
|
|
1193
|
+
const normalizedTable = normalizeTableName(table);
|
|
1194
|
+
const previous = issuedPreparedEntries(args).filter((entry) => entry.table !== normalizedTable);
|
|
1195
|
+
const entry = Object.freeze({
|
|
1196
|
+
table: normalizedTable,
|
|
1197
|
+
readableFields: isAllFieldSelection(readableFields)
|
|
1198
|
+
? ALL_FIELDS_SELECTION
|
|
1199
|
+
: Object.freeze([...readableFields]),
|
|
1200
|
+
});
|
|
1201
|
+
const marker = Object.freeze({
|
|
1202
|
+
entries: Object.freeze([...previous, entry]),
|
|
1203
|
+
});
|
|
1204
|
+
ISSUED_PREPARED_FIELD_ACCESS.add(marker);
|
|
1205
|
+
return marker;
|
|
1206
|
+
}
|
|
1207
|
+
const AGGREGATE_ENVELOPE_FIELDS = new Set([
|
|
1208
|
+
"stats",
|
|
1209
|
+
"groupby_fields",
|
|
1210
|
+
"field",
|
|
1211
|
+
"value",
|
|
1212
|
+
"display_value",
|
|
1213
|
+
"count",
|
|
1214
|
+
"avg",
|
|
1215
|
+
"min",
|
|
1216
|
+
"max",
|
|
1217
|
+
"sum",
|
|
1218
|
+
]);
|
|
1219
|
+
function issuedPreparedEntries(args) {
|
|
1220
|
+
try {
|
|
1221
|
+
const marker = safeOwnDataValue(args, PREPARED_FIELD_ACCESS);
|
|
1222
|
+
if (typeof marker !== "object" ||
|
|
1223
|
+
marker === null ||
|
|
1224
|
+
node_util_1.types.isProxy(marker) ||
|
|
1225
|
+
!ISSUED_PREPARED_FIELD_ACCESS.has(marker) ||
|
|
1226
|
+
!Object.isFrozen(marker)) {
|
|
1227
|
+
return [];
|
|
1228
|
+
}
|
|
1229
|
+
const entries = safeOwnDataValue(marker, "entries");
|
|
1230
|
+
return Array.isArray(entries) && Object.isFrozen(entries)
|
|
1231
|
+
? entries
|
|
1232
|
+
: [];
|
|
1233
|
+
}
|
|
1234
|
+
catch {
|
|
1235
|
+
return [];
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
function copyPlainDataArguments(candidate, allowPreparedMarker) {
|
|
1239
|
+
if (typeof candidate !== "object" ||
|
|
1240
|
+
candidate === null ||
|
|
1241
|
+
node_util_1.types.isProxy(candidate) ||
|
|
1242
|
+
Array.isArray(candidate)) {
|
|
1243
|
+
throw new FieldPolicyError("invalid_argument_shape");
|
|
1244
|
+
}
|
|
1245
|
+
try {
|
|
1246
|
+
const prototype = Object.getPrototypeOf(candidate);
|
|
1247
|
+
if (prototype !== Object.prototype && prototype !== null) {
|
|
1248
|
+
throw new FieldPolicyError("invalid_argument_shape");
|
|
1249
|
+
}
|
|
1250
|
+
}
|
|
1251
|
+
catch (error) {
|
|
1252
|
+
if (isFieldPolicyError(error))
|
|
1253
|
+
throw error;
|
|
1254
|
+
throw new FieldPolicyError("invalid_argument_shape");
|
|
1255
|
+
}
|
|
1256
|
+
const keys = argumentOwnKeys(candidate, exports.MAX_TOOL_ARGUMENT_OWN_KEYS +
|
|
1257
|
+
(allowPreparedMarker ? MAX_CANONICAL_ARGUMENT_KEYS + 1 : 0));
|
|
1258
|
+
const carriesPreparedMarker = keys.includes(PREPARED_FIELD_ACCESS);
|
|
1259
|
+
if (keys.length > exports.MAX_TOOL_ARGUMENT_OWN_KEYS &&
|
|
1260
|
+
(!allowPreparedMarker || !carriesPreparedMarker)) {
|
|
1261
|
+
throw new FieldPolicyError("invalid_argument_shape");
|
|
1262
|
+
}
|
|
1263
|
+
const output = {};
|
|
1264
|
+
for (const key of keys) {
|
|
1265
|
+
if (typeof key === "symbol") {
|
|
1266
|
+
if (key !== PREPARED_FIELD_ACCESS || !allowPreparedMarker) {
|
|
1267
|
+
throw new FieldPolicyError("invalid_argument_shape");
|
|
1268
|
+
}
|
|
1269
|
+
const descriptor = ownDataDescriptor(candidate, key);
|
|
1270
|
+
if (!descriptor ||
|
|
1271
|
+
typeof descriptor.value !== "object" ||
|
|
1272
|
+
descriptor.value === null ||
|
|
1273
|
+
!ISSUED_PREPARED_FIELD_ACCESS.has(descriptor.value)) {
|
|
1274
|
+
throw new FieldPolicyError("invalid_argument_shape");
|
|
1275
|
+
}
|
|
1276
|
+
safeDefineArgument(output, key, descriptor.value);
|
|
1277
|
+
continue;
|
|
1278
|
+
}
|
|
1279
|
+
const descriptor = ownDataDescriptor(candidate, key);
|
|
1280
|
+
if (!descriptor || !descriptor.enumerable) {
|
|
1281
|
+
throw new FieldPolicyError("invalid_argument_shape");
|
|
1282
|
+
}
|
|
1283
|
+
safeDefineArgument(output, key, descriptor.value);
|
|
1284
|
+
}
|
|
1285
|
+
return output;
|
|
1286
|
+
}
|
|
1287
|
+
function argumentOwnKeys(candidate, maximum) {
|
|
1288
|
+
try {
|
|
1289
|
+
const keys = Reflect.ownKeys(candidate);
|
|
1290
|
+
if (keys.length > maximum) {
|
|
1291
|
+
throw new FieldPolicyError("invalid_argument_shape");
|
|
1292
|
+
}
|
|
1293
|
+
return keys;
|
|
1294
|
+
}
|
|
1295
|
+
catch (error) {
|
|
1296
|
+
if (isFieldPolicyError(error))
|
|
1297
|
+
throw error;
|
|
1298
|
+
throw new FieldPolicyError("invalid_argument_shape");
|
|
1299
|
+
}
|
|
1300
|
+
}
|
|
1301
|
+
function ownDataDescriptor(candidate, key) {
|
|
1302
|
+
if (node_util_1.types.isProxy(candidate))
|
|
1303
|
+
return undefined;
|
|
1304
|
+
try {
|
|
1305
|
+
const descriptor = Object.getOwnPropertyDescriptor(candidate, key);
|
|
1306
|
+
return descriptor && "value" in descriptor
|
|
1307
|
+
? descriptor
|
|
1308
|
+
: undefined;
|
|
1309
|
+
}
|
|
1310
|
+
catch {
|
|
1311
|
+
return undefined;
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
function safeDefineArgument(target, key, value) {
|
|
1315
|
+
try {
|
|
1316
|
+
Object.defineProperty(target, key, {
|
|
1317
|
+
value,
|
|
1318
|
+
enumerable: true,
|
|
1319
|
+
configurable: true,
|
|
1320
|
+
writable: true,
|
|
1321
|
+
});
|
|
1322
|
+
}
|
|
1323
|
+
catch {
|
|
1324
|
+
throw new FieldPolicyError("invalid_argument_shape");
|
|
1325
|
+
}
|
|
1326
|
+
}
|
|
1327
|
+
function assertPlainWriteObject(candidate) {
|
|
1328
|
+
if (node_util_1.types.isProxy(candidate)) {
|
|
1329
|
+
throw new FieldPolicyError("invalid_write_payload");
|
|
1330
|
+
}
|
|
1331
|
+
try {
|
|
1332
|
+
const prototype = Object.getPrototypeOf(candidate);
|
|
1333
|
+
if (prototype !== Object.prototype && prototype !== null) {
|
|
1334
|
+
throw new FieldPolicyError("invalid_write_payload");
|
|
1335
|
+
}
|
|
1336
|
+
}
|
|
1337
|
+
catch (error) {
|
|
1338
|
+
if (isFieldPolicyError(error))
|
|
1339
|
+
throw error;
|
|
1340
|
+
throw new FieldPolicyError("invalid_write_payload");
|
|
1341
|
+
}
|
|
1342
|
+
}
|
|
1343
|
+
function writeOwnKeys(candidate, maximum) {
|
|
1344
|
+
if (node_util_1.types.isProxy(candidate)) {
|
|
1345
|
+
throw new FieldPolicyError("invalid_write_payload");
|
|
1346
|
+
}
|
|
1347
|
+
try {
|
|
1348
|
+
const keys = Reflect.ownKeys(candidate);
|
|
1349
|
+
if (keys.length > maximum) {
|
|
1350
|
+
throw new FieldPolicyError("traversal_limit_exceeded");
|
|
1351
|
+
}
|
|
1352
|
+
return keys;
|
|
1353
|
+
}
|
|
1354
|
+
catch (error) {
|
|
1355
|
+
if (isFieldPolicyError(error))
|
|
1356
|
+
throw error;
|
|
1357
|
+
throw new FieldPolicyError("invalid_write_payload");
|
|
1358
|
+
}
|
|
1359
|
+
}
|
|
1360
|
+
function writeOwnDataEntries(candidate, maximum) {
|
|
1361
|
+
const keys = writeOwnKeys(candidate, maximum);
|
|
1362
|
+
const entries = [];
|
|
1363
|
+
for (const key of keys) {
|
|
1364
|
+
if (typeof key !== "string") {
|
|
1365
|
+
throw new FieldPolicyError("invalid_write_payload");
|
|
1366
|
+
}
|
|
1367
|
+
const descriptor = ownDataDescriptor(candidate, key);
|
|
1368
|
+
if (!descriptor || !descriptor.enumerable) {
|
|
1369
|
+
throw new FieldPolicyError("invalid_write_payload");
|
|
1370
|
+
}
|
|
1371
|
+
entries.push([key, descriptor]);
|
|
1372
|
+
}
|
|
1373
|
+
return entries;
|
|
1374
|
+
}
|
|
1375
|
+
function cloneJsonSafeWriteValue(candidate, state, depth) {
|
|
1376
|
+
state.nodes += 1;
|
|
1377
|
+
if (depth > exports.MAX_FIELD_VALUE_DEPTH || state.nodes > exports.MAX_FIELD_VALUE_NODES) {
|
|
1378
|
+
throw new FieldPolicyError("traversal_limit_exceeded");
|
|
1379
|
+
}
|
|
1380
|
+
if (candidate === null || typeof candidate === "string" || typeof candidate === "boolean") {
|
|
1381
|
+
return candidate;
|
|
1382
|
+
}
|
|
1383
|
+
if (typeof candidate === "number") {
|
|
1384
|
+
if (!Number.isFinite(candidate)) {
|
|
1385
|
+
throw new FieldPolicyError("invalid_write_payload");
|
|
1386
|
+
}
|
|
1387
|
+
return candidate;
|
|
1388
|
+
}
|
|
1389
|
+
if (typeof candidate !== "object") {
|
|
1390
|
+
throw new FieldPolicyError("invalid_write_payload");
|
|
1391
|
+
}
|
|
1392
|
+
if (node_util_1.types.isProxy(candidate)) {
|
|
1393
|
+
throw new FieldPolicyError("invalid_write_payload");
|
|
1394
|
+
}
|
|
1395
|
+
if (state.seen.has(candidate)) {
|
|
1396
|
+
throw new FieldPolicyError("invalid_write_payload");
|
|
1397
|
+
}
|
|
1398
|
+
state.seen.add(candidate);
|
|
1399
|
+
try {
|
|
1400
|
+
if (Array.isArray(candidate)) {
|
|
1401
|
+
const keys = writeOwnKeys(candidate, exports.MAX_FIELD_ARRAY_LENGTH + 1);
|
|
1402
|
+
const lengthDescriptor = ownDataDescriptor(candidate, "length");
|
|
1403
|
+
const length = lengthDescriptor?.value;
|
|
1404
|
+
if (typeof length !== "number" ||
|
|
1405
|
+
!Number.isSafeInteger(length) ||
|
|
1406
|
+
length < 0 ||
|
|
1407
|
+
length > exports.MAX_FIELD_ARRAY_LENGTH) {
|
|
1408
|
+
throw new FieldPolicyError("traversal_limit_exceeded");
|
|
1409
|
+
}
|
|
1410
|
+
for (const key of keys) {
|
|
1411
|
+
if (typeof key !== "string") {
|
|
1412
|
+
throw new FieldPolicyError("invalid_write_payload");
|
|
1413
|
+
}
|
|
1414
|
+
if (key !== "length" && !/^(?:0|[1-9][0-9]*)$/u.test(key)) {
|
|
1415
|
+
throw new FieldPolicyError("invalid_write_payload");
|
|
1416
|
+
}
|
|
1417
|
+
}
|
|
1418
|
+
const presentKeys = new Set(keys.filter((key) => typeof key === "string"));
|
|
1419
|
+
const output = [];
|
|
1420
|
+
for (let index = 0; index < length; index += 1) {
|
|
1421
|
+
const key = String(index);
|
|
1422
|
+
const descriptor = ownDataDescriptor(candidate, key);
|
|
1423
|
+
if (!descriptor) {
|
|
1424
|
+
if (presentKeys.has(key)) {
|
|
1425
|
+
throw new FieldPolicyError("invalid_write_payload");
|
|
1426
|
+
}
|
|
1427
|
+
output.push(null);
|
|
1428
|
+
}
|
|
1429
|
+
else {
|
|
1430
|
+
output.push(cloneJsonSafeWriteValue(descriptor.value, state, depth + 1));
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
return Object.freeze(output);
|
|
1434
|
+
}
|
|
1435
|
+
assertPlainWriteObject(candidate);
|
|
1436
|
+
const entries = writeOwnDataEntries(candidate, exports.MAX_FIELD_OBJECT_OWN_KEYS);
|
|
1437
|
+
const output = {};
|
|
1438
|
+
for (const [key, descriptor] of entries) {
|
|
1439
|
+
if (isSensitiveFieldName(key)) {
|
|
1440
|
+
throw new FieldPolicyError("invalid_write_payload");
|
|
1441
|
+
}
|
|
1442
|
+
safeDefine(output, key, cloneJsonSafeWriteValue(descriptor.value, state, depth + 1));
|
|
1443
|
+
}
|
|
1444
|
+
return Object.freeze(output);
|
|
1445
|
+
}
|
|
1446
|
+
finally {
|
|
1447
|
+
state.seen.delete(candidate);
|
|
1448
|
+
}
|
|
1449
|
+
}
|
|
1450
|
+
function responseTraversalState() {
|
|
1451
|
+
return { nodes: 0, seen: new WeakSet() };
|
|
1452
|
+
}
|
|
1453
|
+
function responseNodeAllowed(candidate, state, depth) {
|
|
1454
|
+
state.nodes += 1;
|
|
1455
|
+
if (depth > exports.MAX_FIELD_VALUE_DEPTH ||
|
|
1456
|
+
state.nodes > exports.MAX_FIELD_VALUE_NODES ||
|
|
1457
|
+
typeof candidate !== "object" ||
|
|
1458
|
+
candidate === null ||
|
|
1459
|
+
node_util_1.types.isProxy(candidate) ||
|
|
1460
|
+
state.seen.has(candidate)) {
|
|
1461
|
+
return false;
|
|
1462
|
+
}
|
|
1463
|
+
state.seen.add(candidate);
|
|
1464
|
+
return true;
|
|
1465
|
+
}
|
|
1466
|
+
function filterRecordNode(candidate, approved, state, depth) {
|
|
1467
|
+
if (!responseNodeAllowed(candidate, state, depth)) {
|
|
1468
|
+
return null;
|
|
1469
|
+
}
|
|
1470
|
+
try {
|
|
1471
|
+
if (Array.isArray(candidate)) {
|
|
1472
|
+
const output = [];
|
|
1473
|
+
const view = responseArrayView(candidate);
|
|
1474
|
+
if (!view)
|
|
1475
|
+
return output;
|
|
1476
|
+
for (let index = 0; index < view.length; index += 1) {
|
|
1477
|
+
const key = String(index);
|
|
1478
|
+
if (!view.presentKeys.has(key))
|
|
1479
|
+
continue;
|
|
1480
|
+
const descriptor = ownDataDescriptor(candidate, key);
|
|
1481
|
+
if (!descriptor ||
|
|
1482
|
+
typeof descriptor.value !== "object" ||
|
|
1483
|
+
descriptor.value === null ||
|
|
1484
|
+
node_util_1.types.isProxy(descriptor.value) ||
|
|
1485
|
+
Array.isArray(descriptor.value)) {
|
|
1486
|
+
continue;
|
|
1487
|
+
}
|
|
1488
|
+
const filtered = filterRecordNode(descriptor.value, approved, state, depth + 1);
|
|
1489
|
+
if (typeof filtered === "object" && filtered !== null) {
|
|
1490
|
+
output.push(filtered);
|
|
1491
|
+
}
|
|
1492
|
+
}
|
|
1493
|
+
return output;
|
|
1494
|
+
}
|
|
1495
|
+
const keys = responseOwnKeys(candidate, exports.MAX_FIELD_OBJECT_OWN_KEYS);
|
|
1496
|
+
if (!keys)
|
|
1497
|
+
return null;
|
|
1498
|
+
const output = {};
|
|
1499
|
+
for (const key of keys) {
|
|
1500
|
+
if (typeof key !== "string")
|
|
1501
|
+
continue;
|
|
1502
|
+
let field;
|
|
1503
|
+
try {
|
|
1504
|
+
field = normalizeFieldName(key);
|
|
1505
|
+
}
|
|
1506
|
+
catch {
|
|
1507
|
+
continue;
|
|
1508
|
+
}
|
|
1509
|
+
if (!approved.has(field) || isSensitiveFieldName(field))
|
|
1510
|
+
continue;
|
|
1511
|
+
const descriptor = ownDataDescriptor(candidate, key);
|
|
1512
|
+
if (!descriptor || !descriptor.enumerable)
|
|
1513
|
+
continue;
|
|
1514
|
+
safeDefine(output, field, sanitizeNestedValue(descriptor.value, state, depth + 1));
|
|
1515
|
+
}
|
|
1516
|
+
return output;
|
|
1517
|
+
}
|
|
1518
|
+
finally {
|
|
1519
|
+
state.seen.delete(candidate);
|
|
1520
|
+
}
|
|
1521
|
+
}
|
|
1522
|
+
function sanitizeNestedValue(candidate, state, depth) {
|
|
1523
|
+
if (!responseNodeAllowed(candidate, state, depth)) {
|
|
1524
|
+
return sanitizePrimitive(candidate);
|
|
1525
|
+
}
|
|
1526
|
+
try {
|
|
1527
|
+
if (Array.isArray(candidate)) {
|
|
1528
|
+
const view = responseArrayView(candidate);
|
|
1529
|
+
if (!view)
|
|
1530
|
+
return null;
|
|
1531
|
+
const output = [];
|
|
1532
|
+
for (let index = 0; index < view.length; index += 1) {
|
|
1533
|
+
const key = String(index);
|
|
1534
|
+
const descriptor = view.presentKeys.has(key)
|
|
1535
|
+
? ownDataDescriptor(candidate, key)
|
|
1536
|
+
: undefined;
|
|
1537
|
+
output.push(descriptor
|
|
1538
|
+
? sanitizeNestedValue(descriptor.value, state, depth + 1)
|
|
1539
|
+
: null);
|
|
1540
|
+
}
|
|
1541
|
+
return output;
|
|
1542
|
+
}
|
|
1543
|
+
const keys = responseOwnKeys(candidate, exports.MAX_FIELD_OBJECT_OWN_KEYS);
|
|
1544
|
+
if (!keys)
|
|
1545
|
+
return null;
|
|
1546
|
+
const output = {};
|
|
1547
|
+
for (const key of keys) {
|
|
1548
|
+
if (typeof key !== "string")
|
|
1549
|
+
continue;
|
|
1550
|
+
if (isSensitiveFieldName(key))
|
|
1551
|
+
continue;
|
|
1552
|
+
const descriptor = ownDataDescriptor(candidate, key);
|
|
1553
|
+
if (!descriptor || !descriptor.enumerable)
|
|
1554
|
+
continue;
|
|
1555
|
+
safeDefine(output, key, sanitizeNestedValue(descriptor.value, state, depth + 1));
|
|
1556
|
+
}
|
|
1557
|
+
return output;
|
|
1558
|
+
}
|
|
1559
|
+
finally {
|
|
1560
|
+
state.seen.delete(candidate);
|
|
1561
|
+
}
|
|
1562
|
+
}
|
|
1563
|
+
function filterAggregateNode(candidate, approved, state, depth) {
|
|
1564
|
+
if (!responseNodeAllowed(candidate, state, depth)) {
|
|
1565
|
+
return sanitizePrimitive(candidate);
|
|
1566
|
+
}
|
|
1567
|
+
try {
|
|
1568
|
+
if (Array.isArray(candidate)) {
|
|
1569
|
+
const view = responseArrayView(candidate);
|
|
1570
|
+
if (!view)
|
|
1571
|
+
return [];
|
|
1572
|
+
const output = [];
|
|
1573
|
+
for (let index = 0; index < view.length; index += 1) {
|
|
1574
|
+
const key = String(index);
|
|
1575
|
+
const descriptor = view.presentKeys.has(key)
|
|
1576
|
+
? ownDataDescriptor(candidate, key)
|
|
1577
|
+
: undefined;
|
|
1578
|
+
const value = descriptor
|
|
1579
|
+
? filterAggregateNode(descriptor.value, approved, state, depth + 1)
|
|
1580
|
+
: null;
|
|
1581
|
+
if (value !== null)
|
|
1582
|
+
output.push(value);
|
|
1583
|
+
}
|
|
1584
|
+
return output;
|
|
1585
|
+
}
|
|
1586
|
+
const keys = responseOwnKeys(candidate, exports.MAX_FIELD_OBJECT_OWN_KEYS);
|
|
1587
|
+
if (!keys)
|
|
1588
|
+
return null;
|
|
1589
|
+
const declaredField = keys.includes("field")
|
|
1590
|
+
? ownDataDescriptor(candidate, "field")
|
|
1591
|
+
: undefined;
|
|
1592
|
+
if (declaredField) {
|
|
1593
|
+
try {
|
|
1594
|
+
const field = normalizeFieldName(declaredField.value);
|
|
1595
|
+
if (!approved.has(field) || isSensitiveFieldName(field))
|
|
1596
|
+
return null;
|
|
1597
|
+
}
|
|
1598
|
+
catch {
|
|
1599
|
+
return null;
|
|
1600
|
+
}
|
|
1601
|
+
}
|
|
1602
|
+
const output = {};
|
|
1603
|
+
for (const key of keys) {
|
|
1604
|
+
if (typeof key !== "string")
|
|
1605
|
+
continue;
|
|
1606
|
+
let normalized;
|
|
1607
|
+
try {
|
|
1608
|
+
normalized = normalizeFieldName(key);
|
|
1609
|
+
}
|
|
1610
|
+
catch {
|
|
1611
|
+
continue;
|
|
1612
|
+
}
|
|
1613
|
+
if (isSensitiveFieldName(normalized) ||
|
|
1614
|
+
(!AGGREGATE_ENVELOPE_FIELDS.has(normalized) && !approved.has(normalized))) {
|
|
1615
|
+
continue;
|
|
1616
|
+
}
|
|
1617
|
+
const descriptor = ownDataDescriptor(candidate, key);
|
|
1618
|
+
if (!descriptor || !descriptor.enumerable)
|
|
1619
|
+
continue;
|
|
1620
|
+
safeDefine(output, normalized, filterAggregateNode(descriptor.value, approved, state, depth + 1));
|
|
1621
|
+
}
|
|
1622
|
+
return output;
|
|
1623
|
+
}
|
|
1624
|
+
finally {
|
|
1625
|
+
state.seen.delete(candidate);
|
|
1626
|
+
}
|
|
1627
|
+
}
|
|
1628
|
+
function responseOwnKeys(candidate, maximum) {
|
|
1629
|
+
if (node_util_1.types.isProxy(candidate))
|
|
1630
|
+
return undefined;
|
|
1631
|
+
try {
|
|
1632
|
+
const keys = Reflect.ownKeys(candidate);
|
|
1633
|
+
return keys.length <= maximum ? keys : undefined;
|
|
1634
|
+
}
|
|
1635
|
+
catch {
|
|
1636
|
+
return undefined;
|
|
1637
|
+
}
|
|
1638
|
+
}
|
|
1639
|
+
function responseArrayView(candidate) {
|
|
1640
|
+
const keys = responseOwnKeys(candidate, exports.MAX_FIELD_ARRAY_LENGTH + 1);
|
|
1641
|
+
if (!keys)
|
|
1642
|
+
return undefined;
|
|
1643
|
+
const lengthDescriptor = ownDataDescriptor(candidate, "length");
|
|
1644
|
+
const rawLength = lengthDescriptor?.value;
|
|
1645
|
+
if (typeof rawLength !== "number" ||
|
|
1646
|
+
!Number.isSafeInteger(rawLength) ||
|
|
1647
|
+
rawLength < 0) {
|
|
1648
|
+
return undefined;
|
|
1649
|
+
}
|
|
1650
|
+
return {
|
|
1651
|
+
length: Math.min(rawLength, exports.MAX_FIELD_ARRAY_LENGTH),
|
|
1652
|
+
presentKeys: new Set(keys.filter((key) => typeof key === "string" && key !== "length")),
|
|
1653
|
+
};
|
|
1654
|
+
}
|
|
1655
|
+
function sanitizePrimitive(candidate) {
|
|
1656
|
+
if (candidate === null ||
|
|
1657
|
+
typeof candidate === "string" ||
|
|
1658
|
+
typeof candidate === "boolean") {
|
|
1659
|
+
return candidate;
|
|
1660
|
+
}
|
|
1661
|
+
return typeof candidate === "number" && Number.isFinite(candidate)
|
|
1662
|
+
? candidate
|
|
1663
|
+
: null;
|
|
1664
|
+
}
|
|
1665
|
+
function safeOwnDataValue(candidate, key) {
|
|
1666
|
+
if (node_util_1.types.isProxy(candidate))
|
|
1667
|
+
return undefined;
|
|
1668
|
+
try {
|
|
1669
|
+
const descriptor = Object.getOwnPropertyDescriptor(candidate, key);
|
|
1670
|
+
return descriptor && "value" in descriptor ? descriptor.value : undefined;
|
|
1671
|
+
}
|
|
1672
|
+
catch {
|
|
1673
|
+
return undefined;
|
|
1674
|
+
}
|
|
1675
|
+
}
|
|
1676
|
+
function safeDefine(target, key, value) {
|
|
1677
|
+
Object.defineProperty(target, key, {
|
|
1678
|
+
value,
|
|
1679
|
+
enumerable: true,
|
|
1680
|
+
configurable: true,
|
|
1681
|
+
writable: true,
|
|
1682
|
+
});
|
|
1683
|
+
}
|
|
1684
|
+
//# sourceMappingURL=field-policy.js.map
|