@knowttl/tenable-axi 0.1.4 → 0.1.5

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.
@@ -0,0 +1,19 @@
1
+ export type DiagnosticRedactor = {
2
+ text(value: string): string;
3
+ };
4
+ export declare class ResponseDiagnostics {
5
+ private readonly redactor;
6
+ private readonly errorCodes;
7
+ private details;
8
+ private readonly shapeByPath;
9
+ private data;
10
+ constructor(redactor: DiagnosticRedactor, errorCodes?: readonly string[]);
11
+ record(status: number, headers: Headers, data: unknown): void;
12
+ validationFailed(issues: readonly {
13
+ path: readonly PropertyKey[];
14
+ expected?: unknown;
15
+ code?: unknown;
16
+ }[]): void;
17
+ private shape;
18
+ forError(error: unknown): Record<string, unknown>;
19
+ }
@@ -0,0 +1,115 @@
1
+ const issueCodes = new Set(["invalid_type", "too_big", "too_small", "invalid_format", "not_multiple_of", "unrecognized_keys", "invalid_union", "invalid_key", "invalid_element", "invalid_value", "custom"]);
2
+ const types = new Set(["string", "number", "boolean", "object", "array", "null", "undefined", "bigint", "symbol", "function", "date", "int", "unknown", "never", "void"]);
3
+ function valueType(value) {
4
+ return value === null ? "null" : Array.isArray(value) ? "array" : typeof value;
5
+ }
6
+ function keySegment(key, redactor) {
7
+ const identifier = /^[A-Za-z][A-Za-z0-9_]{0,39}$/.test(key);
8
+ const guid = /^(?:[0-9a-f]{32}|[0-9a-f]{8}(?:[-_][0-9a-f]{4}){3}[-_][0-9a-f]{12})$/i.test(key);
9
+ return `[${JSON.stringify(identifier && !guid ? redactor.text(key) : "<key>")}]`;
10
+ }
11
+ // Emit structure only, never the response body or validation-library errors.
12
+ export class ResponseDiagnostics {
13
+ redactor;
14
+ errorCodes;
15
+ details = {};
16
+ shapeByPath = new Map();
17
+ data;
18
+ constructor(redactor, errorCodes = ["API_RESPONSE_INVALID", "AUTH_RESPONSE_INVALID"]) {
19
+ this.redactor = redactor;
20
+ this.errorCodes = errorCodes;
21
+ }
22
+ record(status, headers, data) {
23
+ this.data = data;
24
+ // Parameters can contain arbitrary server text; report only the bounded media type.
25
+ const mediaType = headers.get("content-type")?.split(";", 1)[0]?.trim();
26
+ const contentType = mediaType && mediaType.length <= 128 && /^[a-z0-9!#$&^_.+\-]+\/[a-z0-9!#$&^_.+\-]+$/i.test(mediaType)
27
+ ? this.redactor.text(mediaType) : null;
28
+ this.details = { status: Number.isInteger(status) && status >= 100 && status <= 599 ? status : null, contentType, ...this.shape(data) };
29
+ }
30
+ validationFailed(issues) {
31
+ const issuePaths = [];
32
+ const annotations = [];
33
+ let truncated = false;
34
+ let characters = 0;
35
+ for (const issue of issues) {
36
+ let path = "$";
37
+ for (const [depth, key] of issue.path.entries()) {
38
+ if (depth >= 4) {
39
+ truncated = true;
40
+ break;
41
+ }
42
+ path += typeof key === "number" && Number.isSafeInteger(key) && key >= 0 ? "[*]" : keySegment(String(key), this.redactor);
43
+ }
44
+ if (issuePaths.length >= 64 || path.length > 256 || characters + path.length > 4096) {
45
+ truncated = true;
46
+ break;
47
+ }
48
+ characters += path.length;
49
+ issuePaths.push(path);
50
+ let received = this.data;
51
+ for (const key of issue.path) {
52
+ received = received !== null && typeof received === "object" && Object.hasOwn(received, key)
53
+ ? received[key] : undefined;
54
+ }
55
+ const expected = typeof issue.expected === "string" && types.has(issue.expected) ? issue.expected : "unknown";
56
+ annotations.push({ path, received: valueType(received), expected, ...(typeof issue.code === "string" && issueCodes.has(issue.code) ? { code: issue.code } : {}), ...(truncated ? { pathTruncated: true } : {}) });
57
+ // Validator messages and arbitrary expected strings can contain response values.
58
+ // Annotate only types from the contract vocabulary, on the recorded received shape.
59
+ if (!truncated && typeof issue.expected === "string" && types.has(issue.expected)) {
60
+ const entry = this.shapeByPath.get(JSON.stringify(issue.path));
61
+ if (entry)
62
+ entry.expected = issue.expected;
63
+ }
64
+ if (truncated)
65
+ break;
66
+ }
67
+ this.details = { ...this.details, issues: annotations, issuePaths, issuePathsTruncated: truncated };
68
+ }
69
+ shape(data) {
70
+ this.shapeByPath.clear();
71
+ const shape = [];
72
+ let truncated = false;
73
+ let characters = 0;
74
+ const visit = (value, path, keys) => {
75
+ if (shape.length >= 64 || path.length > 256 || characters + path.length > 4096) {
76
+ truncated = true;
77
+ return;
78
+ }
79
+ characters += path.length;
80
+ const type = valueType(value);
81
+ const entry = { path, type, ...(Array.isArray(value) ? { count: value.length } : {}) };
82
+ shape.push(entry);
83
+ this.shapeByPath.set(JSON.stringify(keys), entry);
84
+ if (value === null || typeof value !== "object")
85
+ return;
86
+ if (Array.isArray(value)) {
87
+ if (value.length === 0)
88
+ return;
89
+ if (keys.length >= 4) {
90
+ truncated = true;
91
+ return;
92
+ }
93
+ visit(value[0], path + "[*]", [...keys, 0]);
94
+ return;
95
+ }
96
+ for (const key in value) {
97
+ if (!Object.hasOwn(value, key))
98
+ continue;
99
+ if (keys.length >= 4 || shape.length >= 64) {
100
+ truncated = true;
101
+ break;
102
+ }
103
+ const segment = keySegment(key, this.redactor);
104
+ visit(value[key], path + segment, [...keys, key]);
105
+ if (truncated)
106
+ break;
107
+ }
108
+ };
109
+ visit(data, "$", []);
110
+ return { shape, shapeTruncated: truncated };
111
+ }
112
+ forError(error) {
113
+ return error !== null && typeof error === "object" && "code" in error && typeof error.code === "string" && this.errorCodes.includes(error.code) ? this.details : {};
114
+ }
115
+ }
@@ -50,6 +50,23 @@ export declare class TransportError extends Error {
50
50
  status?: number;
51
51
  retryAfter?: number;
52
52
  auditId?: string;
53
+ contentType?: string | null;
54
+ shape?: readonly {
55
+ path: string;
56
+ type: string;
57
+ count?: number;
58
+ expected?: string;
59
+ }[];
60
+ shapeTruncated?: boolean;
61
+ issues?: readonly {
62
+ path: string;
63
+ received: string;
64
+ expected: string;
65
+ code?: string;
66
+ pathTruncated?: boolean;
67
+ }[];
68
+ issuePaths?: readonly string[];
69
+ issuePathsTruncated?: boolean;
53
70
  };
54
71
  readonly help: readonly string[];
55
72
  constructor(error: {
@@ -59,6 +76,23 @@ export declare class TransportError extends Error {
59
76
  status?: number;
60
77
  retryAfter?: number;
61
78
  auditId?: string;
79
+ contentType?: string | null;
80
+ shape?: readonly {
81
+ path: string;
82
+ type: string;
83
+ count?: number;
84
+ expected?: string;
85
+ }[];
86
+ shapeTruncated?: boolean;
87
+ issues?: readonly {
88
+ path: string;
89
+ received: string;
90
+ expected: string;
91
+ code?: string;
92
+ pathTruncated?: boolean;
93
+ }[];
94
+ issuePaths?: readonly string[];
95
+ issuePathsTruncated?: boolean;
62
96
  }, help?: readonly string[]);
63
97
  toJSON(): {
64
98
  help?: readonly string[] | undefined;
@@ -69,6 +103,23 @@ export declare class TransportError extends Error {
69
103
  status?: number;
70
104
  retryAfter?: number;
71
105
  auditId?: string;
106
+ contentType?: string | null;
107
+ shape?: readonly {
108
+ path: string;
109
+ type: string;
110
+ count?: number;
111
+ expected?: string;
112
+ }[];
113
+ shapeTruncated?: boolean;
114
+ issues?: readonly {
115
+ path: string;
116
+ received: string;
117
+ expected: string;
118
+ code?: string;
119
+ pathTruncated?: boolean;
120
+ }[];
121
+ issuePaths?: readonly string[];
122
+ issuePathsTruncated?: boolean;
72
123
  };
73
124
  };
74
125
  }
@@ -1,3 +1,4 @@
1
+ import { ResponseDiagnostics } from "./response-diagnostics.js";
1
2
  export const TENABLE_ORIGIN = "https://cloud.tenable.com";
2
3
  export class TransportError extends Error {
3
4
  error;
@@ -188,15 +189,20 @@ export function constructTransport(options, dependencies) {
188
189
  return { status: response.status, body: file };
189
190
  }
190
191
  let decoded;
192
+ const diagnostics = new ResponseDiagnostics({ text: redact }, ["RESPONSE_INVALID"]);
191
193
  try {
192
194
  decoded = text ? JSON.parse(text) : undefined;
193
195
  }
194
196
  catch {
195
- fail("RESPONSE_INVALID", "Tenable response is not valid JSON", operation, [], { status: response.status });
197
+ diagnostics.record(response.status, response.headers, text);
198
+ fail("RESPONSE_INVALID", "Tenable response is not valid JSON", operation, [], diagnostics.forError({ code: "RESPONSE_INVALID" }));
196
199
  }
197
200
  const result = record.decoder.safeParse(decoded);
198
- if (!result.success)
199
- fail("RESPONSE_INVALID", "Tenable response does not match the selected view", operation, ["Check the operation response contract; no result was inferred"], { status: response.status });
201
+ if (!result.success) {
202
+ diagnostics.record(response.status, response.headers, decoded);
203
+ diagnostics.validationFailed(result.error.issues);
204
+ fail("RESPONSE_INVALID", "Tenable response does not match the selected view", operation, ["Check the operation response contract; no result was inferred"], diagnostics.forError({ code: "RESPONSE_INVALID" }));
205
+ }
200
206
  return { status: response.status, body: result.data };
201
207
  }
202
208
  const status = response.status;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knowttl/tenable-axi",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "Investigate Tenable One exposure and run explicitly guarded writes",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -100,7 +100,7 @@ Exit 0 means success (still inspect completeness), 1 means operation/configurati
100
100
  | `PERMISSION_DENIED` (403) | Check licensing and the operation's privilege; inventory access does not imply attack or connector access. |
101
101
  | `NOT_FOUND` | Verify the exact ID and selected profile; a zero-row list is instead a successful search. |
102
102
  | `RATE_LIMITED`, `REQUEST_FAILED`, `TRANSPORT_FAILED` | Reads already retry 429/502/503/504 up to twice within budgets; honor `retryAfter` milliseconds and investigate persistent failure. |
103
- | `RESPONSE_INVALID` | Treat the result as invalid or partial, preserve evidence and report the mismatch. |
103
+ | `RESPONSE_INVALID` | Treat the result as invalid or partial. For transport JSON/schema failures, report the HTTP status, contentType, bounded shape and issues (received/expected types); values and validator messages are omitted. Later command checks may carry only a fixed message. |
104
104
  | `DEADLINE_EXCEEDED`, `REQUEST_BUDGET_EXCEEDED`, `BYTE_BUDGET_EXCEEDED`, `ROW_BUDGET_EXCEEDED` | Narrow filters/fields or resume a supplied cursor; never infer completeness. |
105
105
  | `EXPORT_NOT_READY` | Inspect job state before downloading; status waiting does not guarantee completion. |
106
106
  | `OPERATION_BLOCKED`, `OUTCOME_UNKNOWN` | Respect write guards; for unknown mutation outcomes follow the audit ID and read-back advice, never replay blindly. |