@ai-pip/csl 0.1.1 → 0.1.3
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/package.json +10 -28
- package/src/index.test.ts +429 -0
- package/{index.ts → src/index.ts} +53 -50
- package/src/test-external.js +547 -0
- package/layers/csl/adapters/index.ts +0 -9
- package/layers/csl/adapters/input/DOMAdapter.ts +0 -236
- package/layers/csl/adapters/input/UIAdapter.ts +0 -0
- package/layers/csl/adapters/output/ConsoleLogger.ts +0 -34
- package/layers/csl/adapters/output/CryptoHashGenerator.ts +0 -29
- package/layers/csl/adapters/output/FilePolicyRepository.ts +0 -0
- package/layers/csl/adapters/output/InMemoryPolicyRepository.ts +0 -135
- package/layers/csl/adapters/output/SystemTimestampProvider.ts +0 -9
- package/layers/csl/domain/entities/CSLResult.ts +0 -309
- package/layers/csl/domain/entities/Segment.ts +0 -338
- package/layers/csl/domain/entities/index.ts +0 -2
- package/layers/csl/domain/exceptions/ClassificationError.ts +0 -26
- package/layers/csl/domain/exceptions/SegmentationError.ts +0 -30
- package/layers/csl/domain/exceptions/index.ts +0 -2
- package/layers/csl/domain/index.ts +0 -4
- package/layers/csl/domain/services/AnomalyService.ts +0 -255
- package/layers/csl/domain/services/LineageService.ts +0 -224
- package/layers/csl/domain/services/NormalizationService.ts +0 -392
- package/layers/csl/domain/services/OriginClassificationService.ts +0 -69
- package/layers/csl/domain/services/PiDetectionService.ts +0 -475
- package/layers/csl/domain/services/PolicyService.ts +0 -296
- package/layers/csl/domain/services/SegmentClassificationService.ts +0 -105
- package/layers/csl/domain/services/SerializationService.ts +0 -229
- package/layers/csl/domain/services/index.ts +0 -7
- package/layers/csl/domain/value-objects/AnomalyScore.ts +0 -23
- package/layers/csl/domain/value-objects/ContentHash.ts +0 -54
- package/layers/csl/domain/value-objects/LineageEntry.ts +0 -42
- package/layers/csl/domain/value-objects/Origin-map.ts +0 -67
- package/layers/csl/domain/value-objects/Origin.ts +0 -99
- package/layers/csl/domain/value-objects/Pattern.ts +0 -221
- package/layers/csl/domain/value-objects/PiDetection.ts +0 -140
- package/layers/csl/domain/value-objects/PiDetectionResult.ts +0 -275
- package/layers/csl/domain/value-objects/PolicyRule.ts +0 -151
- package/layers/csl/domain/value-objects/TrustLevel.ts +0 -34
- package/layers/csl/domain/value-objects/index.ts +0 -10
- package/layers/csl/index.ts +0 -3
- package/layers/csl/ports/index.ts +0 -10
- package/layers/csl/ports/input/ClassificationPort.ts +0 -76
- package/layers/csl/ports/input/SegmentationPort.ts +0 -81
- package/layers/csl/ports/output/DOMAdapter.ts +0 -14
- package/layers/csl/ports/output/HashGenerator.ts +0 -18
- package/layers/csl/ports/output/Logger.ts +0 -17
- package/layers/csl/ports/output/PolicyRepository.ts +0 -29
- package/layers/csl/ports/output/SegmentClassified.ts +0 -8
- package/layers/csl/ports/output/TimeStampProvider.ts +0 -5
- package/layers/csl/services/CSLService.ts +0 -393
- package/layers/csl/services/index.ts +0 -1
- package/layers/csl/types/entities-types.ts +0 -37
- package/layers/csl/types/index.ts +0 -4
- package/layers/csl/types/pi-types.ts +0 -111
- package/layers/csl/types/port-output-types.ts +0 -17
- package/layers/csl/types/value-objects-types.ts +0 -213
- package/layers/csl/utils/colors.ts +0 -25
- package/layers/csl/utils/pattern-helpers.ts +0 -174
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* LoggerPort is the port for the logger
|
|
5
|
-
*
|
|
6
|
-
* @property info - Info
|
|
7
|
-
* @property warn - Warn
|
|
8
|
-
* @property error - Error
|
|
9
|
-
* @property debug - Debug
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
export interface LoggerPort {
|
|
13
|
-
info(msg: string): void;
|
|
14
|
-
warn(msg: string): void;
|
|
15
|
-
error(msg: string): void;
|
|
16
|
-
debug(msg: string): void;
|
|
17
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import type { PolicyRule } from '../../domain/value-objects';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* PolicyRepositoryPort is the port for the policy repository
|
|
5
|
-
*
|
|
6
|
-
* @property getActivePolicy - Get the currently active policy
|
|
7
|
-
* @property getPolicyByVersion - Get a policy by its version
|
|
8
|
-
* @property getAllPolicies - Get all available policies
|
|
9
|
-
*/
|
|
10
|
-
export interface PolicyRepositoryPort {
|
|
11
|
-
/**
|
|
12
|
-
* Get the currently active policy
|
|
13
|
-
* @returns The active PolicyRule
|
|
14
|
-
*/
|
|
15
|
-
getActivePolicy(): Promise<PolicyRule>;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Get a policy by its version
|
|
19
|
-
* @param version - The version of the policy to retrieve
|
|
20
|
-
* @returns The PolicyRule for the specified version, or null if not found
|
|
21
|
-
*/
|
|
22
|
-
getPolicyByVersion(version: string): Promise<PolicyRule | null>;
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Get all available policies
|
|
26
|
-
* @returns An array of all available PolicyRule instances
|
|
27
|
-
*/
|
|
28
|
-
getAllPolicies(): Promise<PolicyRule[]>;
|
|
29
|
-
}
|
|
@@ -1,393 +0,0 @@
|
|
|
1
|
-
import type { SegmentationPort } from '../ports/input/SegmentationPort'
|
|
2
|
-
import { CSLResult } from '../domain/entities/CSLResult'
|
|
3
|
-
import type { Segment } from '../domain/entities'
|
|
4
|
-
import { SegmentationError } from '../domain/exceptions'
|
|
5
|
-
import { DOMAdapter } from '../adapters/input/DOMAdapter'
|
|
6
|
-
import {
|
|
7
|
-
NormalizationService,
|
|
8
|
-
OriginClassificationService,
|
|
9
|
-
PiDetectionService,
|
|
10
|
-
AnomalyService,
|
|
11
|
-
PolicyService,
|
|
12
|
-
LineageService,
|
|
13
|
-
} from '../domain/services'
|
|
14
|
-
import type { HashGeneratorPort } from '../ports/output/HashGenerator'
|
|
15
|
-
import type { LoggerPort } from '../ports/output/Logger'
|
|
16
|
-
import type { TimeStampProviderPort } from '../ports/output/TimeStampProvider'
|
|
17
|
-
import { LineageEntry, ContentHash } from '../domain/value-objects'
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Configuration for CSLService
|
|
21
|
-
*/
|
|
22
|
-
export interface CSLServiceConfig {
|
|
23
|
-
readonly enablePolicyValidation?: boolean
|
|
24
|
-
readonly enableLineageTracking?: boolean
|
|
25
|
-
readonly hashAlgorithm?: 'sha256' | 'sha512'
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* CSLService is the main orchestrator service for the Context Segmentation Layer.
|
|
30
|
-
*
|
|
31
|
-
* @remarks
|
|
32
|
-
* This service implements the SegmentationPort and coordinates the complete CSL processing pipeline.
|
|
33
|
-
* It orchestrates all domain services, adapters, and output ports to process content from DOM
|
|
34
|
-
* elements or documents through the full segmentation, classification, and analysis pipeline.
|
|
35
|
-
*
|
|
36
|
-
* **Processing Pipeline:**
|
|
37
|
-
* 1. Adapts DOM element/document to extract content segments (DOMAdapter)
|
|
38
|
-
* 2. Normalizes segment content (NormalizationService)
|
|
39
|
-
* 3. Classifies segments by origin (OriginClassificationService)
|
|
40
|
-
* 4. Detects prompt injection attempts (PiDetectionService)
|
|
41
|
-
* 5. Generates cryptographic hashes (HashGeneratorPort)
|
|
42
|
-
* 6. Calculates anomaly scores (AnomalyService)
|
|
43
|
-
* 7. Validates against policies (PolicyService, optional)
|
|
44
|
-
* 8. Tracks lineage for auditability (LineageService)
|
|
45
|
-
* 9. Aggregates results into CSLResult
|
|
46
|
-
*
|
|
47
|
-
* **Key Features:**
|
|
48
|
-
* - Complete pipeline orchestration
|
|
49
|
-
* - Error handling with SegmentationError
|
|
50
|
-
* - Configurable processing options
|
|
51
|
-
* - Performance tracking (processing time)
|
|
52
|
-
* - Comprehensive lineage tracking
|
|
53
|
-
*
|
|
54
|
-
* **Dependencies:**
|
|
55
|
-
* - Domain Services: Normalization, Classification, PI Detection, Anomaly, Policy, Lineage
|
|
56
|
-
* - Adapters: DOMAdapter (input)
|
|
57
|
-
* - Ports: HashGenerator, Logger, TimeStampProvider (output)
|
|
58
|
-
*
|
|
59
|
-
* @example
|
|
60
|
-
* ```typescript
|
|
61
|
-
* // Create CSLService with all dependencies
|
|
62
|
-
* const cslService = new CSLService(
|
|
63
|
-
* new DOMAdapter(),
|
|
64
|
-
* new OriginClassificationService(),
|
|
65
|
-
* new PiDetectionService(),
|
|
66
|
-
* new AnomalyService(),
|
|
67
|
-
* new PolicyService(policyRepository),
|
|
68
|
-
* new LineageService(),
|
|
69
|
-
* new CryptoHashGenerator(),
|
|
70
|
-
* new ConsoleLogger(),
|
|
71
|
-
* new SystemTimestampProvider(),
|
|
72
|
-
* { enablePolicyValidation: true }
|
|
73
|
-
* )
|
|
74
|
-
*
|
|
75
|
-
* // Process a DOM element
|
|
76
|
-
* const result = await cslService.segment(document.body)
|
|
77
|
-
*
|
|
78
|
-
* // Access results
|
|
79
|
-
* console.log(`Processed ${result.metadata.total_segments} segments`)
|
|
80
|
-
* console.log(`Blocked: ${result.getBlockedCount()} segments`)
|
|
81
|
-
* ```
|
|
82
|
-
*/
|
|
83
|
-
export class CSLService implements SegmentationPort {
|
|
84
|
-
private readonly config: Required<CSLServiceConfig>
|
|
85
|
-
|
|
86
|
-
constructor(
|
|
87
|
-
private readonly domAdapter: DOMAdapter,
|
|
88
|
-
private readonly originClassificationService: OriginClassificationService,
|
|
89
|
-
private readonly piDetectionService: PiDetectionService,
|
|
90
|
-
private readonly anomalyService: AnomalyService,
|
|
91
|
-
private readonly policyService: PolicyService,
|
|
92
|
-
private readonly lineageService: LineageService,
|
|
93
|
-
private readonly hashGenerator: HashGeneratorPort,
|
|
94
|
-
private readonly logger: LoggerPort,
|
|
95
|
-
private readonly timestampProvider: TimeStampProviderPort,
|
|
96
|
-
config?: CSLServiceConfig
|
|
97
|
-
) {
|
|
98
|
-
// Validate required dependencies
|
|
99
|
-
if (!domAdapter) {
|
|
100
|
-
throw new TypeError('CSLService: domAdapter is required')
|
|
101
|
-
}
|
|
102
|
-
if (!originClassificationService) {
|
|
103
|
-
throw new TypeError('CSLService: originClassificationService is required')
|
|
104
|
-
}
|
|
105
|
-
if (!piDetectionService) {
|
|
106
|
-
throw new TypeError('CSLService: piDetectionService is required')
|
|
107
|
-
}
|
|
108
|
-
if (!anomalyService) {
|
|
109
|
-
throw new TypeError('CSLService: anomalyService is required')
|
|
110
|
-
}
|
|
111
|
-
if (!policyService) {
|
|
112
|
-
throw new TypeError('CSLService: policyService is required')
|
|
113
|
-
}
|
|
114
|
-
if (!lineageService) {
|
|
115
|
-
throw new TypeError('CSLService: lineageService is required')
|
|
116
|
-
}
|
|
117
|
-
if (!hashGenerator) {
|
|
118
|
-
throw new TypeError('CSLService: hashGenerator is required')
|
|
119
|
-
}
|
|
120
|
-
if (!logger) {
|
|
121
|
-
throw new TypeError('CSLService: logger is required')
|
|
122
|
-
}
|
|
123
|
-
if (!timestampProvider) {
|
|
124
|
-
throw new TypeError('CSLService: timestampProvider is required')
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
this.config = {
|
|
128
|
-
enablePolicyValidation: config?.enablePolicyValidation ?? true,
|
|
129
|
-
enableLineageTracking: config?.enableLineageTracking ?? true,
|
|
130
|
-
hashAlgorithm: config?.hashAlgorithm ?? 'sha256',
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Segments content from a DOM element or document through the CSL processing pipeline.
|
|
136
|
-
*
|
|
137
|
-
* @param element - The HTMLElement or Document to segment
|
|
138
|
-
* @returns Promise that resolves to CSLResult with all processed segments
|
|
139
|
-
* @throws {SegmentationError} If segmentation fails
|
|
140
|
-
*
|
|
141
|
-
* @example
|
|
142
|
-
* ```typescript
|
|
143
|
-
* const result = await cslService.segment(document.body)
|
|
144
|
-
* ```
|
|
145
|
-
*/
|
|
146
|
-
async segment(element: HTMLElement | Document): Promise<CSLResult> {
|
|
147
|
-
const startTime = this.timestampProvider.now()
|
|
148
|
-
|
|
149
|
-
try {
|
|
150
|
-
this.logger.debug(`CSLService: Starting segmentation for element`)
|
|
151
|
-
|
|
152
|
-
// Step 1: Adapt DOM to segments
|
|
153
|
-
const segments = this.domAdapter.adapt(element)
|
|
154
|
-
this.logger.debug(`CSLService: Extracted ${segments.length} segments from DOM`)
|
|
155
|
-
|
|
156
|
-
if (segments.length === 0) {
|
|
157
|
-
this.logger.warn('CSLService: No segments extracted from DOM element')
|
|
158
|
-
return this.createEmptyResult(startTime)
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
// Step 2: Process each segment through the pipeline
|
|
162
|
-
const processedSegments: Segment[] = []
|
|
163
|
-
const blockedSegmentIds: string[] = []
|
|
164
|
-
const allLineageEntries: LineageEntry[] = []
|
|
165
|
-
|
|
166
|
-
for (const segment of segments) {
|
|
167
|
-
try {
|
|
168
|
-
const processedSegment = await this.processSegment(segment, startTime)
|
|
169
|
-
processedSegments.push(processedSegment)
|
|
170
|
-
|
|
171
|
-
// Track blocked segments
|
|
172
|
-
if (processedSegment.shouldBlock()) {
|
|
173
|
-
blockedSegmentIds.push(processedSegment.id)
|
|
174
|
-
this.logger.warn(`CSLService: Segment ${processedSegment.id} blocked due to security concerns`)
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// Collect lineage entries
|
|
178
|
-
if (this.config.enableLineageTracking && processedSegment.lineage) {
|
|
179
|
-
allLineageEntries.push(...processedSegment.lineage)
|
|
180
|
-
}
|
|
181
|
-
} catch (error) {
|
|
182
|
-
this.logger.error(`CSLService: Error processing segment ${segment.id}: ${error}`)
|
|
183
|
-
// Continue processing other segments even if one fails
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
// Step 3: Calculate processing time
|
|
188
|
-
const processingTime = this.timestampProvider.now() - startTime
|
|
189
|
-
|
|
190
|
-
// Step 4: Create CSLResult
|
|
191
|
-
const result = new CSLResult({
|
|
192
|
-
segments: processedSegments,
|
|
193
|
-
metadata: {
|
|
194
|
-
blocked_segments: blockedSegmentIds,
|
|
195
|
-
processing_time_ms: processingTime,
|
|
196
|
-
},
|
|
197
|
-
lineage: allLineageEntries,
|
|
198
|
-
})
|
|
199
|
-
|
|
200
|
-
this.logger.info(
|
|
201
|
-
`CSLService: Segmentation completed. Processed ${processedSegments.length} segments in ${processingTime}ms`
|
|
202
|
-
)
|
|
203
|
-
|
|
204
|
-
return result
|
|
205
|
-
} catch (error) {
|
|
206
|
-
const errorMessage = error instanceof Error ? error.message : 'Unknown error during segmentation'
|
|
207
|
-
this.logger.error(`CSLService: Segmentation failed: ${errorMessage}`)
|
|
208
|
-
throw new SegmentationError(`Failed to segment content: ${errorMessage}`, error)
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
/**
|
|
213
|
-
* Processes a single segment through the complete pipeline
|
|
214
|
-
*/
|
|
215
|
-
private async processSegment(segment: Segment, startTime: number): Promise<Segment> {
|
|
216
|
-
const segmentStartTime = this.timestampProvider.now()
|
|
217
|
-
|
|
218
|
-
// Step 1: Normalize content
|
|
219
|
-
const normalizedContent = this.normalizeSegmentContent(segment)
|
|
220
|
-
|
|
221
|
-
// Step 2: Classify origin and assign trust level
|
|
222
|
-
this.classifySegment(segment)
|
|
223
|
-
|
|
224
|
-
// Step 3: Detect prompt injection
|
|
225
|
-
this.detectPromptInjection(segment, normalizedContent)
|
|
226
|
-
|
|
227
|
-
// Step 4: Generate hash
|
|
228
|
-
await this.generateSegmentHash(segment, normalizedContent)
|
|
229
|
-
|
|
230
|
-
// Step 5: Calculate anomaly score
|
|
231
|
-
this.calculateSegmentAnomalyScore(segment)
|
|
232
|
-
|
|
233
|
-
// Step 6: Validate policies (optional)
|
|
234
|
-
if (this.config.enablePolicyValidation) {
|
|
235
|
-
await this.validateSegmentPolicy(segment)
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
// Step 7: Add lineage entries to segment
|
|
239
|
-
if (this.config.enableLineageTracking) {
|
|
240
|
-
this.addLineageToSegment(segment)
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
const segmentProcessingTime = this.timestampProvider.now() - segmentStartTime
|
|
244
|
-
this.logger.debug(`CSLService: Processed segment ${segment.id} in ${segmentProcessingTime}ms`)
|
|
245
|
-
|
|
246
|
-
return segment
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
/**
|
|
250
|
-
* Normalizes segment content and tracks lineage
|
|
251
|
-
*/
|
|
252
|
-
private normalizeSegmentContent(segment: Segment): string {
|
|
253
|
-
const normalizedContent = NormalizationService.normalize(segment.content)
|
|
254
|
-
|
|
255
|
-
if (this.config.enableLineageTracking) {
|
|
256
|
-
this.lineageService.addEntry(
|
|
257
|
-
segment.id,
|
|
258
|
-
new LineageEntry('normalization', this.timestampProvider.now(), 'Content normalized')
|
|
259
|
-
)
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
return normalizedContent
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
* Classifies segment origin and assigns trust level
|
|
267
|
-
*/
|
|
268
|
-
private classifySegment(segment: Segment): void {
|
|
269
|
-
const trustLevel = this.originClassificationService.classify(segment.origin)
|
|
270
|
-
segment.assignTrustLevel(trustLevel)
|
|
271
|
-
|
|
272
|
-
if (this.config.enableLineageTracking) {
|
|
273
|
-
this.lineageService.addEntry(
|
|
274
|
-
segment.id,
|
|
275
|
-
new LineageEntry(
|
|
276
|
-
'classification',
|
|
277
|
-
this.timestampProvider.now(),
|
|
278
|
-
`Classified as ${trustLevel.value}`
|
|
279
|
-
)
|
|
280
|
-
)
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
/**
|
|
285
|
-
* Detects prompt injection in segment
|
|
286
|
-
*/
|
|
287
|
-
private detectPromptInjection(segment: Segment, normalizedContent: string): void {
|
|
288
|
-
const piDetection = this.piDetectionService.detect(normalizedContent)
|
|
289
|
-
segment.assignPiDetection(piDetection)
|
|
290
|
-
|
|
291
|
-
if (this.config.enableLineageTracking) {
|
|
292
|
-
this.lineageService.addEntry(
|
|
293
|
-
segment.id,
|
|
294
|
-
new LineageEntry(
|
|
295
|
-
'pi_detection',
|
|
296
|
-
this.timestampProvider.now(),
|
|
297
|
-
`PI detection: ${piDetection.action} (score: ${piDetection.score.toFixed(2)})`
|
|
298
|
-
)
|
|
299
|
-
)
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
/**
|
|
304
|
-
* Generates hash for segment content
|
|
305
|
-
*/
|
|
306
|
-
private async generateSegmentHash(segment: Segment, normalizedContent: string): Promise<void> {
|
|
307
|
-
const hashValue = await this.hashGenerator.generate(normalizedContent, this.config.hashAlgorithm)
|
|
308
|
-
const contentHash = new ContentHash(hashValue, this.config.hashAlgorithm)
|
|
309
|
-
segment.assignHash(contentHash)
|
|
310
|
-
|
|
311
|
-
if (this.config.enableLineageTracking) {
|
|
312
|
-
this.lineageService.addEntry(
|
|
313
|
-
segment.id,
|
|
314
|
-
new LineageEntry(
|
|
315
|
-
'hash_generation',
|
|
316
|
-
this.timestampProvider.now(),
|
|
317
|
-
`Hash generated: ${this.config.hashAlgorithm}`
|
|
318
|
-
)
|
|
319
|
-
)
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
/**
|
|
324
|
-
* Calculates anomaly score for segment
|
|
325
|
-
*/
|
|
326
|
-
private calculateSegmentAnomalyScore(segment: Segment): void {
|
|
327
|
-
const anomalyScore = this.anomalyService.calculateScore(segment)
|
|
328
|
-
segment.assignAnomalyScore(anomalyScore)
|
|
329
|
-
|
|
330
|
-
if (this.config.enableLineageTracking) {
|
|
331
|
-
this.lineageService.addEntry(
|
|
332
|
-
segment.id,
|
|
333
|
-
new LineageEntry(
|
|
334
|
-
'anomaly_calculation',
|
|
335
|
-
this.timestampProvider.now(),
|
|
336
|
-
`Anomaly score: ${anomalyScore.score.toFixed(2)} (${anomalyScore.action})`
|
|
337
|
-
)
|
|
338
|
-
)
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
/**
|
|
343
|
-
* Validates segment against policies
|
|
344
|
-
*/
|
|
345
|
-
private async validateSegmentPolicy(segment: Segment): Promise<void> {
|
|
346
|
-
try {
|
|
347
|
-
const policyResult = await this.policyService.validateSegment(segment)
|
|
348
|
-
if (!policyResult.isValid) {
|
|
349
|
-
this.logger.warn(
|
|
350
|
-
`CSLService: Segment ${segment.id} has ${policyResult.violations.length} policy violations`
|
|
351
|
-
)
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
if (this.config.enableLineageTracking) {
|
|
355
|
-
this.lineageService.addEntry(
|
|
356
|
-
segment.id,
|
|
357
|
-
new LineageEntry(
|
|
358
|
-
'policy_validation',
|
|
359
|
-
this.timestampProvider.now(),
|
|
360
|
-
`Policy validation: ${policyResult.isValid ? 'PASS' : 'FAIL'} (${policyResult.violations.length} violations)`
|
|
361
|
-
)
|
|
362
|
-
)
|
|
363
|
-
}
|
|
364
|
-
} catch (error) {
|
|
365
|
-
this.logger.error(`CSLService: Policy validation failed for segment ${segment.id}: ${error}`)
|
|
366
|
-
// Continue processing even if policy validation fails
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
/**
|
|
371
|
-
* Adds lineage entries from LineageService to segment
|
|
372
|
-
*/
|
|
373
|
-
private addLineageToSegment(segment: Segment): void {
|
|
374
|
-
const segmentLineage = this.lineageService.getLineage(segment.id)
|
|
375
|
-
segmentLineage.forEach(entry => segment.addLineageEntry(entry))
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
/**
|
|
379
|
-
* Creates an empty CSLResult when no segments are found
|
|
380
|
-
*/
|
|
381
|
-
private createEmptyResult(startTime: number): CSLResult {
|
|
382
|
-
const processingTime = this.timestampProvider.now() - startTime
|
|
383
|
-
|
|
384
|
-
return new CSLResult({
|
|
385
|
-
segments: [],
|
|
386
|
-
metadata: {
|
|
387
|
-
blocked_segments: [],
|
|
388
|
-
processing_time_ms: processingTime,
|
|
389
|
-
},
|
|
390
|
-
lineage: [],
|
|
391
|
-
})
|
|
392
|
-
}
|
|
393
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './CSLService';
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SegmentMetadata contains additional metadata about a content segment
|
|
3
|
-
*
|
|
4
|
-
* @property domPath - XPath or CSS selector path where the segment was extracted from the DOM
|
|
5
|
-
* @property length - Length of the content in characters
|
|
6
|
-
* @property isVisible - Whether the segment is visible in the DOM
|
|
7
|
-
* @property isHidden - Whether the segment is hidden in the DOM
|
|
8
|
-
*/
|
|
9
|
-
export interface SegmentMetadata {
|
|
10
|
-
domPath?: string
|
|
11
|
-
length?: number
|
|
12
|
-
isVisible?: boolean
|
|
13
|
-
isHidden?: boolean
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* CSLResultMetadata contains aggregated statistics about the segmentation process
|
|
20
|
-
*
|
|
21
|
-
* @property total_segments - Total number of segments processed (calculated automatically from segments.length)
|
|
22
|
-
* @property trust_distribution - Distribution of segments by trust level (calculated automatically from segments' TrustLevel values)
|
|
23
|
-
* @property anomaly_score - Average anomaly score across all segments (0-1), calculated as sum(segment.anomalyScore.score) / total_segments
|
|
24
|
-
* @property blocked_segments - Array of segment IDs that should be blocked
|
|
25
|
-
* @property processing_time_ms - Total processing time in milliseconds
|
|
26
|
-
*/
|
|
27
|
-
export interface CSLResultMetadata {
|
|
28
|
-
total_segments: number
|
|
29
|
-
trust_distribution: {
|
|
30
|
-
TC: number
|
|
31
|
-
STC: number
|
|
32
|
-
UC: number
|
|
33
|
-
}
|
|
34
|
-
anomaly_score: number
|
|
35
|
-
blocked_segments: string[]
|
|
36
|
-
processing_time_ms: number
|
|
37
|
-
}
|
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import type { Pattern } from '../domain/value-objects'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* PiDetectionConfig interface defines configuration options for PiDetectionService
|
|
5
|
-
*
|
|
6
|
-
* @remarks
|
|
7
|
-
* This configuration allows fine-tuning of prompt injection detection behavior.
|
|
8
|
-
* All thresholds and enable/disable flags can be customized to match specific
|
|
9
|
-
* security requirements and use cases.
|
|
10
|
-
*
|
|
11
|
-
* **Default Behavior:**
|
|
12
|
-
* If not provided, the service will use sensible defaults:
|
|
13
|
-
* - highConfidenceThreshold: 0.7
|
|
14
|
-
* - mediumConfidenceThreshold: 0.3
|
|
15
|
-
* - All detection types enabled by default
|
|
16
|
-
* - No custom patterns
|
|
17
|
-
*
|
|
18
|
-
* @property highConfidenceThreshold - Score threshold for BLOCK action (default: 0.7)
|
|
19
|
-
* @property mediumConfidenceThreshold - Score threshold for WARN action (default: 0.3)
|
|
20
|
-
* @property enableInstructionOverride - Enable detection of instruction override patterns
|
|
21
|
-
* @property enableRoleSwapping - Enable detection of role swapping patterns
|
|
22
|
-
* @property enablePrivilegeEscalation - Enable detection of privilege escalation patterns
|
|
23
|
-
* @property enableJailbreak - Enable detection of jailbreak phrases
|
|
24
|
-
* @property enableInvisibleChars - Enable detection of invisible character manipulations
|
|
25
|
-
* @property customPatterns - Optional array of custom Pattern objects for additional detection
|
|
26
|
-
*
|
|
27
|
-
* @example
|
|
28
|
-
* ```typescript
|
|
29
|
-
* // Default configuration (all enabled)
|
|
30
|
-
* const defaultConfig: PiDetectionConfig = {
|
|
31
|
-
* highConfidenceThreshold: 0.7,
|
|
32
|
-
* mediumConfidenceThreshold: 0.3,
|
|
33
|
-
* enableInstructionOverride: true,
|
|
34
|
-
* enableRoleSwapping: true,
|
|
35
|
-
* enablePrivilegeEscalation: true,
|
|
36
|
-
* enableJailbreak: true,
|
|
37
|
-
* enableInvisibleChars: true
|
|
38
|
-
* }
|
|
39
|
-
*
|
|
40
|
-
* // Custom configuration with custom patterns
|
|
41
|
-
* const customConfig: PiDetectionConfig = {
|
|
42
|
-
* highConfidenceThreshold: 0.8,
|
|
43
|
-
* mediumConfidenceThreshold: 0.4,
|
|
44
|
-
* enableInstructionOverride: true,
|
|
45
|
-
* enableRoleSwapping: true,
|
|
46
|
-
* enablePrivilegeEscalation: true,
|
|
47
|
-
* enableJailbreak: false,
|
|
48
|
-
* enableInvisibleChars: true,
|
|
49
|
-
* customPatterns: [
|
|
50
|
-
* new Pattern('custom_attack', /specific\s+attack\s+pattern/i, 0.9)
|
|
51
|
-
* ]
|
|
52
|
-
* }
|
|
53
|
-
* ```
|
|
54
|
-
*/
|
|
55
|
-
export interface PiDetectionConfig {
|
|
56
|
-
/**
|
|
57
|
-
* Score threshold for BLOCK action
|
|
58
|
-
* Scores >= this value will result in BLOCK action
|
|
59
|
-
* @default 0.7
|
|
60
|
-
*/
|
|
61
|
-
readonly highConfidenceThreshold: number
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Score threshold for WARN action
|
|
65
|
-
* Scores >= this value but < highConfidenceThreshold will result in WARN action
|
|
66
|
-
* @default 0.3
|
|
67
|
-
*/
|
|
68
|
-
readonly mediumConfidenceThreshold: number
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Enable detection of instruction override patterns
|
|
72
|
-
* Examples: "ignore previous instructions", "forget everything"
|
|
73
|
-
* @default true
|
|
74
|
-
*/
|
|
75
|
-
readonly enableInstructionOverride: boolean
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Enable detection of role swapping patterns
|
|
79
|
-
* Examples: "you are no longer an AI", "act as if you are..."
|
|
80
|
-
* @default true
|
|
81
|
-
*/
|
|
82
|
-
readonly enableRoleSwapping: boolean
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Enable detection of privilege escalation patterns
|
|
86
|
-
* Examples: "execute as administrator", "bypass safety restrictions"
|
|
87
|
-
* @default true
|
|
88
|
-
*/
|
|
89
|
-
readonly enablePrivilegeEscalation: boolean
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Enable detection of jailbreak phrases
|
|
93
|
-
* Examples: "DAN mode", "developer mode", "jailbreak mode"
|
|
94
|
-
* @default true
|
|
95
|
-
*/
|
|
96
|
-
readonly enableJailbreak: boolean
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Enable detection of invisible character manipulations
|
|
100
|
-
* Detects zero-width characters and homoglyphs
|
|
101
|
-
* @default true
|
|
102
|
-
*/
|
|
103
|
-
readonly enableInvisibleChars: boolean
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Optional array of custom Pattern objects for additional detection
|
|
107
|
-
* These patterns will be evaluated in addition to built-in patterns
|
|
108
|
-
* @default undefined (no custom patterns)
|
|
109
|
-
*/
|
|
110
|
-
readonly customPatterns?: readonly Pattern[]
|
|
111
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* RawDomPayload is the payload of the raw dom
|
|
4
|
-
*
|
|
5
|
-
* @remarks
|
|
6
|
-
* This is the payload of the raw dom used in the DOMAdapterPort
|
|
7
|
-
*/
|
|
8
|
-
export interface RawDomPayload {
|
|
9
|
-
html: string;
|
|
10
|
-
textNodes: string[];
|
|
11
|
-
hiddenTextNodes: string[];
|
|
12
|
-
inputs: Record<string, string>;
|
|
13
|
-
cssInline: string[];
|
|
14
|
-
cssStyleTags: string[];
|
|
15
|
-
cssExternal: string[];
|
|
16
|
-
pseudoContent: string[];
|
|
17
|
-
}
|