securl 1.9.0 → 1.11.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/CHANGELOG.md CHANGED
@@ -6,9 +6,38 @@ The format is based on Keep a Changelog and this package follows Semantic Versio
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [1.11.0] - 2026-06-20
10
+
11
+ ### Added
12
+ - Added a bounded declarative observation policy engine, maintained baseline policy, validation helper, and `securl/observation-policy` export.
13
+ - Added `diffObservationLedgers()` and `securl/observation-drift` for deterministic observation-level regression and improvement classification.
14
+
15
+ ## [1.10.0] - 2026-06-20
16
+
17
+ ### Added
18
+ - Added `buildObservationLedger()` and the `securl/observations` package export for stable, source-aware posture observations.
19
+ - Added `observationLedger` to completed analysis results with deterministic IDs, confidence, status, and freshness metadata.
20
+
21
+ ## [1.9.0] - 2026-06-15
22
+
23
+ ### Added
24
+ - Added `scanLiveCertificate()` and the `securl/live-certificate` package export for bounded TLS handshake-only certificate reads.
25
+ - Added certificate chain, protocol, key-strength, and expiry metadata for lightweight certificate clients.
26
+
27
+ ## [1.8.0] - 2026-06-15
28
+
29
+ ### Added
30
+ - Added `buildActionPlan()` and the `securl/action-plan` package export for prioritized owner, effort, and impact-ranked remediation actions.
31
+
32
+ ## [1.7.0] - 2026-06-15
33
+
9
34
  ### Added
10
35
  - Added `buildVendorExposureBrief()` for compact vendor and supply-chain exposure summaries covering visible third-party providers, data-flow categories, SRI gaps, priority vendors, and next actions.
11
36
  - Added `vendorExposure` to analysis results and the `securl/vendor-exposure` package export for SDK consumers.
37
+
38
+ ## [1.6.0] - 2026-06-15
39
+
40
+ ### Added
12
41
  - Added `buildExposureBrief()` for compact outside-observer action briefs covering public entry points, sensitive exposures, trust gaps, abuse indicators, third-party risk, AI surface signals, and next actions.
13
42
  - Added `exposureBrief` to analysis results and the `securl/exposure-brief` package export for SDK consumers.
14
43
 
package/README.md CHANGED
@@ -211,7 +211,27 @@ console.log({
211
211
  });
212
212
  ```
213
213
 
214
- ### 6. Evidence-backed remediation plans
214
+ ### 8. Machine-readable observation ledger
215
+
216
+ Version `1.10.0+` adds stable posture observations for monitoring, inventory, policy, and future SaaS integrations. Each observation records what was seen, whether it was observed, inferred, missing, or unavailable, its confidence and source, and when that evidence should be refreshed.
217
+
218
+ ```ts
219
+ import { analyzeUrl } from "securl";
220
+ import { buildObservationLedger } from "securl/observations";
221
+
222
+ const result = await analyzeUrl("https://example.com");
223
+ const ledger = result.observationLedger ?? buildObservationLedger(result);
224
+
225
+ console.log(ledger.summary, ledger.observations);
226
+ ```
227
+
228
+ Observation IDs are deterministic across scans for the same subject and signal, making the ledger suitable for change detection without exposing backend job metadata.
229
+
230
+ Version `1.11.0+` adds `diffObservationLedgers(current, previous)` from `securl/observation-drift` to classify observation-level regressions, improvements, and neutral changes.
231
+
232
+ Use `evaluateObservationPolicy({ ledger, drift, policy })` from `securl/observation-policy` to apply bounded declarative rules. Rules can select an exact observation kind, kind prefix, or category, then assert equality, membership, or numeric thresholds against current observations or changes. `DEFAULT_OBSERVATION_POLICY` provides a maintained baseline for certificate validity/window, HSTS, CSP, DMARC, and critical regressions.
233
+
234
+ ### 9. Evidence-backed remediation plans
215
235
 
216
236
  Version `1.4.0+` includes a remediation plan helper that turns score drivers and findings into prioritized, owner-aware fix guidance. Findings can also carry structured evidence references so clients can show why a finding was raised.
217
237
 
@@ -322,6 +342,9 @@ Primary exports:
322
342
  - `buildPostureDigest(result)` - reduce a full scan result to a compact API/mobile-friendly digest.
323
343
  - `buildActionPlan(result)` - turn remediation, score drivers, exposure, and vendor context into prioritized fix actions.
324
344
  - `scanLiveCertificate(url)` - perform a TLS handshake-only certificate read for lightweight cert monitoring.
345
+ - `buildObservationLedger(result)` - produce stable source, confidence, status, and freshness-aware posture observations.
346
+ - `diffObservationLedgers(current, previous)` - compare stable observations and classify their operational impact.
347
+ - `evaluateObservationPolicy({ ledger, drift, policy })` - evaluate bounded declarative posture and change rules.
325
348
  - `buildPostureDriftReportFromSnapshots(current, previous)` - produce a complete scan-to-scan drift report for monitoring, alerting, and history views.
326
349
  - `buildPostureRemediationPlan(result)` - generate prioritized, owner-aware remediation actions from findings and score drivers.
327
350
  - `attachIssueEvidence(result)` - add structured evidence references to findings without changing their existing fields.
@@ -333,6 +356,9 @@ Package subpath exports:
333
356
  - `securl/posture-digest`
334
357
  - `securl/action-plan`
335
358
  - `securl/live-certificate`
359
+ - `securl/observations`
360
+ - `securl/observation-drift`
361
+ - `securl/observation-policy`
336
362
  - `securl/posture-drift`
337
363
  - `securl/remediation-plan`
338
364
  - `securl/risk-events`
package/dist/index.d.ts CHANGED
@@ -11,6 +11,9 @@ export declare function analyzeUrl(input: string, options?: AnalyzeTargetOptions
11
11
  export declare const analyzeTarget: typeof analyzeUrl;
12
12
  export { formatErrorMessage };
13
13
  export { buildCompromiseSignals, emptyCompromiseSignals } from "./compromiseSignals.js";
14
+ export { buildObservationLedger } from "./observations.js";
15
+ export { diffObservationLedgers } from "./observationDrift.js";
16
+ export { DEFAULT_OBSERVATION_POLICY, evaluateObservationPolicy, validateObservationPolicy } from "./observationPolicy.js";
14
17
  export { buildActionPlan } from "./actionPlan.js";
15
18
  export { scanLiveCertificate } from "./certificate.js";
16
19
  export { buildExposureBrief } from "./exposureBrief.js";
package/dist/index.js CHANGED
@@ -19,6 +19,7 @@ import { analyzeApiSurface, analyzeCorsSecurity, analyzeExposure, fetchPublicSig
19
19
  import { fetchLibraryRiskSignals } from "./libraryRisk.js";
20
20
  import { fetchWithRedirects, requestJson, requestOnce, requestText, requestWithHeaders, } from "./network.js";
21
21
  import { normalizeDiscoveredPath, rankDiscoveredPaths } from "./path-discovery.js";
22
+ import { buildObservationLedger } from "./observations.js";
22
23
  import { buildPassiveIntelligence, emptyPassiveIntelligence } from "./passive-intelligence.js";
23
24
  import { analyzeRedirectChain } from "./redirectChain.js";
24
25
  import { attachIssueEvidence, buildPostureEvidenceSummary, buildPostureRemediationPlan } from "./postureRemediation.js";
@@ -514,10 +515,14 @@ async function buildLimitedResult(input, normalizedInput, failure, scanTiming) {
514
515
  exposureBrief: buildExposureBrief(resultWithRemediation),
515
516
  vendorExposure: buildVendorExposureBrief(resultWithRemediation),
516
517
  };
517
- return {
518
+ const resultWithActions = {
518
519
  ...resultWithBriefs,
519
520
  actionPlan: buildActionPlan(resultWithBriefs),
520
521
  };
522
+ return {
523
+ ...resultWithActions,
524
+ observationLedger: buildObservationLedger(resultWithActions),
525
+ };
521
526
  }
522
527
  async function enrichCoreResult(result, profile) {
523
528
  const finalUrl = new URL(result.finalUrl);
@@ -953,10 +958,14 @@ function buildTimedOutEnrichmentResult(result, pageAnalysisEnabled, timeoutMs, c
953
958
  exposureBrief: buildExposureBrief(resultWithRemediation),
954
959
  vendorExposure: buildVendorExposureBrief(resultWithRemediation),
955
960
  };
956
- return {
961
+ const resultWithActions = {
957
962
  ...resultWithBriefs,
958
963
  actionPlan: buildActionPlan(resultWithBriefs),
959
964
  };
965
+ return {
966
+ ...resultWithActions,
967
+ observationLedger: buildObservationLedger(resultWithActions),
968
+ };
960
969
  }
961
970
  export async function analyzeUrl(input, options = {}) {
962
971
  const scanStartedAt = Date.now();
@@ -1031,14 +1040,21 @@ export async function analyzeUrl(input, options = {}) {
1031
1040
  exposureBrief: buildExposureBrief(resultWithRemediation),
1032
1041
  vendorExposure: buildVendorExposureBrief(resultWithRemediation),
1033
1042
  };
1034
- return {
1043
+ const resultWithActions = {
1035
1044
  ...resultWithBriefs,
1036
1045
  actionPlan: buildActionPlan(resultWithBriefs),
1037
1046
  };
1047
+ return {
1048
+ ...resultWithActions,
1049
+ observationLedger: buildObservationLedger(resultWithActions),
1050
+ };
1038
1051
  }
1039
1052
  export const analyzeTarget = analyzeUrl;
1040
1053
  export { formatErrorMessage };
1041
1054
  export { buildCompromiseSignals, emptyCompromiseSignals } from "./compromiseSignals.js";
1055
+ export { buildObservationLedger } from "./observations.js";
1056
+ export { diffObservationLedgers } from "./observationDrift.js";
1057
+ export { DEFAULT_OBSERVATION_POLICY, evaluateObservationPolicy, validateObservationPolicy } from "./observationPolicy.js";
1042
1058
  export { buildActionPlan } from "./actionPlan.js";
1043
1059
  export { scanLiveCertificate } from "./certificate.js";
1044
1060
  export { buildExposureBrief } from "./exposureBrief.js";
@@ -0,0 +1,2 @@
1
+ import type { ObservationDriftReport, ObservationLedger } from "./types.js";
2
+ export declare function diffObservationLedgers(current: ObservationLedger, previous: ObservationLedger): ObservationDriftReport;
@@ -0,0 +1,152 @@
1
+ import { createHash } from "node:crypto";
2
+ const STATUS_RANK = {
3
+ unavailable: 0,
4
+ missing: 1,
5
+ inferred: 2,
6
+ observed: 3,
7
+ };
8
+ const CRITICAL_KINDS = new Set([
9
+ "tls.certificate.valid",
10
+ "email.dmarc",
11
+ "http.header.strict-transport-security",
12
+ "http.header.content-security-policy",
13
+ ]);
14
+ function equalValue(left, right) {
15
+ return JSON.stringify(left) === JSON.stringify(right);
16
+ }
17
+ function certificateBand(days) {
18
+ if (days < 0)
19
+ return 0;
20
+ if (days <= 7)
21
+ return 1;
22
+ if (days <= 14)
23
+ return 2;
24
+ if (days <= 30)
25
+ return 3;
26
+ return 4;
27
+ }
28
+ function impactFor(previous, current) {
29
+ if (!previous && current) {
30
+ return current.status === "missing" || current.status === "unavailable" ? "regression" : "change";
31
+ }
32
+ if (previous && !current) {
33
+ return previous.status === "missing" || previous.status === "unavailable" ? "improvement" : "regression";
34
+ }
35
+ if (!previous || !current)
36
+ return "change";
37
+ if (STATUS_RANK[current.status] < STATUS_RANK[previous.status])
38
+ return "regression";
39
+ if (STATUS_RANK[current.status] > STATUS_RANK[previous.status])
40
+ return "improvement";
41
+ if (current.kind === "tls.certificate.days_remaining"
42
+ && typeof previous.value === "number"
43
+ && typeof current.value === "number") {
44
+ const previousBand = certificateBand(previous.value);
45
+ const currentBand = certificateBand(current.value);
46
+ if (currentBand < previousBand)
47
+ return "regression";
48
+ if (currentBand > previousBand || current.value > previous.value + 7)
49
+ return "improvement";
50
+ return "change";
51
+ }
52
+ if (current.kind === "tls.certificate.valid"
53
+ && typeof previous.value === "boolean"
54
+ && typeof current.value === "boolean") {
55
+ return current.value ? "improvement" : "regression";
56
+ }
57
+ return "change";
58
+ }
59
+ function severityFor(kind, impact, current) {
60
+ if (impact !== "regression")
61
+ return "info";
62
+ if (kind === "tls.certificate.valid" && current?.value === false)
63
+ return "critical";
64
+ if (CRITICAL_KINDS.has(kind) && (current?.status === "missing" || current?.status === "unavailable"))
65
+ return "critical";
66
+ if (kind === "tls.certificate.days_remaining" && typeof current?.value === "number" && current.value <= 14)
67
+ return "critical";
68
+ return "warning";
69
+ }
70
+ function changeId(observationId, type) {
71
+ return `chg_${createHash("sha256").update(`${observationId}\u0000${type}`).digest("hex").slice(0, 20)}`;
72
+ }
73
+ function summaryFor(type, previous, current) {
74
+ const label = current?.kind ?? previous?.kind ?? "observation";
75
+ if (type === "added")
76
+ return `${label} was newly detected.`;
77
+ if (type === "removed")
78
+ return `${label} is no longer detected.`;
79
+ if (type === "status_changed")
80
+ return `${label} changed from ${previous?.status} to ${current?.status}.`;
81
+ if (type === "confidence_changed")
82
+ return `${label} confidence changed from ${previous?.confidence} to ${current?.confidence}.`;
83
+ return `${label} changed from ${JSON.stringify(previous?.value)} to ${JSON.stringify(current?.value)}.`;
84
+ }
85
+ function buildChange(type, previous, current) {
86
+ const observation = current ?? previous;
87
+ if (!observation)
88
+ throw new Error("Observation change requires a current or previous value.");
89
+ const impact = impactFor(previous, current);
90
+ return {
91
+ id: changeId(observation.id, type),
92
+ observationId: observation.id,
93
+ type,
94
+ impact,
95
+ severity: severityFor(observation.kind, impact, current),
96
+ category: observation.category,
97
+ kind: observation.kind,
98
+ subject: observation.subject,
99
+ previous,
100
+ current,
101
+ summary: summaryFor(type, previous, current),
102
+ };
103
+ }
104
+ export function diffObservationLedgers(current, previous) {
105
+ const currentById = new Map(current.observations.map((observation) => [observation.id, observation]));
106
+ const previousById = new Map(previous.observations.map((observation) => [observation.id, observation]));
107
+ const changes = [];
108
+ for (const observation of current.observations) {
109
+ const before = previousById.get(observation.id) ?? null;
110
+ if (!before) {
111
+ changes.push(buildChange("added", null, observation));
112
+ continue;
113
+ }
114
+ if (before.status !== observation.status)
115
+ changes.push(buildChange("status_changed", before, observation));
116
+ if (!equalValue(before.value, observation.value))
117
+ changes.push(buildChange("value_changed", before, observation));
118
+ if (before.confidence !== observation.confidence)
119
+ changes.push(buildChange("confidence_changed", before, observation));
120
+ }
121
+ for (const observation of previous.observations) {
122
+ if (!currentById.has(observation.id))
123
+ changes.push(buildChange("removed", observation, null));
124
+ }
125
+ const severityRank = { critical: 3, warning: 2, info: 1 };
126
+ changes.sort((left, right) => severityRank[right.severity] - severityRank[left.severity] || left.id.localeCompare(right.id));
127
+ const regressions = changes.filter((change) => change.impact === "regression").length;
128
+ const improvements = changes.filter((change) => change.impact === "improvement").length;
129
+ const bySeverity = { info: 0, warning: 0, critical: 0 };
130
+ const byCategory = {};
131
+ for (const change of changes) {
132
+ bySeverity[change.severity] += 1;
133
+ byCategory[change.category] = (byCategory[change.category] ?? 0) + 1;
134
+ }
135
+ return {
136
+ version: "1.0",
137
+ target: current.target,
138
+ comparedAt: current.generatedAt,
139
+ previousObservedAt: previous.generatedAt,
140
+ currentObservedAt: current.generatedAt,
141
+ changes,
142
+ summary: {
143
+ direction: regressions ? "regressed" : improvements ? "improved" : changes.length ? "changed" : "unchanged",
144
+ total: changes.length,
145
+ regressions,
146
+ improvements,
147
+ neutralChanges: changes.length - regressions - improvements,
148
+ bySeverity,
149
+ byCategory,
150
+ },
151
+ };
152
+ }
@@ -0,0 +1,8 @@
1
+ import type { ObservationDriftReport, ObservationLedger, ObservationPolicy, ObservationPolicyEvaluation } from "./types.js";
2
+ export declare const DEFAULT_OBSERVATION_POLICY: ObservationPolicy;
3
+ export declare function validateObservationPolicy(value: unknown): ObservationPolicy;
4
+ export declare function evaluateObservationPolicy({ ledger, drift, policy, }: {
5
+ ledger: ObservationLedger;
6
+ drift?: ObservationDriftReport | null;
7
+ policy?: ObservationPolicy;
8
+ }): ObservationPolicyEvaluation;
@@ -0,0 +1,226 @@
1
+ import { createHash } from "node:crypto";
2
+ const MAX_RULES = 25;
3
+ const VALID_OPERATORS = new Set(["eq", "neq", "in", "gte", "lte"]);
4
+ const VALID_SEVERITIES = new Set(["info", "warning", "critical"]);
5
+ const VALID_SCOPES = new Set(["observation", "change"]);
6
+ const VALID_FIELDS = new Set(["status", "value", "confidence", "impact", "severity", "type"]);
7
+ const VALID_CATEGORIES = new Set(["transport", "header", "certificate", "dns", "email", "infrastructure", "technology", "trust", "availability"]);
8
+ export const DEFAULT_OBSERVATION_POLICY = {
9
+ id: "securl-baseline-v1",
10
+ name: "SecURL baseline",
11
+ version: "1.0",
12
+ rules: [
13
+ {
14
+ id: "certificate-valid",
15
+ title: "Certificate must remain valid",
16
+ severity: "critical",
17
+ scope: "observation",
18
+ selector: { kind: "tls.certificate.valid" },
19
+ assertion: { field: "value", operator: "eq", value: true },
20
+ requireMatch: true,
21
+ },
22
+ {
23
+ id: "certificate-window",
24
+ title: "Certificate must have at least 14 days remaining",
25
+ severity: "critical",
26
+ scope: "observation",
27
+ selector: { kind: "tls.certificate.days_remaining" },
28
+ assertion: { field: "value", operator: "gte", value: 14 },
29
+ requireMatch: true,
30
+ },
31
+ {
32
+ id: "hsts-present",
33
+ title: "HSTS must be present",
34
+ severity: "warning",
35
+ scope: "observation",
36
+ selector: { kind: "http.header.strict-transport-security" },
37
+ assertion: { field: "status", operator: "eq", value: "observed" },
38
+ requireMatch: true,
39
+ },
40
+ {
41
+ id: "csp-present",
42
+ title: "Content Security Policy must be present",
43
+ severity: "warning",
44
+ scope: "observation",
45
+ selector: { kind: "http.header.content-security-policy" },
46
+ assertion: { field: "status", operator: "eq", value: "observed" },
47
+ requireMatch: true,
48
+ },
49
+ {
50
+ id: "dmarc-enforced",
51
+ title: "DMARC should be strong or monitored",
52
+ severity: "warning",
53
+ scope: "observation",
54
+ selector: { kind: "email.dmarc" },
55
+ assertion: { field: "value", operator: "in", value: ["strong", "watch"] },
56
+ requireMatch: true,
57
+ },
58
+ {
59
+ id: "critical-regression",
60
+ title: "Critical observation regressions are not allowed",
61
+ severity: "critical",
62
+ scope: "change",
63
+ selector: {},
64
+ assertion: { field: "severity", operator: "neq", value: "critical" },
65
+ requireMatch: false,
66
+ },
67
+ ],
68
+ };
69
+ function boundedText(value, field, max) {
70
+ if (typeof value !== "string" || !value.trim() || value.length > max) {
71
+ throw new Error(`Observation policy ${field} must be a non-empty string up to ${max} characters.`);
72
+ }
73
+ return value.trim();
74
+ }
75
+ export function validateObservationPolicy(value) {
76
+ if (!value || typeof value !== "object" || Array.isArray(value))
77
+ throw new Error("Observation policy must be an object.");
78
+ const input = value;
79
+ if (!Array.isArray(input.rules) || input.rules.length === 0 || input.rules.length > MAX_RULES) {
80
+ throw new Error(`Observation policy must contain between 1 and ${MAX_RULES} rules.`);
81
+ }
82
+ const seen = new Set();
83
+ const rules = input.rules.map((rawRule) => {
84
+ if (!rawRule || typeof rawRule !== "object" || Array.isArray(rawRule))
85
+ throw new Error("Each observation policy rule must be an object.");
86
+ const rule = rawRule;
87
+ const id = boundedText(rule.id, "rule id", 64);
88
+ if (!/^[a-z0-9][a-z0-9._-]*$/i.test(id) || seen.has(id))
89
+ throw new Error(`Observation policy rule id is invalid or duplicated: ${id}.`);
90
+ seen.add(id);
91
+ if (!VALID_SEVERITIES.has(String(rule.severity)))
92
+ throw new Error(`Observation policy rule ${id} has an invalid severity.`);
93
+ if (!VALID_SCOPES.has(String(rule.scope)))
94
+ throw new Error(`Observation policy rule ${id} has an invalid scope.`);
95
+ const selector = rule.selector && typeof rule.selector === "object" && !Array.isArray(rule.selector)
96
+ ? rule.selector
97
+ : {};
98
+ const assertion = rule.assertion && typeof rule.assertion === "object" && !Array.isArray(rule.assertion)
99
+ ? rule.assertion
100
+ : null;
101
+ if (!assertion || !VALID_FIELDS.has(String(assertion.field)) || !VALID_OPERATORS.has(String(assertion.operator))) {
102
+ throw new Error(`Observation policy rule ${id} has an invalid assertion.`);
103
+ }
104
+ if (selector.category !== undefined && !VALID_CATEGORIES.has(String(selector.category))) {
105
+ throw new Error(`Observation policy rule ${id} has an invalid selector category.`);
106
+ }
107
+ if (rule.scope === "observation" && ["impact", "severity", "type"].includes(String(assertion.field))) {
108
+ throw new Error(`Observation policy rule ${id} uses a change-only field for an observation rule.`);
109
+ }
110
+ if (rule.scope === "change" && ["status", "confidence"].includes(String(assertion.field))) {
111
+ throw new Error(`Observation policy rule ${id} uses an observation-only field for a change rule.`);
112
+ }
113
+ if (assertion.operator === "in" && !Array.isArray(assertion.value))
114
+ throw new Error(`Observation policy rule ${id} requires an array value for in.`);
115
+ if ((assertion.operator === "gte" || assertion.operator === "lte") && typeof assertion.value !== "number") {
116
+ throw new Error(`Observation policy rule ${id} requires a numeric assertion value.`);
117
+ }
118
+ return {
119
+ id,
120
+ title: boundedText(rule.title, `rule ${id} title`, 160),
121
+ ...(typeof rule.description === "string" ? { description: rule.description.trim().slice(0, 500) } : {}),
122
+ enabled: rule.enabled !== false,
123
+ severity: rule.severity,
124
+ scope: rule.scope,
125
+ selector: {
126
+ ...(typeof selector.kind === "string" ? { kind: selector.kind.slice(0, 160) } : {}),
127
+ ...(typeof selector.kindPrefix === "string" ? { kindPrefix: selector.kindPrefix.slice(0, 160) } : {}),
128
+ ...(typeof selector.category === "string" ? { category: selector.category } : {}),
129
+ },
130
+ assertion: {
131
+ field: assertion.field,
132
+ operator: assertion.operator,
133
+ value: assertion.value,
134
+ },
135
+ requireMatch: rule.requireMatch === true,
136
+ };
137
+ });
138
+ return {
139
+ id: boundedText(input.id, "id", 64),
140
+ name: boundedText(input.name, "name", 120),
141
+ version: boundedText(input.version, "version", 32),
142
+ rules,
143
+ };
144
+ }
145
+ function matchesSelector(entity, rule) {
146
+ const { selector } = rule;
147
+ return (!selector.kind || entity.kind === selector.kind)
148
+ && (!selector.kindPrefix || entity.kind.startsWith(selector.kindPrefix))
149
+ && (!selector.category || entity.category === selector.category);
150
+ }
151
+ function actualValue(entity, field) {
152
+ if (field === "value")
153
+ return "current" in entity ? entity.current?.value ?? null : entity.value;
154
+ return entity[field] ?? null;
155
+ }
156
+ function assertionPasses(actual, rule) {
157
+ const { operator, value } = rule.assertion;
158
+ if (operator === "eq")
159
+ return JSON.stringify(actual) === JSON.stringify(value);
160
+ if (operator === "neq")
161
+ return JSON.stringify(actual) !== JSON.stringify(value);
162
+ if (operator === "in")
163
+ return Array.isArray(value) && value.some((candidate) => JSON.stringify(candidate) === JSON.stringify(actual));
164
+ if (operator === "gte")
165
+ return typeof actual === "number" && typeof value === "number" && actual >= value;
166
+ if (operator === "lte")
167
+ return typeof actual === "number" && typeof value === "number" && actual <= value;
168
+ return false;
169
+ }
170
+ function violationId(ruleId, entityId) {
171
+ return `pol_${createHash("sha256").update(`${ruleId}\u0000${entityId}`).digest("hex").slice(0, 20)}`;
172
+ }
173
+ function violationFor(rule, entity) {
174
+ const actual = entity ? actualValue(entity, rule.assertion.field) : null;
175
+ const isChange = entity && "observationId" in entity;
176
+ const entityId = entity?.id ?? "missing";
177
+ return {
178
+ id: violationId(rule.id, entityId),
179
+ ruleId: rule.id,
180
+ title: rule.title,
181
+ severity: rule.severity,
182
+ scope: rule.scope,
183
+ observationId: isChange ? entity.observationId : entity?.id ?? null,
184
+ changeId: isChange ? entity.id : null,
185
+ kind: entity?.kind ?? rule.selector.kind ?? rule.selector.kindPrefix ?? null,
186
+ subject: entity?.subject ?? null,
187
+ expected: rule.assertion,
188
+ actual: actual,
189
+ summary: entity
190
+ ? `${rule.title}: ${rule.assertion.field} ${rule.assertion.operator} ${JSON.stringify(rule.assertion.value)} was not satisfied.`
191
+ : `${rule.title}: no matching observation was available.`,
192
+ };
193
+ }
194
+ export function evaluateObservationPolicy({ ledger, drift = null, policy = DEFAULT_OBSERVATION_POLICY, }) {
195
+ const normalized = validateObservationPolicy(policy);
196
+ const violations = [];
197
+ const enabledRules = normalized.rules.filter((rule) => rule.enabled !== false);
198
+ for (const rule of enabledRules) {
199
+ const source = rule.scope === "change" ? drift?.changes ?? [] : ledger.observations;
200
+ const matches = source.filter((entity) => matchesSelector(entity, rule));
201
+ if (!matches.length && rule.requireMatch)
202
+ violations.push(violationFor(rule, null));
203
+ for (const entity of matches) {
204
+ if (!assertionPasses(actualValue(entity, rule.assertion.field), rule))
205
+ violations.push(violationFor(rule, entity));
206
+ }
207
+ }
208
+ const bySeverity = { info: 0, warning: 0, critical: 0 };
209
+ for (const violation of violations)
210
+ bySeverity[violation.severity] += 1;
211
+ const highestSeverity = bySeverity.critical ? "critical" : bySeverity.warning ? "warning" : bySeverity.info ? "info" : null;
212
+ return {
213
+ version: "1.0",
214
+ policy: { id: normalized.id, name: normalized.name, version: normalized.version },
215
+ target: ledger.target,
216
+ evaluatedAt: ledger.generatedAt,
217
+ passed: violations.length === 0,
218
+ violations,
219
+ summary: {
220
+ rulesEvaluated: enabledRules.length,
221
+ violations: violations.length,
222
+ bySeverity,
223
+ highestSeverity,
224
+ },
225
+ };
226
+ }
@@ -0,0 +1,2 @@
1
+ import type { AnalysisResult, ObservationLedger } from "./types.js";
2
+ export declare function buildObservationLedger(result: AnalysisResult): ObservationLedger;
@@ -0,0 +1,203 @@
1
+ import { createHash } from "node:crypto";
2
+ const HOUR = 60 * 60 * 1000;
3
+ const categoryTtl = {
4
+ transport: HOUR,
5
+ header: HOUR,
6
+ certificate: 6 * HOUR,
7
+ dns: 24 * HOUR,
8
+ email: 24 * HOUR,
9
+ infrastructure: 24 * HOUR,
10
+ technology: 24 * HOUR,
11
+ trust: 24 * HOUR,
12
+ availability: HOUR,
13
+ };
14
+ function kindToken(value) {
15
+ return value.toLowerCase().replace(/[^a-z0-9]+/g, "_").replace(/^_+|_+$/g, "").slice(0, 48) || "unknown";
16
+ }
17
+ function observationId(category, kind, subject, source) {
18
+ const fingerprint = createHash("sha256")
19
+ .update(`${category}\u0000${kind}\u0000${subject.toLowerCase()}\u0000${source}`)
20
+ .digest("hex")
21
+ .slice(0, 20);
22
+ return `obs_${fingerprint}`;
23
+ }
24
+ function evidence(kind, label, observed, source = "observed") {
25
+ return [{
26
+ kind,
27
+ label,
28
+ observed: Array.isArray(observed) ? observed.join(", ") : observed === null ? null : String(observed),
29
+ source,
30
+ }];
31
+ }
32
+ export function buildObservationLedger(result) {
33
+ const generatedAt = result.scannedAt || new Date().toISOString();
34
+ const observedAtMs = new Date(generatedAt).getTime();
35
+ const baseMs = Number.isFinite(observedAtMs) ? observedAtMs : Date.now();
36
+ const observations = [];
37
+ const add = (input) => {
38
+ observations.push({
39
+ ...input,
40
+ id: observationId(input.category, input.kind, input.subject, input.source),
41
+ observedAt: generatedAt,
42
+ freshUntil: new Date(baseMs + (input.ttlMs ?? categoryTtl[input.category])).toISOString(),
43
+ });
44
+ };
45
+ add({
46
+ category: "transport",
47
+ kind: "http.status",
48
+ subject: result.finalUrl,
49
+ status: result.statusCode > 0 ? "observed" : "unavailable",
50
+ value: result.statusCode > 0 ? result.statusCode : null,
51
+ confidence: "high",
52
+ source: "probe",
53
+ evidence: evidence("probe", "HTTP response status", result.statusCode > 0 ? result.statusCode : null),
54
+ });
55
+ for (const header of result.headers) {
56
+ add({
57
+ category: "header",
58
+ kind: `http.header.${header.key.toLowerCase()}`,
59
+ subject: result.finalUrl,
60
+ status: header.status === "missing" ? "missing" : "observed",
61
+ value: header.value,
62
+ confidence: "high",
63
+ source: "header",
64
+ evidence: evidence("header", header.label, header.value),
65
+ });
66
+ }
67
+ const certificate = result.certificate;
68
+ add({
69
+ category: "certificate",
70
+ kind: "tls.certificate.valid",
71
+ subject: result.host,
72
+ status: certificate.available ? "observed" : "unavailable",
73
+ value: certificate.available ? certificate.valid && certificate.authorized : null,
74
+ confidence: "high",
75
+ source: "tls",
76
+ evidence: evidence("tls", "Certificate validity", certificate.available ? certificate.valid && certificate.authorized : null),
77
+ });
78
+ add({
79
+ category: "certificate",
80
+ kind: "tls.certificate.days_remaining",
81
+ subject: result.host,
82
+ status: certificate.daysRemaining === null ? "unavailable" : "observed",
83
+ value: certificate.daysRemaining,
84
+ confidence: "high",
85
+ source: "tls",
86
+ evidence: evidence("tls", "Certificate days remaining", certificate.daysRemaining),
87
+ });
88
+ add({
89
+ category: "transport",
90
+ kind: "tls.protocol",
91
+ subject: result.host,
92
+ status: certificate.protocol ? "observed" : "unavailable",
93
+ value: certificate.protocol,
94
+ confidence: "high",
95
+ source: "tls",
96
+ evidence: evidence("tls", "Negotiated TLS protocol", certificate.protocol),
97
+ });
98
+ const domain = result.domainSecurity;
99
+ add({
100
+ category: "dns",
101
+ kind: "dns.dnssec",
102
+ subject: domain.host,
103
+ status: domain.dnssec.status === "unknown" ? "unavailable" : domain.dnssec.enabled ? "observed" : "missing",
104
+ value: domain.dnssec.status,
105
+ confidence: domain.dnssec.status === "unknown" ? "low" : "high",
106
+ source: "dns",
107
+ evidence: evidence("dns", "DNSSEC status", domain.dnssec.status),
108
+ });
109
+ for (const [kind, policy] of [["email.spf", domain.emailPolicy.spf], ["email.dmarc", domain.emailPolicy.dmarc]]) {
110
+ add({
111
+ category: "email",
112
+ kind,
113
+ subject: domain.host,
114
+ status: policy.status === "missing" ? "missing" : "observed",
115
+ value: policy.status,
116
+ confidence: "high",
117
+ source: "dns",
118
+ evidence: evidence("dns", kind === "email.spf" ? "SPF policy" : "DMARC policy", policy.status),
119
+ });
120
+ }
121
+ add({
122
+ category: "trust",
123
+ kind: "public.security_txt",
124
+ subject: result.host,
125
+ status: result.securityTxt.status === "missing" ? "missing" : "observed",
126
+ value: result.securityTxt.status,
127
+ confidence: "high",
128
+ source: "public_record",
129
+ evidence: evidence("public_record", "security.txt status", result.securityTxt.status),
130
+ });
131
+ for (const provider of result.infrastructure.providers) {
132
+ add({
133
+ category: "infrastructure",
134
+ kind: `infrastructure.provider.${provider.category}.${kindToken(provider.provider)}`,
135
+ subject: result.host,
136
+ status: provider.source === "technology" ? "inferred" : "observed",
137
+ value: provider.provider,
138
+ confidence: provider.confidence,
139
+ source: "infrastructure",
140
+ evidence: evidence(provider.source === "dns" || provider.source === "reverse_dns" ? "dns" : "header", provider.provider, provider.evidence, provider.source === "technology" ? "inferred" : "observed"),
141
+ });
142
+ }
143
+ for (const technology of result.technologies) {
144
+ add({
145
+ category: "technology",
146
+ kind: `technology.${technology.category}.${kindToken(technology.name)}`,
147
+ subject: result.host,
148
+ status: technology.detection === "inferred" ? "inferred" : "observed",
149
+ value: technology.version ? `${technology.name}@${technology.version}` : technology.name,
150
+ confidence: technology.confidence,
151
+ source: "technology",
152
+ evidence: evidence("html", technology.name, technology.evidence, technology.detection),
153
+ });
154
+ }
155
+ for (const provider of result.wafFingerprint.providers) {
156
+ add({
157
+ category: "infrastructure",
158
+ kind: `infrastructure.waf.${kindToken(provider.name)}`,
159
+ subject: result.host,
160
+ status: provider.detection === "inferred" ? "inferred" : "observed",
161
+ value: provider.name,
162
+ confidence: provider.confidence,
163
+ source: "infrastructure",
164
+ evidence: evidence("header", `${provider.name} WAF`, provider.evidence, provider.detection),
165
+ });
166
+ }
167
+ if (result.assessmentLimitation.limited) {
168
+ add({
169
+ category: "availability",
170
+ kind: "assessment.limitation",
171
+ subject: result.finalUrl,
172
+ status: "unavailable",
173
+ value: result.assessmentLimitation.kind,
174
+ confidence: "high",
175
+ source: "availability",
176
+ evidence: evidence("score_driver", "Assessment limitation", result.assessmentLimitation.detail),
177
+ });
178
+ }
179
+ observations.sort((left, right) => left.id.localeCompare(right.id));
180
+ const byStatus = {
181
+ observed: 0,
182
+ inferred: 0,
183
+ missing: 0,
184
+ unavailable: 0,
185
+ };
186
+ const byCategory = {};
187
+ for (const observation of observations) {
188
+ byStatus[observation.status] += 1;
189
+ byCategory[observation.category] = (byCategory[observation.category] ?? 0) + 1;
190
+ }
191
+ return {
192
+ version: "1.0",
193
+ target: result.finalUrl,
194
+ generatedAt,
195
+ observations,
196
+ summary: {
197
+ total: observations.length,
198
+ byStatus,
199
+ byCategory,
200
+ highConfidence: observations.filter((observation) => observation.confidence === "high").length,
201
+ },
202
+ };
203
+ }
package/dist/types.d.ts CHANGED
@@ -815,6 +815,128 @@ export interface PublicSignalsInfo {
815
815
  issues: string[];
816
816
  strengths: string[];
817
817
  }
818
+ export type ObservationCategory = "transport" | "header" | "certificate" | "dns" | "email" | "infrastructure" | "technology" | "trust" | "availability";
819
+ export type ObservationStatus = "observed" | "inferred" | "missing" | "unavailable";
820
+ export type ObservationValue = string | number | boolean | null | string[];
821
+ export interface PostureObservation {
822
+ id: string;
823
+ category: ObservationCategory;
824
+ kind: string;
825
+ subject: string;
826
+ status: ObservationStatus;
827
+ value: ObservationValue;
828
+ confidence: IssueConfidence;
829
+ source: ScanEvidenceKind | "availability" | "technology" | "infrastructure";
830
+ observedAt: string;
831
+ freshUntil: string;
832
+ evidence: ScanEvidenceReference[];
833
+ }
834
+ export interface ObservationLedger {
835
+ version: "1.0";
836
+ target: string;
837
+ generatedAt: string;
838
+ observations: PostureObservation[];
839
+ summary: {
840
+ total: number;
841
+ byStatus: Record<ObservationStatus, number>;
842
+ byCategory: Partial<Record<ObservationCategory, number>>;
843
+ highConfidence: number;
844
+ };
845
+ }
846
+ export type ObservationChangeType = "added" | "removed" | "status_changed" | "value_changed" | "confidence_changed";
847
+ export type ObservationChangeImpact = "regression" | "improvement" | "change";
848
+ export type ObservationChangeSeverity = "info" | "warning" | "critical";
849
+ export interface ObservationChange {
850
+ id: string;
851
+ observationId: string;
852
+ type: ObservationChangeType;
853
+ impact: ObservationChangeImpact;
854
+ severity: ObservationChangeSeverity;
855
+ category: ObservationCategory;
856
+ kind: string;
857
+ subject: string;
858
+ previous: PostureObservation | null;
859
+ current: PostureObservation | null;
860
+ summary: string;
861
+ }
862
+ export interface ObservationDriftReport {
863
+ version: "1.0";
864
+ target: string;
865
+ comparedAt: string;
866
+ previousObservedAt: string;
867
+ currentObservedAt: string;
868
+ changes: ObservationChange[];
869
+ summary: {
870
+ direction: "regressed" | "improved" | "changed" | "unchanged";
871
+ total: number;
872
+ regressions: number;
873
+ improvements: number;
874
+ neutralChanges: number;
875
+ bySeverity: Record<ObservationChangeSeverity, number>;
876
+ byCategory: Partial<Record<ObservationCategory, number>>;
877
+ };
878
+ }
879
+ export type ObservationPolicySeverity = "info" | "warning" | "critical";
880
+ export type ObservationPolicyScope = "observation" | "change";
881
+ export type ObservationPolicyField = "status" | "value" | "confidence" | "impact" | "severity" | "type";
882
+ export type ObservationPolicyOperator = "eq" | "neq" | "in" | "gte" | "lte";
883
+ export interface ObservationPolicyRule {
884
+ id: string;
885
+ title: string;
886
+ description?: string;
887
+ enabled?: boolean;
888
+ severity: ObservationPolicySeverity;
889
+ scope: ObservationPolicyScope;
890
+ selector: {
891
+ kind?: string;
892
+ kindPrefix?: string;
893
+ category?: ObservationCategory;
894
+ };
895
+ assertion: {
896
+ field: ObservationPolicyField;
897
+ operator: ObservationPolicyOperator;
898
+ value: ObservationValue | ObservationChangeImpact | ObservationChangeSeverity | ObservationChangeType;
899
+ };
900
+ requireMatch?: boolean;
901
+ }
902
+ export interface ObservationPolicy {
903
+ id: string;
904
+ name: string;
905
+ version: string;
906
+ rules: ObservationPolicyRule[];
907
+ }
908
+ export interface ObservationPolicyViolation {
909
+ id: string;
910
+ ruleId: string;
911
+ title: string;
912
+ severity: ObservationPolicySeverity;
913
+ scope: ObservationPolicyScope;
914
+ observationId: string | null;
915
+ changeId: string | null;
916
+ kind: string | null;
917
+ subject: string | null;
918
+ expected: ObservationPolicyRule["assertion"];
919
+ actual: ObservationValue;
920
+ summary: string;
921
+ }
922
+ export interface ObservationPolicyEvaluation {
923
+ version: "1.0";
924
+ policy: {
925
+ id: string;
926
+ name: string;
927
+ version: string;
928
+ };
929
+ target: string;
930
+ evaluatedAt: string;
931
+ passed: boolean;
932
+ violations: ObservationPolicyViolation[];
933
+ summary: {
934
+ rulesEvaluated: number;
935
+ violations: number;
936
+ bySeverity: Record<ObservationPolicySeverity, number>;
937
+ highestSeverity: ObservationPolicySeverity | null;
938
+ };
939
+ }
818
940
  export interface AnalysisResult {
819
941
  inputUrl: string;
820
942
  normalizedUrl: string;
@@ -862,6 +984,7 @@ export interface AnalysisResult {
862
984
  publicSignals: PublicSignalsInfo;
863
985
  wafFingerprint: WafFingerprintInfo;
864
986
  scanTiming?: ScanTimingInfo;
987
+ observationLedger?: ObservationLedger;
865
988
  }
866
989
  export interface AnalyzeTargetOptions {
867
990
  includeCertificate?: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "securl",
3
- "version": "1.9.0",
3
+ "version": "1.11.0",
4
4
  "type": "module",
5
5
  "description": "Passive external security posture scanner for public URLs and web services.",
6
6
  "author": {
@@ -81,6 +81,18 @@
81
81
  "./live-certificate": {
82
82
  "types": "./dist/certificate.d.ts",
83
83
  "default": "./dist/certificate.js"
84
+ },
85
+ "./observations": {
86
+ "types": "./dist/observations.d.ts",
87
+ "default": "./dist/observations.js"
88
+ },
89
+ "./observation-drift": {
90
+ "types": "./dist/observationDrift.d.ts",
91
+ "default": "./dist/observationDrift.js"
92
+ },
93
+ "./observation-policy": {
94
+ "types": "./dist/observationPolicy.d.ts",
95
+ "default": "./dist/observationPolicy.js"
84
96
  }
85
97
  },
86
98
  "files": [