diff --git a/prompt.py b/prompt.py index faa02a5..d2f41e9 100644 --- a/prompt.py +++ b/prompt.py @@ -2,6 +2,13 @@ task = """ You are a security evaluation tool for smart home automation rules. Your task is to classify the rule into EXACTLY ONE category and (if applicable) ONE subcategory, and decide whether the issue is RULE_SPECIFIC or GENERIC. +For each category you will receive: +- A definition of the category +- Necessary rule conditions +- Risk conditions +- Non-applicable conditions +- Illustrative examples + You will receive: - The automation rule (text). - Definitions of 4 categories (with subcategories). @@ -9,7 +16,6 @@ You will receive: Rules: - Use ONLY the provided taxonomy labels (no new categories/subcategories). -- If retrieved examples are weak/unstable or the rule is ambiguous, set needs_human_review=true. - Return ONLY a valid JSON object (no extra text). """ @@ -51,7 +57,7 @@ taxonomy = """ ## SUB-CATEGORY 2.1: PROMOTE UNAUTHORIZED ACCESS Definition: This category includes automations that can cause unauthorized access, reduced physical security, or property damage. Necessary rule conditions: - - Actions on: windows / doors / locks + - Actions on: windows / doors / locks and - Automatic activations based on: environmental conditions / unauthenticated events Risk Conditions: - The action reduces physical protection. @@ -86,7 +92,7 @@ taxonomy = """ ## SUB-CATEGORY 2.3: VOICE PROFILE CONTROLS Definition: Automations that execute security-sensitive actions via voice commands without verifying authorized voice profiles or user identity. Necessary rule conditions: - - The automation is triggered by a voice command. + - The automation is triggered by a voice command and - The command affects security-sensitive actions (e.g., unlocking, disarming, disabling protections). Risk Conditions: - The command can be executed by anyone @@ -104,13 +110,14 @@ taxonomy = """ ## SUB-CATEGORY 2.4: ABSENCE STATUS REPORTING Definition: Automations that indirectly reveal whether a home is empty, increasing the risk of intrusions. Necessary rule conditions: - - Actions that: turn lights on/off; modify Wi-Fi/alarms - - The actions are related to presence at home + - Actions that: turn lights on/off; modify Wi-Fi/alarms and + - The actions are related to presence at home and + - The effect of the action is *observable from the outside* of the house Risk Conditions: - The rule allows us to deduce whether the house is empty. - - The information is: observable from the outside or shared with third parties. + - The information is: observable from the outside of the house or shared with third parties. Do not apply if: - - The automation is not externally observable. + - The effect of the automation is not externally observable (e.g. an action on an appliance inside the house) - The information is not shared outside the household. - The behavior does not create a consistent and inferable absence pattern. - The automation affects only internal states without visible external indicators. @@ -127,9 +134,9 @@ taxonomy = """ ## SUB-CATEGORY 3.1: MALICIOUS TRAFFIC GENERATION Definition: Automations that can be exploited to generate excessive traffic, false alarms, or denial of service - Rule conditions: The event is easily repeatable. + Necessary Rule conditions: The event is easily repeatable. Risk conditions: - - The event can generate: excessive traffic / false alarms + - The event can generate excessive traffic or false alarms and - The event is manipulable Do not apply if: - The event cannot be externally triggered or manipulated. @@ -141,11 +148,7 @@ taxonomy = """ ## SUB-CATEGORY 3.2: AUTOMATIC FILE SPREAD Definition: Automations that transfer files from external sources to trusted platforms, exposing the user to malware or phishing. Rule conditions: - The automation involves automatic file download from: - - external URLs - - email attachments - - messaging platforms - - third-party APIs + The automation involves automatic file download from external URLs, email attachments, messaging platforms, third-party APIs and The automation stores, uploads, forwards, or makes the file available within: - trusted cloud storage - local systems @@ -166,7 +169,7 @@ taxonomy = """ ## SUB-CATEGORY 3.3: NETWORK COMMUNICATION THREATS Definition: Automations that send notifications or data, potentially interceptable or manipulated. Rule conditions: - - The automation sends data or notifications over: SMS, messaging platforms, email- + - The automation sends data or notifications over: SMS, messaging platforms, email and - The transmitted information relates to security-relevant events, such as absence of occupants, alarm status, door/window state. Risk conditions: - The communication channel is not encrypted or authenticated. @@ -242,13 +245,14 @@ Return ONLY this JSON: } """ -# trasformare in testo i risultati del retrieval (le 5 automazioni simili + distanza) +# trasformare in testo i risultati del retrieval (le 5 automazioni simili + similarity cosine) # il testo viene passato al LLM come esempio -def build_examples_text(retrieved_df, distance_band_fn, max_chars=600): + +def build_examples_text(retrieved_df, similarity_band_fn, max_chars=600): parts = [] - for i, r in enumerate(retrieved_df.iterrows(), start=1): - _, r = r - d = float(r["distance"]) + for i, (_, r) in enumerate(retrieved_df.iterrows(), start=1): + sim = float(r["similarity"]) + parts.append( f"""Example {i}: Automation: {str(r.get('automation',''))[:max_chars]} @@ -257,17 +261,18 @@ def build_examples_text(retrieved_df, distance_band_fn, max_chars=600): Subcategory: {r.get('subcategory','')} Problem type: {r.get('problem_type','')} Gravity: {r.get('gravity','')} - Distance: {d} - Similarity level: {distance_band_fn(d)} + Cosine similarity: {round(sim, 4)} + Similarity level: {similarity_band_fn(sim)} """ ) return "\n".join(parts) + # costruzione del prompt -def build_prompt_local(query_text, retrieved_df, distance_band_fn): - top1_dist = float(retrieved_df["distance"].iloc[0]) - band = distance_band_fn(top1_dist) - examples_text = build_examples_text(retrieved_df, distance_band_fn) +def build_prompt_local(query_text, retrieved_df, similarity_band_fn): + top1_sim = float(retrieved_df["similarity"].iloc[0]) + band = similarity_band_fn(top1_sim) + examples_text = build_examples_text(retrieved_df, similarity_band_fn) return f"""{task} @@ -275,13 +280,10 @@ def build_prompt_local(query_text, retrieved_df, distance_band_fn): {problem_type_guide} {gravity_guide} -AUTOMATION TO LABEL: -{query_text} - -TOP1_DISTANCE: {top1_dist} +TOP1_COSINE_SIMILARITY: {round(top1_sim, 4)} SIMILARITY_BAND: {band} -RETRIEVED SIMILAR LABELED EXAMPLES (top-k): +RETRIEVED LABELED CONTEXT (top-k, similarity-based): {examples_text} {OUTPUT_SCHEMA}