|
| 1 | +import { z } from "zod"; |
| 2 | + |
| 3 | +import { jsonValueSchema } from "./jsonValue.js"; |
| 4 | + |
| 5 | +/** Supported transport types for custom protocol capture. */ |
| 6 | +export const transportTypeSchema = z.enum([ |
| 7 | + "tcp", |
| 8 | + "udp", |
| 9 | + "ipc", |
| 10 | + "unix-socket", |
| 11 | + "named-pipe", |
| 12 | + "xpc", |
| 13 | +]); |
| 14 | +export type TransportType = z.infer<typeof transportTypeSchema>; |
| 15 | + |
| 16 | +/** Direction of a protocol frame. */ |
| 17 | +export const frameDirectionSchema = z.enum(["sent", "received", "intercepted"]); |
| 18 | +export type FrameDirection = z.infer<typeof frameDirectionSchema>; |
| 19 | + |
| 20 | +/** A captured protocol frame. */ |
| 21 | +export const protocolFrameSchema = z.strictObject({ |
| 22 | + /** Monotonic sequence number. */ |
| 23 | + sequence: z.number().int().nonnegative(), |
| 24 | + /** Timestamp in milliseconds. */ |
| 25 | + at_ms: z.number().int().nonnegative(), |
| 26 | + /** Transport type. */ |
| 27 | + transport: transportTypeSchema, |
| 28 | + /** Direction of the frame. */ |
| 29 | + direction: frameDirectionSchema, |
| 30 | + /** Source process identity. */ |
| 31 | + source_pid: z.number().int().nullable(), |
| 32 | + /** Destination process identity. */ |
| 33 | + dest_pid: z.number().int().nullable(), |
| 34 | + /** Endpoint address (IP:port, socket path, pipe name). */ |
| 35 | + source_endpoint: z.string().nullable(), |
| 36 | + /** Destination endpoint. */ |
| 37 | + dest_endpoint: z.string().nullable(), |
| 38 | + /** Raw frame data as base64. */ |
| 39 | + raw_data: z.string().nullable(), |
| 40 | + /** Decoded/parsed frame content if known. */ |
| 41 | + decoded_content: jsonValueSchema.nullable(), |
| 42 | + /** Frame size in bytes. */ |
| 43 | + size: z.number().int().nonnegative(), |
| 44 | + /** Whether the frame was truncated. */ |
| 45 | + truncated: z.boolean().default(false), |
| 46 | +}); |
| 47 | +export type ProtocolFrame = z.infer<typeof protocolFrameSchema>; |
| 48 | + |
| 49 | +/** Authentication flow stage. */ |
| 50 | +export const authStageSchema = z.enum([ |
| 51 | + "init", |
| 52 | + "challenge", |
| 53 | + "response", |
| 54 | + "token_exchange", |
| 55 | + "renewal", |
| 56 | + "revocation", |
| 57 | + "failure", |
| 58 | + "success", |
| 59 | +]); |
| 60 | +export type AuthStage = z.infer<typeof authStageSchema>; |
| 61 | + |
| 62 | +/** Token lifecycle event. */ |
| 63 | +export const tokenLifecycleEventSchema = z.enum([ |
| 64 | + "issued", |
| 65 | + "refreshed", |
| 66 | + "expired", |
| 67 | + "revoked", |
| 68 | + "renewed", |
| 69 | +]); |
| 70 | +export type TokenLifecycleEvent = z.infer<typeof tokenLifecycleEventSchema>; |
| 71 | + |
| 72 | +/** A captured authentication flow event. */ |
| 73 | +export const authFlowEventSchema = z.strictObject({ |
| 74 | + /** Monotonic sequence number. */ |
| 75 | + sequence: z.number().int().nonnegative(), |
| 76 | + /** Timestamp in milliseconds. */ |
| 77 | + at_ms: z.number().int().nonnegative(), |
| 78 | + /** Authentication stage. */ |
| 79 | + stage: authStageSchema, |
| 80 | + /** Protocol used. */ |
| 81 | + protocol: z.string().min(1), |
| 82 | + /** Token type if applicable. */ |
| 83 | + token_type: z.string().nullable(), |
| 84 | + /** Token lifecycle event. */ |
| 85 | + token_lifecycle: tokenLifecycleEventSchema.nullable(), |
| 86 | + /** Correlation ID linking challenge-response pairs. */ |
| 87 | + correlation_id: z.string().nullable(), |
| 88 | + /** Whether credentials were detected and redacted. */ |
| 89 | + credentials_redacted: z.boolean().default(false), |
| 90 | + /** Whether the authentication succeeded. */ |
| 91 | + succeeded: z.boolean().default(false), |
| 92 | + /** Error message if authentication failed. */ |
| 93 | + error: z.string().nullable(), |
| 94 | +}); |
| 95 | +export type AuthFlowEvent = z.infer<typeof authFlowEventSchema>; |
| 96 | + |
| 97 | +/** A captured protocol session. */ |
| 98 | +export const customProtocolCaptureSchema = z.strictObject({ |
| 99 | + /** Transport type for this session. */ |
| 100 | + transport: transportTypeSchema, |
| 101 | + /** Captured frames in order. */ |
| 102 | + frames: z.array(protocolFrameSchema).min(0).max(100_000), |
| 103 | + /** Authentication flow events. */ |
| 104 | + auth_events: z.array(authFlowEventSchema).default([]), |
| 105 | + /** Whether any frame was truncated. */ |
| 106 | + has_truncated: z.boolean().default(false), |
| 107 | + /** Whether credentials were detected. */ |
| 108 | + credentials_detected: z.boolean().default(false), |
| 109 | +}); |
| 110 | +export type CustomProtocolCapture = z.infer<typeof customProtocolCaptureSchema>; |
| 111 | + |
| 112 | +/** Correlate a frame with process identity. */ |
| 113 | +export function correlateProcessIdentity( |
| 114 | + frame: ProtocolFrame, |
| 115 | + pid: number, |
| 116 | +): boolean { |
| 117 | + return frame.source_pid === pid || frame.dest_pid === pid; |
| 118 | +} |
| 119 | + |
| 120 | +/** Check if a frame contains credential-like content. */ |
| 121 | +export function looksLikeCredentialFrame(frame: ProtocolFrame): boolean { |
| 122 | + const patterns = [ |
| 123 | + "password", |
| 124 | + "token", |
| 125 | + "secret", |
| 126 | + "api_key", |
| 127 | + "apikey", |
| 128 | + "authorization", |
| 129 | + "credential", |
| 130 | + ]; |
| 131 | + const content = JSON.stringify(frame.decoded_content ?? "").toLowerCase(); |
| 132 | + return patterns.some((p) => content.includes(p)); |
| 133 | +} |
| 134 | + |
| 135 | +/** Filter frames by transport type. */ |
| 136 | +export function framesByTransport( |
| 137 | + frames: readonly ProtocolFrame[], |
| 138 | + transport: TransportType, |
| 139 | +): ProtocolFrame[] { |
| 140 | + return frames.filter((f) => f.transport === transport); |
| 141 | +} |
| 142 | + |
| 143 | +/** Get all authentication flow events for a specific protocol. */ |
| 144 | +export function authEventsByProtocol( |
| 145 | + events: readonly AuthFlowEvent[], |
| 146 | + protocol: string, |
| 147 | +): AuthFlowEvent[] { |
| 148 | + return events.filter((e) => e.protocol === protocol); |
| 149 | +} |
| 150 | + |
| 151 | +/** Get successful authentication events. */ |
| 152 | +export function successfulAuthEvents( |
| 153 | + events: readonly AuthFlowEvent[], |
| 154 | +): AuthFlowEvent[] { |
| 155 | + return events.filter((e) => e.succeeded); |
| 156 | +} |
| 157 | + |
| 158 | +/** Get failed authentication events. */ |
| 159 | +export function failedAuthEvents( |
| 160 | + events: readonly AuthFlowEvent[], |
| 161 | +): AuthFlowEvent[] { |
| 162 | + return events.filter((e) => !e.succeeded); |
| 163 | +} |
| 164 | + |
| 165 | +/** Compute the duration of an authentication flow in milliseconds. */ |
| 166 | +export function authFlowDuration( |
| 167 | + events: readonly AuthFlowEvent[], |
| 168 | +): number | null { |
| 169 | + if (events.length === 0) return null; |
| 170 | + const first = events[0]!; |
| 171 | + const last = events[events.length - 1]!; |
| 172 | + return last.at_ms - first.at_ms; |
| 173 | +} |
0 commit comments