@silmaril-security/sdk 0.10.0 → 0.10.2
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/CHANGELOG.md +18 -0
- package/README.md +2 -2
- package/dist/index.cjs +35 -22
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +35 -22
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1438,6 +1438,10 @@ var OUTCOME_DESCRIPTIONS = {
|
|
|
1438
1438
|
};
|
|
1439
1439
|
var PRIMARY_OUTCOME_SET = new Set(PRIMARY_OUTCOMES);
|
|
1440
1440
|
var HARMFUL_OUTCOME_SET = new Set(HARMFUL_OUTCOMES);
|
|
1441
|
+
var PYTHON_NONBLANK = /[^\p{White_Space}\u001c-\u001f]/u;
|
|
1442
|
+
function isNonblankOutcomeLabel(value) {
|
|
1443
|
+
return typeof value === "string" && PYTHON_NONBLANK.test(value);
|
|
1444
|
+
}
|
|
1441
1445
|
function isPrimaryOutcome(value) {
|
|
1442
1446
|
return typeof value === "string" && PRIMARY_OUTCOME_SET.has(value);
|
|
1443
1447
|
}
|
|
@@ -1445,18 +1449,18 @@ function isHarmfulOutcome(value) {
|
|
|
1445
1449
|
return typeof value === "string" && HARMFUL_OUTCOME_SET.has(value);
|
|
1446
1450
|
}
|
|
1447
1451
|
function normalizePrimaryOutcome(value, fieldName = "primary_outcome") {
|
|
1448
|
-
if (
|
|
1452
|
+
if (!isNonblankOutcomeLabel(value)) {
|
|
1449
1453
|
throw new Error(`Firewall: invalid ${fieldName} ${JSON.stringify(value)}`);
|
|
1450
1454
|
}
|
|
1451
1455
|
return isPrimaryOutcome(value) ? value : value;
|
|
1452
1456
|
}
|
|
1453
1457
|
function normalizeHarmfulOutcome(value, fieldName = "outcome") {
|
|
1454
|
-
if (
|
|
1458
|
+
if (!isNonblankOutcomeLabel(value) || value === Outcome.Benign) {
|
|
1455
1459
|
throw new Error(`Firewall: invalid ${fieldName} ${JSON.stringify(value)}`);
|
|
1456
1460
|
}
|
|
1457
1461
|
return isHarmfulOutcome(value) ? value : value;
|
|
1458
1462
|
}
|
|
1459
|
-
function
|
|
1463
|
+
function normalizeNumericOutcomeMap(values, fieldName, accepts) {
|
|
1460
1464
|
if (values === void 0 || values === null) {
|
|
1461
1465
|
return void 0;
|
|
1462
1466
|
}
|
|
@@ -1466,24 +1470,23 @@ function normalizeHarmfulOutcomeMap(values, fieldName) {
|
|
|
1466
1470
|
const out = {};
|
|
1467
1471
|
for (const [key, value] of Object.entries(values)) {
|
|
1468
1472
|
const normalizedKey = normalizeHarmfulOutcome(key, `${fieldName} key`);
|
|
1469
|
-
if (typeof value !== "number") {
|
|
1473
|
+
if (typeof value !== "number" || !accepts(value)) {
|
|
1470
1474
|
throw new Error(`Firewall: invalid ${fieldName} value for ${JSON.stringify(key)}`);
|
|
1471
1475
|
}
|
|
1472
|
-
out
|
|
1476
|
+
Object.defineProperty(out, normalizedKey, {
|
|
1477
|
+
value,
|
|
1478
|
+
enumerable: true,
|
|
1479
|
+
configurable: true,
|
|
1480
|
+
writable: true
|
|
1481
|
+
});
|
|
1473
1482
|
}
|
|
1474
1483
|
return Object.freeze(out);
|
|
1475
1484
|
}
|
|
1485
|
+
function normalizeHarmfulOutcomeMap(values, fieldName) {
|
|
1486
|
+
return normalizeNumericOutcomeMap(values, fieldName, (value) => value >= 0 && value <= 1);
|
|
1487
|
+
}
|
|
1476
1488
|
function normalizeHarmfulOutcomeCountMap(values, fieldName) {
|
|
1477
|
-
|
|
1478
|
-
if (out === void 0) {
|
|
1479
|
-
return void 0;
|
|
1480
|
-
}
|
|
1481
|
-
for (const [key, value] of Object.entries(out)) {
|
|
1482
|
-
if (typeof value !== "number" || !Number.isFinite(value) || !Number.isInteger(value) || value < 0) {
|
|
1483
|
-
throw new Error(`Firewall: invalid ${fieldName} value for ${JSON.stringify(key)}`);
|
|
1484
|
-
}
|
|
1485
|
-
}
|
|
1486
|
-
return out;
|
|
1489
|
+
return normalizeNumericOutcomeMap(values, fieldName, (value) => Number.isInteger(value) && value >= 0);
|
|
1487
1490
|
}
|
|
1488
1491
|
|
|
1489
1492
|
// src/firewall.ts
|
|
@@ -1515,8 +1518,16 @@ function sanitizeText(text) {
|
|
|
1515
1518
|
return out;
|
|
1516
1519
|
}
|
|
1517
1520
|
|
|
1521
|
+
// src/validation.ts
|
|
1522
|
+
function validateThreshold(name, value) {
|
|
1523
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value < 0 || value > 1) {
|
|
1524
|
+
throw new Error(`Firewall: ${name} must be a finite number between 0 and 1`);
|
|
1525
|
+
}
|
|
1526
|
+
return value;
|
|
1527
|
+
}
|
|
1528
|
+
|
|
1518
1529
|
// src/firewall.ts
|
|
1519
|
-
var SDK_VERSION = "0.10.
|
|
1530
|
+
var SDK_VERSION = "0.10.2";
|
|
1520
1531
|
var DEFAULT_TIMEOUT_MS = 1e4;
|
|
1521
1532
|
var DEFAULT_MAX_RETRIES = 5;
|
|
1522
1533
|
var MAX_BACKOFF_SECONDS = 30;
|
|
@@ -1560,8 +1571,8 @@ function blockResultFromResponse(data, requestedMode, requestedReview) {
|
|
|
1560
1571
|
}
|
|
1561
1572
|
const result = {
|
|
1562
1573
|
prediction: data.prediction,
|
|
1563
|
-
score:
|
|
1564
|
-
threshold:
|
|
1574
|
+
score: validateThreshold("response score", data.score),
|
|
1575
|
+
threshold: validateThreshold("response threshold", data.threshold)
|
|
1565
1576
|
};
|
|
1566
1577
|
if (requestedReview !== void 0) {
|
|
1567
1578
|
result.review = requestedReview;
|
|
@@ -1723,13 +1734,15 @@ function governanceDecisionFromResponse(value) {
|
|
|
1723
1734
|
if (value.rule_id !== void 0 && value.rule_id !== null && typeof value.rule_id !== "string") {
|
|
1724
1735
|
throw new Error("Firewall: response governance rule_id must be a string when provided");
|
|
1725
1736
|
}
|
|
1726
|
-
if (value.identity_revision !== void 0 && value.identity_revision !== null && typeof value.identity_revision !== "string") {
|
|
1737
|
+
if (value.identity_revision !== void 0 && value.identity_revision !== null && (typeof value.identity_revision !== "string" || !value.identity_revision.trim())) {
|
|
1727
1738
|
throw new Error(
|
|
1728
|
-
"Firewall: response governance identity_revision must be a string when provided"
|
|
1739
|
+
"Firewall: response governance identity_revision must be a non-empty string when provided"
|
|
1729
1740
|
);
|
|
1730
1741
|
}
|
|
1731
|
-
if (value.reason !== void 0 && value.reason !== null && typeof value.reason !== "string") {
|
|
1732
|
-
throw new Error(
|
|
1742
|
+
if (value.reason !== void 0 && value.reason !== null && (typeof value.reason !== "string" || !value.reason.trim())) {
|
|
1743
|
+
throw new Error(
|
|
1744
|
+
"Firewall: response governance reason must be a non-empty string when provided"
|
|
1745
|
+
);
|
|
1733
1746
|
}
|
|
1734
1747
|
const resource = value.resource === void 0 || value.resource === null ? void 0 : governanceResourceFromResponse(value.resource);
|
|
1735
1748
|
return Object.freeze({
|