@ai-pip/core 0.1.3 → 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.
- package/README.md +91 -30
- package/dist/cpe/index.d.ts +2 -2
- package/dist/cpe/index.d.ts.map +1 -1
- package/dist/cpe/index.js +1 -3
- package/dist/cpe/index.js.map +1 -1
- package/dist/cpe/value-objects/Metadata.js +1 -1
- package/dist/cpe/value-objects/index.d.ts +5 -3
- package/dist/cpe/value-objects/index.d.ts.map +1 -1
- package/dist/cpe/value-objects/index.js +3 -3
- package/dist/cpe/value-objects/index.js.map +1 -1
- package/dist/csl/exceptions/index.d.ts +2 -2
- package/dist/csl/exceptions/index.d.ts.map +1 -1
- package/dist/csl/exceptions/index.js +2 -2
- package/dist/csl/exceptions/index.js.map +1 -1
- package/dist/csl/index.d.ts +6 -3
- package/dist/csl/index.d.ts.map +1 -1
- package/dist/csl/index.js +4 -4
- package/dist/csl/index.js.map +1 -1
- package/dist/index.d.ts +15 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +16 -8
- package/dist/index.js.map +1 -1
- package/dist/isl/exceptions/index.d.ts +1 -1
- package/dist/isl/exceptions/index.d.ts.map +1 -1
- package/dist/isl/exceptions/index.js +1 -1
- package/dist/isl/exceptions/index.js.map +1 -1
- package/dist/isl/index.d.ts +4 -3
- package/dist/isl/index.d.ts.map +1 -1
- package/dist/isl/index.js +2 -5
- package/dist/isl/index.js.map +1 -1
- package/dist/shared/index.d.ts +1 -1
- package/dist/shared/index.d.ts.map +1 -1
- package/dist/shared/index.js +1 -1
- package/dist/shared/index.js.map +1 -1
- package/package.json +1 -2
- package/src/cpe/envelope.ts +0 -115
- package/src/cpe/exceptions/EnvelopeError.ts +0 -11
- package/src/cpe/exceptions/index.ts +0 -6
- package/src/cpe/index.ts +0 -35
- package/src/cpe/types.ts +0 -68
- package/src/cpe/utils.ts +0 -65
- package/src/cpe/value-objects/Metadata.ts +0 -78
- package/src/cpe/value-objects/Nonce.ts +0 -57
- package/src/cpe/value-objects/Signature.ts +0 -83
- package/src/cpe/value-objects/index.ts +0 -8
- package/src/csl/classify.ts +0 -77
- package/src/csl/exceptions/ClassificationError.ts +0 -16
- package/src/csl/exceptions/SegmentationError.ts +0 -19
- package/src/csl/exceptions/index.ts +0 -3
- package/src/csl/index.ts +0 -34
- package/src/csl/lineage.ts +0 -40
- package/src/csl/segment.ts +0 -100
- package/src/csl/types.ts +0 -113
- package/src/csl/utils.ts +0 -30
- package/src/csl/value-objects/ContentHash.ts +0 -48
- package/src/csl/value-objects/LineageEntry.ts +0 -33
- package/src/csl/value-objects/Origin-map.ts +0 -51
- package/src/csl/value-objects/Origin.ts +0 -52
- package/src/csl/value-objects/TrustLevel.ts +0 -33
- package/src/csl/value-objects/index.ts +0 -14
- package/src/index.ts +0 -20
- package/src/isl/exceptions/SanitizationError.ts +0 -14
- package/src/isl/exceptions/index.ts +0 -2
- package/src/isl/index.ts +0 -20
- package/src/isl/sanitize.ts +0 -93
- package/src/isl/types.ts +0 -87
- package/src/isl/value-objects/AnomalyScore.ts +0 -40
- package/src/isl/value-objects/Pattern.ts +0 -158
- package/src/isl/value-objects/PiDetection.ts +0 -92
- package/src/isl/value-objects/PiDetectionResult.ts +0 -129
- package/src/isl/value-objects/PolicyRule.ts +0 -117
- package/src/isl/value-objects/index.ts +0 -41
- package/src/shared/index.ts +0 -13
- package/src/shared/lineage.ts +0 -53
package/src/csl/lineage.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { createLineageEntry as createEntry } from './value-objects/LineageEntry'
|
|
2
|
-
import type { LineageEntry } from './value-objects/LineageEntry'
|
|
3
|
-
import type { CSLSegment } from './types'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Inicializa el linaje para un segmento - función pura
|
|
7
|
-
*
|
|
8
|
-
* @remarks
|
|
9
|
-
* Crea la entrada inicial de linaje cuando se crea un segmento en CSL.
|
|
10
|
-
* El core solo registra step y timestamp, sin notes.
|
|
11
|
-
*
|
|
12
|
-
* @param segment - El segmento para el cual inicializar el linaje
|
|
13
|
-
* @returns Array con la entrada inicial de linaje
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* ```typescript
|
|
17
|
-
* const lineage = initLineage(segment)
|
|
18
|
-
* // Returns: [{ step: 'CSL', timestamp: ... }]
|
|
19
|
-
* ```
|
|
20
|
-
*/
|
|
21
|
-
export function initLineage(_segment: CSLSegment): LineageEntry[] {
|
|
22
|
-
return [
|
|
23
|
-
createEntry('CSL', Date.now())
|
|
24
|
-
]
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Crea una entrada de linaje - función pura
|
|
29
|
-
*
|
|
30
|
-
* @remarks
|
|
31
|
-
* El core solo registra step y timestamp.
|
|
32
|
-
* Notes y metadata van en el SDK para observabilidad.
|
|
33
|
-
*
|
|
34
|
-
* @param step - Nombre del paso de procesamiento
|
|
35
|
-
* @returns Nueva entrada de linaje
|
|
36
|
-
*/
|
|
37
|
-
export function createLineageEntry(step: string): LineageEntry {
|
|
38
|
-
return createEntry(step, Date.now())
|
|
39
|
-
}
|
|
40
|
-
|
package/src/csl/segment.ts
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import type { CSLInput, CSLResult, CSLSegment } from './types'
|
|
2
|
-
import { classifySource } from './classify'
|
|
3
|
-
import { initLineage } from './lineage'
|
|
4
|
-
import { generateId, splitByContextRules } from './utils'
|
|
5
|
-
import { SegmentationError } from './exceptions'
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Segmenta input en segmentos semánticos - función pura principal de CSL
|
|
9
|
-
*
|
|
10
|
-
* @remarks
|
|
11
|
-
* Esta es la función principal de CSL. Segmenta el contenido, clasifica
|
|
12
|
-
* por origen, e inicializa el linaje. Todo de forma pura y determinista.
|
|
13
|
-
*
|
|
14
|
-
* **Invariantes preservados:**
|
|
15
|
-
* - El contenido original nunca se pierde
|
|
16
|
-
* - El orden de segmentos es estable
|
|
17
|
-
* - Todo segmento tiene linaje inicial
|
|
18
|
-
* - CSL es determinista
|
|
19
|
-
*
|
|
20
|
-
* @param input - Input con contenido y source
|
|
21
|
-
* @returns CSLResult con segmentos clasificados y linaje inicializado
|
|
22
|
-
*
|
|
23
|
-
* @throws {SegmentationError} Si la segmentación falla
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* ```typescript
|
|
27
|
-
* const result = segment({
|
|
28
|
-
* content: 'Hello\nWorld',
|
|
29
|
-
* source: 'UI',
|
|
30
|
-
* metadata: {}
|
|
31
|
-
* })
|
|
32
|
-
*
|
|
33
|
-
* // result.segments contiene 2 segmentos, cada uno con:
|
|
34
|
-
* // - content original
|
|
35
|
-
* // - trust level clasificado
|
|
36
|
-
* // - lineage inicializado
|
|
37
|
-
* ```
|
|
38
|
-
*/
|
|
39
|
-
export function segment(input: CSLInput): CSLResult {
|
|
40
|
-
try {
|
|
41
|
-
// 1. Validar input
|
|
42
|
-
if (!input.content || typeof input.content !== 'string') {
|
|
43
|
-
throw new SegmentationError('CSLInput content must be a non-empty string')
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (!input.source) {
|
|
47
|
-
throw new SegmentationError('CSLInput source is required')
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// 2. Dividir contenido en segmentos estructurales (función pura)
|
|
51
|
-
// CSL solo segmenta por estructura, no por intención semántica
|
|
52
|
-
const contentSegments = splitByContextRules(input.content)
|
|
53
|
-
|
|
54
|
-
// 3. Si no hay contenido, retornar resultado vacío
|
|
55
|
-
if (contentSegments.length === 0) {
|
|
56
|
-
return {
|
|
57
|
-
segments: Object.freeze([]),
|
|
58
|
-
lineage: Object.freeze([])
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// 4. Clasificar source una vez (determinista)
|
|
63
|
-
const trust = classifySource(input.source)
|
|
64
|
-
|
|
65
|
-
// 5. Crear segmentos con clasificación y linaje
|
|
66
|
-
const segments: CSLSegment[] = contentSegments.map((content) => {
|
|
67
|
-
// Crear segmento temporal para inicializar linaje
|
|
68
|
-
const tempSegment: CSLSegment = {
|
|
69
|
-
id: generateId(),
|
|
70
|
-
content, // ✅ Original preservado
|
|
71
|
-
source: input.source, // ✅ Origen preservado
|
|
72
|
-
trust, // ✅ Clasificación determinista
|
|
73
|
-
lineage: [], // Se inicializa después
|
|
74
|
-
...(input.metadata && { metadata: input.metadata })
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// Inicializar linaje
|
|
78
|
-
const lineage = initLineage(tempSegment)
|
|
79
|
-
|
|
80
|
-
// Retornar segmento completo
|
|
81
|
-
return {
|
|
82
|
-
...tempSegment,
|
|
83
|
-
lineage // ✅ Linaje inicializado
|
|
84
|
-
}
|
|
85
|
-
})
|
|
86
|
-
|
|
87
|
-
// 6. Recolectar todo el linaje
|
|
88
|
-
const allLineage = segments.flatMap(s => s.lineage)
|
|
89
|
-
|
|
90
|
-
// 7. Retornar resultado puro
|
|
91
|
-
return {
|
|
92
|
-
segments: Object.freeze(segments),
|
|
93
|
-
lineage: Object.freeze(allLineage)
|
|
94
|
-
}
|
|
95
|
-
} catch (error) {
|
|
96
|
-
const errorMessage = error instanceof Error ? error.message : 'Unknown error during segmentation'
|
|
97
|
-
throw new SegmentationError(`Failed to segment content: ${errorMessage}`, error)
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
package/src/csl/types.ts
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Types for CSL (Context Segmentation Layer) - Core Semántico
|
|
3
|
-
*
|
|
4
|
-
* Solo tipos esenciales para CSL. Tipos relacionados con detección,
|
|
5
|
-
* anomalías y políticas van a ISL.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* OriginType represents the deterministic source of a content segment.
|
|
10
|
-
*/
|
|
11
|
-
export enum OriginType {
|
|
12
|
-
/**
|
|
13
|
-
* Direct user input from UI controls
|
|
14
|
-
* Always classified as UC (Untrusted Content) for security.
|
|
15
|
-
*/
|
|
16
|
-
USER = 'USER',
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Content from visible DOM elements
|
|
20
|
-
* Classified as STC (Semi-Trusted Content) because user can verify it.
|
|
21
|
-
*/
|
|
22
|
-
DOM_VISIBLE = 'DOM_VISIBLE',
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Content from hidden DOM elements
|
|
26
|
-
* Classified as UC (Untrusted Content) - potential attack vector.
|
|
27
|
-
*/
|
|
28
|
-
DOM_HIDDEN = 'DOM_HIDDEN',
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Content from DOM attributes (data-*, aria-*, etc.)
|
|
32
|
-
* Classified as STC (Semi-Trusted Content) - visible in source.
|
|
33
|
-
*/
|
|
34
|
-
DOM_ATTRIBUTE = 'DOM_ATTRIBUTE',
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Content injected by scripts (dynamically generated)
|
|
38
|
-
* Classified as UC (Untrusted Content) - can be manipulated.
|
|
39
|
-
*/
|
|
40
|
-
SCRIPT_INJECTED = 'SCRIPT_INJECTED',
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Content fetched from network (API calls, external resources)
|
|
44
|
-
* Classified as UC (Untrusted Content) - external source, not verified.
|
|
45
|
-
*/
|
|
46
|
-
NETWORK_FETCHED = 'NETWORK_FETCHED',
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* System-generated content (instructions, system prompts, etc.)
|
|
50
|
-
* Classified as TC (Trusted Content) - system controls this content.
|
|
51
|
-
*/
|
|
52
|
-
SYSTEM_GENERATED = 'SYSTEM_GENERATED',
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Origin cannot be determined
|
|
56
|
-
* Classified as UC (Untrusted Content) - unknown is untrusted by default.
|
|
57
|
-
*/
|
|
58
|
-
UNKNOWN = 'UNKNOWN',
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* TrustLevelType represents the trust level of content
|
|
63
|
-
*/
|
|
64
|
-
export enum TrustLevelType {
|
|
65
|
-
TC = 'TC', // Trusted Content
|
|
66
|
-
STC = 'STC', // Semi-Trusted Content
|
|
67
|
-
UC = 'UC', // Untrusted Content
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* HashAlgorithm for ContentHash (opcional, para trazabilidad)
|
|
72
|
-
*/
|
|
73
|
-
export type HashAlgorithm = 'sha256' | 'sha512'
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Source type for CSL input
|
|
77
|
-
*/
|
|
78
|
-
export type Source = 'DOM' | 'UI' | 'SYSTEM' | 'API'
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* CSLInput - Input para la función segment()
|
|
82
|
-
*/
|
|
83
|
-
export interface CSLInput {
|
|
84
|
-
readonly content: string
|
|
85
|
-
readonly source: Source
|
|
86
|
-
readonly metadata?: Record<string, unknown>
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// Importar tipos de value objects para usar en interfaces
|
|
90
|
-
import type { ContentHash, LineageEntry, TrustLevel } from './value-objects'
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* CSLSegment - Segmento puro, solo datos semánticos
|
|
94
|
-
*/
|
|
95
|
-
export interface CSLSegment {
|
|
96
|
-
readonly id: string
|
|
97
|
-
readonly content: string // Original, sin modificar
|
|
98
|
-
readonly source: Source
|
|
99
|
-
readonly trust: TrustLevel // Clasificado por origen
|
|
100
|
-
readonly lineage: LineageEntry[] // Inicializado en CSL
|
|
101
|
-
readonly hash?: ContentHash // Opcional, para trazabilidad
|
|
102
|
-
readonly metadata?: Record<string, unknown>
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* CSLResult - Resultado puro, solo datos
|
|
107
|
-
*/
|
|
108
|
-
export interface CSLResult {
|
|
109
|
-
readonly segments: readonly CSLSegment[]
|
|
110
|
-
readonly lineage: readonly LineageEntry[]
|
|
111
|
-
readonly processingTimeMs?: number // Opcional, para métricas
|
|
112
|
-
}
|
|
113
|
-
|
package/src/csl/utils.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Utility functions for CSL - funciones puras
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Generates a unique ID for a segment
|
|
7
|
-
*/
|
|
8
|
-
export function generateId(): string {
|
|
9
|
-
return `seg-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Splits content by context rules - función pura de segmentación
|
|
14
|
-
*
|
|
15
|
-
* @remarks
|
|
16
|
-
* Segmentación básica por líneas. Sin normalización agresiva
|
|
17
|
-
* (la normalización va a ISL).
|
|
18
|
-
*/
|
|
19
|
-
export function splitByContextRules(content: string): string[] {
|
|
20
|
-
if (content.length === 0) {
|
|
21
|
-
return []
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// Segmentación básica por líneas
|
|
25
|
-
return content
|
|
26
|
-
.split(/\n+/)
|
|
27
|
-
.map(line => line.trim())
|
|
28
|
-
.filter(line => line.length > 0)
|
|
29
|
-
}
|
|
30
|
-
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import type { HashAlgorithm } from '../types'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* ContentHash - tipo puro
|
|
5
|
-
*/
|
|
6
|
-
export type ContentHash = {
|
|
7
|
-
readonly value: string
|
|
8
|
-
readonly algorithm: HashAlgorithm
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Crea un ContentHash - función pura
|
|
13
|
-
*/
|
|
14
|
-
export function createContentHash(value: string, algorithm: HashAlgorithm = 'sha256'): ContentHash {
|
|
15
|
-
if (!value || typeof value !== 'string') {
|
|
16
|
-
throw new Error('ContentHash value must be a non-empty string')
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
if (!['sha256', 'sha512'].includes(algorithm)) {
|
|
20
|
-
throw new Error(`Invalid HashAlgorithm: ${algorithm}. Must be 'sha256' or 'sha512'`)
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const hexPattern = /^[a-f0-9]+$/i
|
|
24
|
-
if (!hexPattern.test(value)) {
|
|
25
|
-
throw new Error('ContentHash value must be a valid hexadecimal string')
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const expectedLength = algorithm === 'sha256' ? 64 : 128
|
|
29
|
-
if (value.length !== expectedLength) {
|
|
30
|
-
throw new Error(`ContentHash value length must be ${expectedLength} characters for ${algorithm}`)
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return {
|
|
34
|
-
value: value.toLowerCase(),
|
|
35
|
-
algorithm
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Funciones puras para ContentHash
|
|
41
|
-
*/
|
|
42
|
-
export function isSha256(hash: ContentHash): boolean {
|
|
43
|
-
return hash.algorithm === 'sha256'
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export function isSha512(hash: ContentHash): boolean {
|
|
47
|
-
return hash.algorithm === 'sha512'
|
|
48
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* LineageEntry - tipo puro
|
|
3
|
-
*
|
|
4
|
-
* @remarks
|
|
5
|
-
* El core semántico solo preserva linaje estructural.
|
|
6
|
-
* Notes libres son para observabilidad (SDK), no core.
|
|
7
|
-
*/
|
|
8
|
-
export type LineageEntry = {
|
|
9
|
-
readonly step: string
|
|
10
|
-
readonly timestamp: number
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Crea un LineageEntry - función pura
|
|
15
|
-
*
|
|
16
|
-
* @remarks
|
|
17
|
-
* El core solo registra step y timestamp.
|
|
18
|
-
* Notes y metadata van en el SDK para observabilidad.
|
|
19
|
-
*/
|
|
20
|
-
export function createLineageEntry(step: string, timestamp: number): LineageEntry {
|
|
21
|
-
if (!step || typeof step !== 'string' || step.trim().length === 0) {
|
|
22
|
-
throw new Error('LineageEntry step must be a non-empty string')
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
if (typeof timestamp !== 'number' || timestamp < 0 || !Number.isFinite(timestamp)) {
|
|
26
|
-
throw new Error('LineageEntry timestamp must be a valid positive number')
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return {
|
|
30
|
-
step: step.trim(),
|
|
31
|
-
timestamp
|
|
32
|
-
}
|
|
33
|
-
}
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { OriginType, TrustLevelType } from '../types'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* originMap is the deterministic mapping from OriginType to TrustLevelType.
|
|
5
|
-
*
|
|
6
|
-
* @remarks
|
|
7
|
-
* This map defines the **deterministic classification rules** for content segments.
|
|
8
|
-
* The mapping is based solely on the origin type, not on content analysis.
|
|
9
|
-
*
|
|
10
|
-
* **Key Principles:**
|
|
11
|
-
* - 100% deterministic: same origin → same trust level, always
|
|
12
|
-
* - No heuristics or content analysis
|
|
13
|
-
* - All OriginType values must be present in this map
|
|
14
|
-
*/
|
|
15
|
-
export const originMap = new Map<OriginType, TrustLevelType>([
|
|
16
|
-
// User origins - always untrusted (security by default)
|
|
17
|
-
[OriginType.USER, TrustLevelType.UC],
|
|
18
|
-
|
|
19
|
-
// DOM origins - trust based on visibility
|
|
20
|
-
[OriginType.DOM_VISIBLE, TrustLevelType.STC],
|
|
21
|
-
[OriginType.DOM_HIDDEN, TrustLevelType.UC],
|
|
22
|
-
[OriginType.DOM_ATTRIBUTE, TrustLevelType.STC],
|
|
23
|
-
|
|
24
|
-
// External origins - always untrusted
|
|
25
|
-
[OriginType.SCRIPT_INJECTED, TrustLevelType.UC],
|
|
26
|
-
[OriginType.NETWORK_FETCHED, TrustLevelType.UC],
|
|
27
|
-
|
|
28
|
-
// System origins - trusted (system controls)
|
|
29
|
-
[OriginType.SYSTEM_GENERATED, TrustLevelType.TC],
|
|
30
|
-
|
|
31
|
-
// Unknown - untrusted by default (fail-secure)
|
|
32
|
-
[OriginType.UNKNOWN, TrustLevelType.UC],
|
|
33
|
-
])
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Validates that all OriginType values are mapped in originMap.
|
|
37
|
-
*
|
|
38
|
-
* @throws {Error} If any OriginType is not present in originMap
|
|
39
|
-
*/
|
|
40
|
-
export function validateOriginMap(): void {
|
|
41
|
-
const allOriginTypes = Object.values(OriginType)
|
|
42
|
-
const missingTypes = allOriginTypes.filter(type => !originMap.has(type))
|
|
43
|
-
|
|
44
|
-
if (missingTypes.length > 0) {
|
|
45
|
-
throw new Error(
|
|
46
|
-
`Missing origin mappings: ${missingTypes.join(', ')}. ` +
|
|
47
|
-
`All OriginType values must be mapped in originMap.`
|
|
48
|
-
)
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { OriginType } from '../types'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Origin - tipo puro
|
|
5
|
-
*/
|
|
6
|
-
export type Origin = {
|
|
7
|
-
readonly type: OriginType
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Crea un Origin - función pura
|
|
12
|
-
*/
|
|
13
|
-
export function createOrigin(type: OriginType): Origin {
|
|
14
|
-
if (!Object.values(OriginType).includes(type)) {
|
|
15
|
-
throw new Error(`Invalid Origin type: ${type}`)
|
|
16
|
-
}
|
|
17
|
-
return { type }
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Funciones puras para Origin
|
|
22
|
-
*/
|
|
23
|
-
export function isDom(origin: Origin): boolean {
|
|
24
|
-
return origin.type === OriginType.DOM_HIDDEN ||
|
|
25
|
-
origin.type === OriginType.DOM_VISIBLE ||
|
|
26
|
-
origin.type === OriginType.DOM_ATTRIBUTE
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function isUser(origin: Origin): boolean {
|
|
30
|
-
return origin.type === OriginType.USER
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function isSystem(origin: Origin): boolean {
|
|
34
|
-
return origin.type === OriginType.SYSTEM_GENERATED
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export function isInjected(origin: Origin): boolean {
|
|
38
|
-
return origin.type === OriginType.SCRIPT_INJECTED
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export function isUnknown(origin: Origin): boolean {
|
|
42
|
-
return origin.type === OriginType.UNKNOWN
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export function isNetworkFetched(origin: Origin): boolean {
|
|
46
|
-
return origin.type === OriginType.NETWORK_FETCHED
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export function isExternal(origin: Origin): boolean {
|
|
50
|
-
return origin.type === OriginType.NETWORK_FETCHED ||
|
|
51
|
-
origin.type === OriginType.SCRIPT_INJECTED
|
|
52
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { TrustLevelType } from '../types'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* TrustLevel - tipo puro
|
|
5
|
-
*/
|
|
6
|
-
export type TrustLevel = {
|
|
7
|
-
readonly value: TrustLevelType
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Crea un TrustLevel - función pura
|
|
12
|
-
*/
|
|
13
|
-
export function createTrustLevel(value: TrustLevelType): TrustLevel {
|
|
14
|
-
if (!Object.values(TrustLevelType).includes(value)) {
|
|
15
|
-
throw new Error(`Invalid TrustLevel: ${value}`)
|
|
16
|
-
}
|
|
17
|
-
return { value }
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Funciones puras para TrustLevel
|
|
22
|
-
*/
|
|
23
|
-
export function isTrusted(trust: TrustLevel): boolean {
|
|
24
|
-
return trust.value === TrustLevelType.TC
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function isSemiTrusted(trust: TrustLevel): boolean {
|
|
28
|
-
return trust.value === TrustLevelType.STC
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function isUntrusted(trust: TrustLevel): boolean {
|
|
32
|
-
return trust.value === TrustLevelType.UC
|
|
33
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
// Tipos
|
|
2
|
-
export type { TrustLevel } from './TrustLevel'
|
|
3
|
-
export type { Origin } from './Origin'
|
|
4
|
-
export type { LineageEntry } from './LineageEntry'
|
|
5
|
-
export type { ContentHash } from './ContentHash'
|
|
6
|
-
|
|
7
|
-
// Funciones de creación
|
|
8
|
-
export { createTrustLevel, isTrusted, isSemiTrusted, isUntrusted } from './TrustLevel'
|
|
9
|
-
export { createOrigin, isDom, isUser, isSystem, isInjected, isUnknown, isNetworkFetched, isExternal } from './Origin'
|
|
10
|
-
export { createLineageEntry } from './LineageEntry'
|
|
11
|
-
export { createContentHash, isSha256, isSha512 } from './ContentHash'
|
|
12
|
-
|
|
13
|
-
// Origin-map
|
|
14
|
-
export { originMap, validateOriginMap } from './Origin-map'
|
package/src/index.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @ai-pip/core - Core implementation of the AI-PIP protocol
|
|
3
|
-
*
|
|
4
|
-
* @remarks
|
|
5
|
-
* Main entry point that re-exports all layers (CSL, ISL, Shared)
|
|
6
|
-
*
|
|
7
|
-
* You can also import specific layers:
|
|
8
|
-
* - import { segment } from '@ai-pip/core/csl'
|
|
9
|
-
* - import { sanitize } from '@ai-pip/core/isl'
|
|
10
|
-
* - import { addLineageEntry } from '@ai-pip/core/shared'
|
|
11
|
-
* - import { envelope } from '@ai-pip/core/cpe'
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
// Re-export all layers
|
|
15
|
-
export * from './csl'
|
|
16
|
-
export * from './isl'
|
|
17
|
-
export * from './shared'
|
|
18
|
-
export * from './cpe'
|
|
19
|
-
|
|
20
|
-
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SanitizationError is thrown when sanitization fails.
|
|
3
|
-
*/
|
|
4
|
-
export class SanitizationError extends Error {
|
|
5
|
-
constructor(
|
|
6
|
-
message: string,
|
|
7
|
-
public readonly cause?: unknown
|
|
8
|
-
) {
|
|
9
|
-
super(message)
|
|
10
|
-
this.name = 'SanitizationError'
|
|
11
|
-
Object.setPrototypeOf(this, SanitizationError.prototype)
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
package/src/isl/index.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ISL (Instruction Sanitization Layer) - Core Semántico
|
|
3
|
-
*
|
|
4
|
-
* @remarks
|
|
5
|
-
* ISL sanitiza instrucciones maliciosas recibidas de CSL,
|
|
6
|
-
* aplicando diferentes niveles de sanitización según el nivel de confianza.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
// Funciones puras principales
|
|
10
|
-
export { sanitize } from './sanitize'
|
|
11
|
-
|
|
12
|
-
// Value objects
|
|
13
|
-
export * from './value-objects'
|
|
14
|
-
|
|
15
|
-
// Exceptions
|
|
16
|
-
export * from './exceptions'
|
|
17
|
-
|
|
18
|
-
// Types
|
|
19
|
-
export * from './types'
|
|
20
|
-
|
package/src/isl/sanitize.ts
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import type { CSLResult } from '../csl/types'
|
|
2
|
-
import type { ISLResult, ISLSegment, RemovedInstruction } from './types'
|
|
3
|
-
import { createLineageEntry } from '../csl/value-objects'
|
|
4
|
-
import { addLineageEntry } from '../shared/lineage'
|
|
5
|
-
import type { TrustLevel } from '../csl/value-objects'
|
|
6
|
-
import { TrustLevelType } from '../csl/types'
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Sanitiza contenido según nivel de confianza - función pura
|
|
10
|
-
*
|
|
11
|
-
* @remarks
|
|
12
|
-
* ISL aplica sanitización diferenciada según el trust level:
|
|
13
|
-
* - TC: Sanitización mínima
|
|
14
|
-
* - STC: Sanitización moderada
|
|
15
|
-
* - UC: Sanitización agresiva
|
|
16
|
-
*/
|
|
17
|
-
export function sanitize(cslResult: CSLResult): ISLResult {
|
|
18
|
-
const segments: ISLSegment[] = []
|
|
19
|
-
let allLineage: typeof cslResult.lineage = [...cslResult.lineage]
|
|
20
|
-
const blockedCount = 0
|
|
21
|
-
let instructionsRemovedCount = 0
|
|
22
|
-
|
|
23
|
-
for (const cslSegment of cslResult.segments) {
|
|
24
|
-
// Determinar nivel de sanitización según trust level
|
|
25
|
-
const sanitizationLevel = getSanitizationLevel(cslSegment.trust)
|
|
26
|
-
|
|
27
|
-
// Sanitizar contenido según nivel
|
|
28
|
-
const sanitized = sanitizeContent(
|
|
29
|
-
cslSegment.content,
|
|
30
|
-
sanitizationLevel
|
|
31
|
-
)
|
|
32
|
-
|
|
33
|
-
// Detectar instrucciones removidas (esto se implementará con detección de PI)
|
|
34
|
-
const removedInstructions: RemovedInstruction[] = []
|
|
35
|
-
|
|
36
|
-
// Crear segmento sanitizado
|
|
37
|
-
const islSegment: ISLSegment = {
|
|
38
|
-
id: cslSegment.id,
|
|
39
|
-
originalContent: cslSegment.content, // ✅ Preservar original
|
|
40
|
-
sanitizedContent: sanitized.content,
|
|
41
|
-
trust: cslSegment.trust,
|
|
42
|
-
lineage: addLineageEntry(
|
|
43
|
-
cslSegment.lineage,
|
|
44
|
-
createLineageEntry('ISL', Date.now())
|
|
45
|
-
),
|
|
46
|
-
instructionsRemoved: removedInstructions,
|
|
47
|
-
sanitizationLevel
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
segments.push(islSegment)
|
|
51
|
-
const lastLineageEntry = islSegment.lineage.at(-1)
|
|
52
|
-
if (lastLineageEntry) {
|
|
53
|
-
allLineage = addLineageEntry(allLineage, lastLineageEntry)
|
|
54
|
-
}
|
|
55
|
-
instructionsRemovedCount += removedInstructions.length
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return {
|
|
59
|
-
segments: Object.freeze(segments),
|
|
60
|
-
lineage: Object.freeze(allLineage),
|
|
61
|
-
metadata: {
|
|
62
|
-
totalSegments: segments.length,
|
|
63
|
-
sanitizedSegments: segments.length,
|
|
64
|
-
blockedSegments: blockedCount,
|
|
65
|
-
instructionsRemoved: instructionsRemovedCount
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Determina nivel de sanitización según trust level - función pura
|
|
72
|
-
*/
|
|
73
|
-
function getSanitizationLevel(trust: TrustLevel): 'minimal' | 'moderate' | 'aggressive' {
|
|
74
|
-
if (trust.value === TrustLevelType.TC) return 'minimal'
|
|
75
|
-
if (trust.value === TrustLevelType.STC) return 'moderate'
|
|
76
|
-
return 'aggressive' // UC
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Sanitiza contenido según nivel - función pura
|
|
81
|
-
*/
|
|
82
|
-
function sanitizeContent(
|
|
83
|
-
content: string,
|
|
84
|
-
_level: 'minimal' | 'moderate' | 'aggressive'
|
|
85
|
-
): { content: string; removed: RemovedInstruction[] } {
|
|
86
|
-
// Por ahora retorna el contenido sin cambios
|
|
87
|
-
// La lógica de sanitización real se implementará después
|
|
88
|
-
return {
|
|
89
|
-
content,
|
|
90
|
-
removed: []
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|