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57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
"""
osint_core.intent
=================

Intent packet modeling for the Enterprise Drift-Aware OSINT Control Fabric.

This module turns a validated user request into an explicit, bounded, signed
intent packet. It does not execute actions, perform network calls, mutate
policy, update memory, or write audit records.
"""

from __future__ import annotations

import hashlib
import hmac
import json
import os
import re
import uuid
from dataclasses import asdict, dataclass, field, replace
from datetime import datetime, timezone
from enum import Enum
from typing import Any, Iterable, Literal, Mapping


IntentAction = Literal[
    "enrich_indicator",
    "generate_report",
    "update_sources",
    "run_module",
    "export_audit",
]

RiskLabel = Literal["low", "medium", "high", "critical"]
RollbackStrategy = Literal["none", "observe_only", "disable_module", "sandbox", "revert"]
IndicatorType = Literal["domain", "username", "email", "ip", "url", "unknown"]


class IntentErrorCode(str, Enum):
    MISSING_FIELD = "missing_field"
    INVALID_ACTION = "invalid_action"
    INVALID_RISK = "invalid_risk"
    INVALID_SCOPE = "invalid_scope"
    INVALID_ROLLBACK = "invalid_rollback"
    FORBIDDEN_OPERATION_REQUESTED = "forbidden_operation_requested"
    RAW_INDICATOR_LEAK = "raw_indicator_leak"
    UNSIGNED_PACKET = "unsigned_packet"
    SIGNATURE_MISMATCH = "signature_mismatch"
    SECRET_MISSING = "secret_missing"


class IntentValidationError(ValueError):
    def __init__(self, message: str, code: IntentErrorCode):
        super().__init__(message)
        self.code = code


@dataclass(frozen=True)
class IntentScope:
    """Explicit scope boundary for an intent.

    target_hash must be a hash/HMAC of the target. Raw indicators do not belong
    in scope objects or intent packets.
    """

    target_hash: str
    indicator_type: IndicatorType
    allowed_operations: tuple[str, ...]
    forbidden_operations: tuple[str, ...] = field(default_factory=tuple)
    time_horizon_seconds: int = 300
    success_criteria: tuple[str, ...] = field(default_factory=tuple)

    def to_dict(self) -> dict[str, Any]:
        return asdict(self)


@dataclass(frozen=True)
class IntentPacket:
    """Signed, immutable description of a requested action."""

    intent_id: str
    action: IntentAction
    purpose: str
    scope: IntentScope
    requested_modules: tuple[str, ...]
    expected_side_effects: tuple[str, ...]
    rollback_strategy: RollbackStrategy
    risk_label: RiskLabel
    manifest_hash: str
    created_at: str
    schema_version: str = "1.0.0"
    signature: str | None = None

    def unsigned_payload(self) -> dict[str, Any]:
        payload = asdict(self)
        payload.pop("signature", None)
        return payload

    def to_dict(self, include_signature: bool = True) -> dict[str, Any]:
        payload = asdict(self)
        if not include_signature:
            payload.pop("signature", None)
        return payload

    def to_json(self, include_signature: bool = True) -> str:
        return canonical_json(self.to_dict(include_signature=include_signature))


@dataclass(frozen=True)
class IntentValidationResult:
    ok: bool
    errors: tuple[str, ...] = field(default_factory=tuple)
    error_codes: tuple[IntentErrorCode, ...] = field(default_factory=tuple)


VALID_ACTIONS: set[str] = {
    "enrich_indicator",
    "generate_report",
    "update_sources",
    "run_module",
    "export_audit",
}

VALID_RISKS: set[str] = {"low", "medium", "high", "critical"}
VALID_ROLLBACK_STRATEGIES: set[str] = {
    "none",
    "observe_only",
    "disable_module",
    "sandbox",
    "revert",
}

DEFAULT_FORBIDDEN_OPERATIONS: tuple[str, ...] = (
    "port_scan",
    "mass_scan",
    "brute_force",
    "credential_testing",
    "exploitation",
    "directory_fuzzing",
    "web_vulnerability_scan",
    "password_spray",
    "login_attempt",
)

RAW_FIELD_NAMES: set[str] = {
    "raw_indicator",
    "raw_input",
    "indicator",
    "target",
    "domain",
    "email",
    "username",
    "url",
    "ip",
}

HASH_RE = re.compile(r"^[a-fA-F0-9]{32,128}$")


def now_utc() -> str:
    return datetime.now(timezone.utc).isoformat()


def canonical_json(value: Mapping[str, Any]) -> str:
    """Return deterministic JSON for signing and hashing."""
    return json.dumps(value, sort_keys=True, separators=(",", ":"), ensure_ascii=False)


def get_intent_signing_secret() -> str:
    """Return signing secret.

    Production should use INTENT_SIGNING_SECRET. Smaller deployments may fall
    back to OSINT_HASH_SALT, but separating both is preferred.
    """
    secret = os.getenv("INTENT_SIGNING_SECRET") or os.getenv("OSINT_HASH_SALT")
    if not secret:
        raise IntentValidationError(
            "Missing INTENT_SIGNING_SECRET or OSINT_HASH_SALT.",
            IntentErrorCode.SECRET_MISSING,
        )
    return secret


def hash_manifest_payload(payload: Mapping[str, Any]) -> str:
    return hashlib.sha256(canonical_json(payload).encode("utf-8")).hexdigest()


def sign_payload(payload: Mapping[str, Any], secret: str | None = None) -> str:
    secret = secret or get_intent_signing_secret()
    return hmac.new(
        secret.encode("utf-8"),
        canonical_json(payload).encode("utf-8"),
        hashlib.sha256,
    ).hexdigest()


def sign_intent(packet: IntentPacket, secret: str | None = None) -> IntentPacket:
    signature = sign_payload(packet.unsigned_payload(), secret=secret)
    return replace(packet, signature=signature)


def verify_intent_signature(packet: IntentPacket, secret: str | None = None) -> bool:
    if not packet.signature:
        raise IntentValidationError("Intent packet is unsigned.", IntentErrorCode.UNSIGNED_PACKET)

    expected = sign_payload(packet.unsigned_payload(), secret=secret)
    if not hmac.compare_digest(expected, packet.signature):
        raise IntentValidationError(
            "Intent signature mismatch.",
            IntentErrorCode.SIGNATURE_MISMATCH,
        )
    return True


def make_scope(
    *,
    target_hash: str,
    indicator_type: IndicatorType,
    allowed_operations: Iterable[str],
    forbidden_operations: Iterable[str] | None = None,
    time_horizon_seconds: int = 300,
    success_criteria: Iterable[str] | None = None,
) -> IntentScope:
    merged_forbidden = tuple(
        dedupe_preserve_order(
            [
                *(forbidden_operations or ()),
                *DEFAULT_FORBIDDEN_OPERATIONS,
            ]
        )
    )

    scope = IntentScope(
        target_hash=target_hash,
        indicator_type=indicator_type,
        allowed_operations=tuple(dedupe_preserve_order(allowed_operations)),
        forbidden_operations=merged_forbidden,
        time_horizon_seconds=time_horizon_seconds,
        success_criteria=tuple(success_criteria or ()),
    )
    validate_scope_or_raise(scope)
    return scope


def create_intent_packet(
    *,
    action: IntentAction,
    purpose: str,
    scope: IntentScope,
    requested_modules: Iterable[str],
    expected_side_effects: Iterable[str] | None = None,
    rollback_strategy: RollbackStrategy = "observe_only",
    risk_label: RiskLabel = "low",
    manifest_hash: str,
    intent_id: str | None = None,
    created_at: str | None = None,
    sign: bool = True,
    signing_secret: str | None = None,
) -> IntentPacket:
    packet = IntentPacket(
        intent_id=intent_id or f"intent_{uuid.uuid4().hex}",
        action=action,
        purpose=purpose.strip(),
        scope=scope,
        requested_modules=tuple(dedupe_preserve_order(requested_modules)),
        expected_side_effects=tuple(expected_side_effects or ()),
        rollback_strategy=rollback_strategy,
        risk_label=risk_label,
        manifest_hash=manifest_hash,
        created_at=created_at or now_utc(),
    )

    validate_intent_or_raise(packet)

    if sign:
        packet = sign_intent(packet, secret=signing_secret)
        verify_intent_signature(packet, secret=signing_secret)

    return packet


def validate_scope(scope: IntentScope) -> IntentValidationResult:
    errors: list[str] = []
    codes: list[IntentErrorCode] = []

    if not scope.target_hash:
        errors.append("scope.target_hash is required.")
        codes.append(IntentErrorCode.MISSING_FIELD)
    elif not HASH_RE.fullmatch(scope.target_hash):
        errors.append("scope.target_hash must look like a cryptographic hash.")
        codes.append(IntentErrorCode.INVALID_SCOPE)

    if scope.indicator_type not in {"domain", "username", "email", "ip", "url", "unknown"}:
        errors.append("scope.indicator_type is invalid.")
        codes.append(IntentErrorCode.INVALID_SCOPE)

    if not scope.allowed_operations:
        errors.append("scope.allowed_operations must not be empty.")
        codes.append(IntentErrorCode.MISSING_FIELD)

    if scope.time_horizon_seconds <= 0 or scope.time_horizon_seconds > 86_400:
        errors.append("scope.time_horizon_seconds must be between 1 and 86400.")
        codes.append(IntentErrorCode.INVALID_SCOPE)

    overlap = set(scope.allowed_operations).intersection(scope.forbidden_operations)
    if overlap:
        errors.append(f"Allowed operations include forbidden operation(s): {sorted(overlap)}")
        codes.append(IntentErrorCode.FORBIDDEN_OPERATION_REQUESTED)

    return IntentValidationResult(ok=not errors, errors=tuple(errors), error_codes=tuple(codes))


def validate_scope_or_raise(scope: IntentScope) -> None:
    result = validate_scope(scope)
    if not result.ok:
        raise IntentValidationError(result.errors[0], result.error_codes[0])


def validate_intent(packet: IntentPacket) -> IntentValidationResult:
    errors: list[str] = []
    codes: list[IntentErrorCode] = []

    if packet.action not in VALID_ACTIONS:
        errors.append(f"Invalid action: {packet.action}")
        codes.append(IntentErrorCode.INVALID_ACTION)

    if not packet.purpose:
        errors.append("purpose is required.")
        codes.append(IntentErrorCode.MISSING_FIELD)

    if packet.risk_label not in VALID_RISKS:
        errors.append(f"Invalid risk label: {packet.risk_label}")
        codes.append(IntentErrorCode.INVALID_RISK)

    if packet.rollback_strategy not in VALID_ROLLBACK_STRATEGIES:
        errors.append(f"Invalid rollback strategy: {packet.rollback_strategy}")
        codes.append(IntentErrorCode.INVALID_ROLLBACK)

    if not packet.manifest_hash or not HASH_RE.fullmatch(packet.manifest_hash):
        errors.append("manifest_hash must look like a cryptographic hash.")
        codes.append(IntentErrorCode.MISSING_FIELD)

    scope_result = validate_scope(packet.scope)
    errors.extend(scope_result.errors)
    codes.extend(scope_result.error_codes)

    raw_leak_paths = find_raw_indicator_fields(packet.to_dict())
    if raw_leak_paths:
        errors.append(f"Raw indicator-like field(s) are not allowed in intent packet: {raw_leak_paths}")
        codes.append(IntentErrorCode.RAW_INDICATOR_LEAK)

    return IntentValidationResult(ok=not errors, errors=tuple(errors), error_codes=tuple(codes))


def validate_intent_or_raise(packet: IntentPacket) -> None:
    result = validate_intent(packet)
    if not result.ok:
        raise IntentValidationError(result.errors[0], result.error_codes[0])


def find_raw_indicator_fields(value: Any, path: str = "") -> list[str]:
    findings: list[str] = []

    if isinstance(value, Mapping):
        for key, child in value.items():
            key_str = str(key)
            child_path = f"{path}.{key_str}" if path else key_str
            if key_str.lower() in RAW_FIELD_NAMES:
                findings.append(child_path)
            findings.extend(find_raw_indicator_fields(child, child_path))

    elif isinstance(value, (list, tuple)):
        for index, child in enumerate(value):
            findings.extend(find_raw_indicator_fields(child, f"{path}[{index}]"))

    return findings


def intent_fingerprint(packet: IntentPacket) -> str:
    return hashlib.sha256(packet.to_json(include_signature=True).encode("utf-8")).hexdigest()


def unsigned_intent_fingerprint(packet: IntentPacket) -> str:
    return hashlib.sha256(packet.to_json(include_signature=False).encode("utf-8")).hexdigest()


def risk_score(risk_label: RiskLabel) -> float:
    return {
        "low": 0.25,
        "medium": 0.5,
        "high": 0.75,
        "critical": 1.0,
    }[risk_label]


def default_rollback_for_risk(risk_label: RiskLabel) -> RollbackStrategy:
    if risk_label == "low":
        return "observe_only"
    if risk_label == "medium":
        return "disable_module"
    if risk_label == "high":
        return "sandbox"
    return "revert"


def derive_risk_label(
    *,
    requested_modules: Iterable[str],
    authorized_target: bool,
    contains_conditional_operation: bool = False,
) -> RiskLabel:
    modules = {str(module).strip().lower().replace(" ", "_") for module in requested_modules}

    forbidden = {
        "port_scan",
        "nmap",
        "masscan",
        "brute_force",
        "credential_testing",
        "exploitation",
    }
    conditional = {
        "http_headers",
        "robots_txt",
        "screenshot",
        "ssl_labs_ssl_test",
    }

    if modules.intersection(forbidden):
        return "critical"

    if contains_conditional_operation or modules.intersection(conditional):
        return "medium" if authorized_target else "high"

    return "low"


def dedupe_preserve_order(values: Iterable[str]) -> list[str]:
    seen: set[str] = set()
    output: list[str] = []
    for value in values:
        normalized = str(value).strip()
        if not normalized:
            continue
        if normalized not in seen:
            output.append(normalized)
            seen.add(normalized)
    return output