@o3co/auth.policy-verifier.builtins 0.2.0 → 0.3.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/README.ja.md +101 -0
- package/README.md +101 -0
- package/dist/__tests__/rules/AttrLiteralCompare.test.d.mts +2 -0
- package/dist/__tests__/rules/AttrLiteralCompare.test.d.mts.map +1 -0
- package/dist/__tests__/rules/AttrLiteralCompare.test.mjs +253 -0
- package/dist/__tests__/rules/AttrLiteralEqual.test.d.mts +2 -0
- package/dist/__tests__/rules/AttrLiteralEqual.test.d.mts.map +1 -0
- package/dist/__tests__/rules/AttrLiteralEqual.test.mjs +138 -0
- package/dist/__tests__/rules/AttrLiteralIn.test.d.mts +2 -0
- package/dist/__tests__/rules/AttrLiteralIn.test.d.mts.map +1 -0
- package/dist/__tests__/rules/AttrLiteralIn.test.mjs +160 -0
- package/dist/__tests__/rules/AttrLiteralNotEqual.test.d.mts +2 -0
- package/dist/__tests__/rules/AttrLiteralNotEqual.test.d.mts.map +1 -0
- package/dist/__tests__/rules/AttrLiteralNotEqual.test.mjs +157 -0
- package/dist/__tests__/rules/AttrLiteralNotIn.test.d.mts +2 -0
- package/dist/__tests__/rules/AttrLiteralNotIn.test.d.mts.map +1 -0
- package/dist/__tests__/rules/AttrLiteralNotIn.test.mjs +168 -0
- package/dist/__tests__/rules/AttrMatchRule.test.mjs +8 -0
- package/dist/__tests__/rules/AttrPairCompare.test.d.mts +2 -0
- package/dist/__tests__/rules/AttrPairCompare.test.d.mts.map +1 -0
- package/dist/__tests__/rules/AttrPairCompare.test.mjs +319 -0
- package/dist/__tests__/rules/AttrPairEqual.test.d.mts +2 -0
- package/dist/__tests__/rules/AttrPairEqual.test.d.mts.map +1 -0
- package/dist/__tests__/rules/AttrPairEqual.test.mjs +155 -0
- package/dist/__tests__/rules/AttrPairNotEqual.test.d.mts +2 -0
- package/dist/__tests__/rules/AttrPairNotEqual.test.d.mts.map +1 -0
- package/dist/__tests__/rules/AttrPairNotEqual.test.mjs +155 -0
- package/dist/__tests__/rules/_sharedValidation.test.d.mts +2 -0
- package/dist/__tests__/rules/_sharedValidation.test.d.mts.map +1 -0
- package/dist/__tests__/rules/_sharedValidation.test.mjs +325 -0
- package/dist/index.d.mts +8 -0
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +8 -0
- package/dist/rules/AttrLiteralCompare.d.mts +31 -0
- package/dist/rules/AttrLiteralCompare.d.mts.map +1 -0
- package/dist/rules/AttrLiteralCompare.mjs +36 -0
- package/dist/rules/AttrLiteralEqual.d.mts +37 -0
- package/dist/rules/AttrLiteralEqual.d.mts.map +1 -0
- package/dist/rules/AttrLiteralEqual.mjs +43 -0
- package/dist/rules/AttrLiteralIn.d.mts +38 -0
- package/dist/rules/AttrLiteralIn.d.mts.map +1 -0
- package/dist/rules/AttrLiteralIn.mjs +46 -0
- package/dist/rules/AttrLiteralNotEqual.d.mts +36 -0
- package/dist/rules/AttrLiteralNotEqual.d.mts.map +1 -0
- package/dist/rules/AttrLiteralNotEqual.mjs +42 -0
- package/dist/rules/AttrLiteralNotIn.d.mts +33 -0
- package/dist/rules/AttrLiteralNotIn.d.mts.map +1 -0
- package/dist/rules/AttrLiteralNotIn.mjs +41 -0
- package/dist/rules/AttrMatchRule.d.mts +7 -34
- package/dist/rules/AttrMatchRule.d.mts.map +1 -1
- package/dist/rules/AttrMatchRule.mjs +7 -32
- package/dist/rules/AttrPairCompare.d.mts +35 -0
- package/dist/rules/AttrPairCompare.d.mts.map +1 -0
- package/dist/rules/AttrPairCompare.mjs +43 -0
- package/dist/rules/AttrPairEqual.d.mts +35 -0
- package/dist/rules/AttrPairEqual.d.mts.map +1 -0
- package/dist/rules/AttrPairEqual.mjs +44 -0
- package/dist/rules/AttrPairNotEqual.d.mts +34 -0
- package/dist/rules/AttrPairNotEqual.d.mts.map +1 -0
- package/dist/rules/AttrPairNotEqual.mjs +43 -0
- package/dist/rules/_sharedValidation.d.mts +87 -0
- package/dist/rules/_sharedValidation.d.mts.map +1 -0
- package/dist/rules/_sharedValidation.mjs +189 -0
- package/package.json +5 -2
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
/**
|
|
3
|
+
* Returns a human-readable type label for use in error messages.
|
|
4
|
+
* Distinguishes null and undefined from other types instead of returning
|
|
5
|
+
* "object" or "undefined" from typeof.
|
|
6
|
+
*/
|
|
7
|
+
function describeType(value) {
|
|
8
|
+
if (value === null)
|
|
9
|
+
return "null";
|
|
10
|
+
if (value === undefined)
|
|
11
|
+
return "undefined";
|
|
12
|
+
return typeof value;
|
|
13
|
+
}
|
|
14
|
+
export const COMPARE_OPS = ["lt", "le", "gt", "ge"];
|
|
15
|
+
/**
|
|
16
|
+
* Validates that a config field used as an attribute name is a non-empty string
|
|
17
|
+
* that does not contain the `:` separator character used in derived `ruleType`
|
|
18
|
+
* strings. Allowing `:` would let distinct configs collide on the same
|
|
19
|
+
* `ruleType` (e.g. `(a="x:y", b="z")` and `(a="x", b="y:z")` both produce
|
|
20
|
+
* `attr_pair_equal:x:y:z`), which the evaluator would silently OR together
|
|
21
|
+
* and weaken authorization.
|
|
22
|
+
* @param className - The rule class name, used in error messages.
|
|
23
|
+
* @param fieldName - The config field name ("a" or "b"), used in error messages.
|
|
24
|
+
* @param value - The raw value to validate.
|
|
25
|
+
* @returns The validated non-empty string.
|
|
26
|
+
* @throws {Error} If value is not a non-empty string, or contains `:`.
|
|
27
|
+
*/
|
|
28
|
+
export function requireAttrName(className, fieldName, value) {
|
|
29
|
+
if (typeof value !== "string") {
|
|
30
|
+
throw new Error(`${className}: '${fieldName}' must be a non-empty string, got ${describeType(value)}`);
|
|
31
|
+
}
|
|
32
|
+
if (value.length === 0) {
|
|
33
|
+
throw new Error(`${className}: '${fieldName}' must be a non-empty string, got empty string`);
|
|
34
|
+
}
|
|
35
|
+
if (value.includes(":")) {
|
|
36
|
+
throw new Error(`${className}: '${fieldName}' must not contain ':' (reserved as ruleType separator), got '${value}'`);
|
|
37
|
+
}
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Validates that a config field used as a literal comparison value is a
|
|
42
|
+
* string, number, or boolean (i.e. a LiteralValue). NaN is rejected because
|
|
43
|
+
* its comparison semantics (`NaN !== NaN` is always true, `NaN === NaN` is
|
|
44
|
+
* always false) would silently invert Equal/NotEqual rules — a NotEqual(v=NaN)
|
|
45
|
+
* would always pass against any numeric attribute, weakening authorization.
|
|
46
|
+
* @param className - The rule class name, used in error messages.
|
|
47
|
+
* @param fieldName - The config field name ("v"), used in error messages.
|
|
48
|
+
* @param value - The raw value to validate.
|
|
49
|
+
* @returns The validated LiteralValue.
|
|
50
|
+
* @throws {Error} If value is null, undefined, of any other type, or NaN.
|
|
51
|
+
*/
|
|
52
|
+
export function requireLiteralValue(className, fieldName, value) {
|
|
53
|
+
if (typeof value === "string" || typeof value === "boolean") {
|
|
54
|
+
return value;
|
|
55
|
+
}
|
|
56
|
+
if (typeof value === "number") {
|
|
57
|
+
if (Number.isNaN(value)) {
|
|
58
|
+
throw new Error(`${className}: '${fieldName}' must not be NaN`);
|
|
59
|
+
}
|
|
60
|
+
return value;
|
|
61
|
+
}
|
|
62
|
+
throw new Error(`${className}: '${fieldName}' must be string | number | boolean, got ${describeType(value)}`);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Validates a numeric comparison value. Rejects NaN; accepts Infinity/-Infinity.
|
|
66
|
+
* @param className - The rule class name, used in error messages.
|
|
67
|
+
* @param fieldName - The config field name ("v"), used in error messages.
|
|
68
|
+
* @param value - The raw value to validate.
|
|
69
|
+
* @returns The validated number.
|
|
70
|
+
* @throws {Error} If value is not a number, or if value is NaN.
|
|
71
|
+
*/
|
|
72
|
+
export function requireNumber(className, fieldName, value) {
|
|
73
|
+
if (typeof value !== "number") {
|
|
74
|
+
throw new Error(`${className}: '${fieldName}' must be a number, got ${describeType(value)}`);
|
|
75
|
+
}
|
|
76
|
+
if (Number.isNaN(value)) {
|
|
77
|
+
throw new Error(`${className}: '${fieldName}' must not be NaN`);
|
|
78
|
+
}
|
|
79
|
+
return value;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Validates that a value is a non-empty, homogeneous array of LiteralValues
|
|
83
|
+
* (no null/undefined elements, no NaN elements, all elements the same typeof).
|
|
84
|
+
* @param className - The rule class name, used in error messages.
|
|
85
|
+
* @param value - The raw value to validate.
|
|
86
|
+
* @returns The validated LiteralValue array.
|
|
87
|
+
* @throws {Error} If not an array, empty, contains invalid elements, has mixed types, or contains NaN.
|
|
88
|
+
*/
|
|
89
|
+
export function requireHomogeneousLiteralArray(className, value) {
|
|
90
|
+
if (!Array.isArray(value)) {
|
|
91
|
+
throw new Error(`${className}: 'values' must be a non-empty array of string | number | boolean, got ${describeType(value)}`);
|
|
92
|
+
}
|
|
93
|
+
if (value.length === 0) {
|
|
94
|
+
throw new Error(`${className}: 'values' must be a non-empty array`);
|
|
95
|
+
}
|
|
96
|
+
const seenTypes = new Set();
|
|
97
|
+
for (const element of value) {
|
|
98
|
+
if (element === null || element === undefined) {
|
|
99
|
+
throw new Error(`${className}: 'values' elements must be string | number | boolean, got ${describeType(element)}`);
|
|
100
|
+
}
|
|
101
|
+
const t = typeof element;
|
|
102
|
+
if (t !== "string" && t !== "number" && t !== "boolean") {
|
|
103
|
+
throw new Error(`${className}: 'values' elements must be string | number | boolean, got ${t}`);
|
|
104
|
+
}
|
|
105
|
+
if (t === "number" && Number.isNaN(element)) {
|
|
106
|
+
throw new Error(`${className}: 'values' must not contain NaN`);
|
|
107
|
+
}
|
|
108
|
+
seenTypes.add(t);
|
|
109
|
+
}
|
|
110
|
+
if (seenTypes.size > 1) {
|
|
111
|
+
const typeList = [...seenTypes].join(", ");
|
|
112
|
+
throw new Error(`${className}: 'values' must be a homogeneous array of string | number | boolean; got mixed types [${typeList}]`);
|
|
113
|
+
}
|
|
114
|
+
return value;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Validates that a value is one of the allowed CompareOp strings.
|
|
118
|
+
* @param className - The rule class name, used in error messages.
|
|
119
|
+
* @param value - The raw value to validate.
|
|
120
|
+
* @returns The validated CompareOp.
|
|
121
|
+
* @throws {Error} If value is not a string or not one of the allowed operators.
|
|
122
|
+
*/
|
|
123
|
+
export function requireCompareOp(className, value) {
|
|
124
|
+
if (typeof value !== "string") {
|
|
125
|
+
throw new Error(`${className}: 'op' must be one of ${COMPARE_OPS.join(" | ")}, got ${describeType(value)}`);
|
|
126
|
+
}
|
|
127
|
+
if (!COMPARE_OPS.includes(value)) {
|
|
128
|
+
throw new Error(`${className}: 'op' must be one of ${COMPARE_OPS.join(" | ")}, got '${value}'`);
|
|
129
|
+
}
|
|
130
|
+
return value;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Validates an optional group string. Returns undefined when value is undefined;
|
|
134
|
+
* throws when value is a non-string or an empty string; otherwise returns the
|
|
135
|
+
* non-empty string.
|
|
136
|
+
* @param className - The rule class name, used in error messages.
|
|
137
|
+
* @param value - The raw value to validate.
|
|
138
|
+
* @returns The validated non-empty string, or undefined.
|
|
139
|
+
* @throws {Error} If value is a non-string or an empty string.
|
|
140
|
+
*/
|
|
141
|
+
export function requireOptionalGroup(className, value) {
|
|
142
|
+
if (value === undefined) {
|
|
143
|
+
return undefined;
|
|
144
|
+
}
|
|
145
|
+
if (typeof value !== "string") {
|
|
146
|
+
throw new Error(`${className}: 'group' must be a non-empty string or undefined, got ${describeType(value)}`);
|
|
147
|
+
}
|
|
148
|
+
if (value.length === 0) {
|
|
149
|
+
throw new Error(`${className}: 'group' must be a non-empty string or undefined, got empty string`);
|
|
150
|
+
}
|
|
151
|
+
return value;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Computes a stable cache/dedup key for an array of LiteralValues.
|
|
155
|
+
* Format: `{type}:{count}:{hashPrefix}` — SHA-256 over a canonical serialization
|
|
156
|
+
* of `values`. The canonical form first deduplicates `values` (to mirror the
|
|
157
|
+
* Set-based semantics of AttrLiteralIn / AttrLiteralNotIn — duplicates do not
|
|
158
|
+
* change the rule's behavior, so they must not change its `ruleType`), then
|
|
159
|
+
* applies `JSON.stringify(String(v))` to each remaining element before sorting
|
|
160
|
+
* and joining with `,`. The per-element quoting prevents separator collisions
|
|
161
|
+
* across different arrays (e.g. `["a,b", "c"]` vs `["a", "b,c"]`).
|
|
162
|
+
* The `{count}` segment reflects the post-dedup element count.
|
|
163
|
+
* Caller must guarantee values is a non-empty homogeneous LiteralValue[].
|
|
164
|
+
*/
|
|
165
|
+
export function computeValuesKey(values) {
|
|
166
|
+
const elementType = typeof values[0];
|
|
167
|
+
const unique = [...new Set(values)];
|
|
168
|
+
const joined = unique
|
|
169
|
+
.map((v) => JSON.stringify(String(v)))
|
|
170
|
+
.sort()
|
|
171
|
+
.join(",");
|
|
172
|
+
const hash = createHash("sha256").update(joined).digest("hex").slice(0, 8);
|
|
173
|
+
return `${elementType}:${unique.length}:${hash}`;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Applies a numeric comparison operator between left and right.
|
|
177
|
+
*/
|
|
178
|
+
export function applyCompare(op, left, right) {
|
|
179
|
+
switch (op) {
|
|
180
|
+
case "lt":
|
|
181
|
+
return left < right;
|
|
182
|
+
case "le":
|
|
183
|
+
return left <= right;
|
|
184
|
+
case "gt":
|
|
185
|
+
return left > right;
|
|
186
|
+
case "ge":
|
|
187
|
+
return left >= right;
|
|
188
|
+
}
|
|
189
|
+
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@o3co/auth.policy-verifier.builtins",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=22.0.0"
|
|
8
|
+
},
|
|
6
9
|
"main": "./dist/index.mjs",
|
|
7
10
|
"types": "./dist/index.d.mts",
|
|
8
11
|
"exports": {
|
|
@@ -28,7 +31,7 @@
|
|
|
28
31
|
}
|
|
29
32
|
},
|
|
30
33
|
"dependencies": {
|
|
31
|
-
"@o3co/auth.policy-verifier.core": "0.
|
|
34
|
+
"@o3co/auth.policy-verifier.core": "0.3.0"
|
|
32
35
|
},
|
|
33
36
|
"devDependencies": {
|
|
34
37
|
"@types/node": "^25.1.0",
|