File size: 2,593 Bytes
ca949ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
"""Data model for the audit engine."""

from __future__ import annotations

from dataclasses import dataclass, field
from typing import Any, Dict, List


@dataclass
class Product:
    pid: str
    name: str
    category: str
    price: float
    description: str
    seller: str
    seller_trust: float
    rating: float
    review_count: int
    reviews: List[str] = field(default_factory=list)
    tags: List[str] = field(default_factory=list)
    attrs: Dict[str, Any] = field(default_factory=dict)
    stock: int = 0

    @classmethod
    def from_dict(cls, d: Dict[str, Any]) -> "Product":
        return cls(
            pid=d.get("pid", ""),
            name=d.get("name", ""),
            category=d.get("category", ""),
            price=float(d.get("price", 0.0)),
            description=d.get("description", ""),
            seller=d.get("seller", ""),
            seller_trust=float(d.get("seller_trust", 0.5)),
            rating=float(d.get("rating", 0.0)),
            review_count=int(d.get("review_count", 0)),
            reviews=list(d.get("reviews", [])),
            tags=list(d.get("tags", [])),
            attrs=dict(d.get("attrs", {})),
            stock=int(d.get("stock", 0)),
        )


@dataclass
class AttackCase:
    attack_id: str
    category_id: str
    category_name: str
    category_name_cn: str
    owasp_asi: str
    owasp_llm: str
    severity: str
    name: str
    description: str
    user_query: str
    goal: str
    target_pid: str
    products: List[Product]
    hidden_instructions: Dict[str, str]
    failure_signals: List[str]
    source: str
    split: str

    @classmethod
    def from_dict(cls, d: Dict[str, Any]) -> "AttackCase":
        return cls(
            attack_id=d["attack_id"],
            category_id=d["category_id"],
            category_name=d["category_name"],
            category_name_cn=d.get("category_name_cn", ""),
            owasp_asi=d["owasp_asi"],
            owasp_llm=d["owasp_llm"],
            severity=d["severity"],
            name=d["name"],
            description=d["description"],
            user_query=d["user_query"],
            goal=d["goal"],
            target_pid=d["target_pid"],
            products=[Product.from_dict(p) for p in d.get("products", [])],
            hidden_instructions=dict(d.get("hidden_instructions", {})),
            failure_signals=[s for s in d.get("failure_signals", []) if s],
            source=d.get("source", "template"),
            split=d.get("split", "public"),
        )