-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.py
More file actions
348 lines (292 loc) · 11 KB
/
Copy pathengine.py
File metadata and controls
348 lines (292 loc) · 11 KB
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
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
"""
Core analysis engine: pattern matching, scoring, and finding.
"""
import re
from rules import (
RED_FLAGS,
VAGUE_PATTERNS,
OPT_OUT_PATTERNS,
OPT_IN_PATTERNS,
MISSING_SECTION_FLAGS,
DATE_PATTERNS,
SILENT_COLLECTION_PATTERNS,
)
# Words that negate or reverse the meaning of a phrase
_NEGATION_WORDS = re.compile(
r"(?:don.t|doesn.t|didn.t|won.t|wouldn.t|can.t|cannot|never|"
r"do not|does not|did not|will not|would not|shall not|"
r"no longer|not|without)"
)
def _is_negated(text_lower, match_start, match_end, lookback=100):
"""Check if a regex match is preceded by negation words within a short window."""
start = max(0, match_start - lookback)
preceding = text_lower[start:match_start]
# Check if any negation word appears between the last period/comma and the match
# Find the last sentence boundary before the match
last_boundary = max(preceding.rfind("."), preceding.rfind(";"), preceding.rfind("\n"))
if last_boundary != -1:
preceding = preceding[last_boundary + 1:]
return bool(_NEGATION_WORDS.search(preceding))
def analyze_text(text, categorized_sections):
"""Run full analysis on the policy text. Returns an AnalysisResult dict."""
text_lower = text.lower()
results = {
"red_flags": [],
"vague_language": [],
"opt_out_signals": [],
"opt_in_signals": [],
"missing_sections": [],
"silent_collection": [],
"positive_findings": [],
"policy_date": None,
"risk_score": 0,
"total_words": len(text.split()),
}
# --- Red flag detection ---
for pattern, points, explanation, category in RED_FLAGS:
try:
matches = list(re.finditer(pattern, text_lower))
except re.error:
continue
if not matches:
continue
# Check negation for each match
true_matches = []
negated_matches = []
for m in matches:
if _is_negated(text_lower, m.start(), m.end()):
negated_matches.append(m)
else:
true_matches.append(m)
if true_matches:
context = _find_context(text, pattern)
# Generate a positive finding if the phrase is negated
if negated_matches:
clean_explanation = explanation.split(".")[0].strip()
results["positive_findings"].append({
"message": f'"{pattern}" is mentioned but negated - they state they do NOT do this.',
"context": _find_context(text, pattern),
})
results["red_flags"].append({
"pattern": pattern,
"points": points,
"explanation": explanation,
"category": category,
"occurrences": len(true_matches),
"context": context,
"severity": _severity_from_points(points),
})
elif negated_matches:
# Pattern exists but is always negated - note as positive
context = _find_context(text, pattern)
results["positive_findings"].append({
"message": f'"{pattern}" is mentioned but negated - they promise not to do this.',
"context": context,
})
# --- Vague language detection ---
for pattern, explanation in VAGUE_PATTERNS:
count = len(re.findall(r"\b" + re.escape(pattern) + r"\b", text_lower))
if count > 0:
results["vague_language"].append({
"pattern": pattern,
"count": count,
"explanation": explanation,
})
# --- Opt-out vs opt-in analysis ---
for pattern, signal_type in OPT_OUT_PATTERNS:
if re.search(pattern, text_lower):
results["opt_out_signals"].append(pattern)
for pattern, signal_type in OPT_IN_PATTERNS:
if re.search(pattern, text_lower):
results["opt_in_signals"].append(pattern)
# --- Silent collection detection ---
for pattern in SILENT_COLLECTION_PATTERNS:
if re.search(pattern, text_lower):
context = _find_context(text, re.escape(pattern))
results["silent_collection"].append({
"pattern": pattern,
"context": context,
})
# --- Missing section detection ---
for cat_key, label, warning, points in MISSING_SECTION_FLAGS:
cat_data = categorized_sections.get(cat_key, {})
if not cat_data.get("sections"):
results["missing_sections"].append({
"category": cat_key,
"label": label,
"warning": warning,
"points": points,
})
# --- Policy date detection ---
for pattern in DATE_PATTERNS:
match = re.search(pattern, text, re.IGNORECASE)
if match:
results["policy_date"] = match.group(1).strip()
break
# --- Calculate risk score ---
results["risk_score"] = _calculate_score(results)
return results
def _find_context(text, pattern, context_chars=150):
"""Find the sentence/paragraph containing a pattern match."""
try:
match = re.search(pattern, text, re.IGNORECASE)
except re.error:
match = re.search(pattern, text)
if not match:
return ""
start = max(0, match.start() - context_chars)
end = min(len(text), match.end() + context_chars)
context = text[start:end].strip()
# Clean up to sentence boundaries
if start > 0:
first_period = context.find(".")
if first_period != -1 and first_period < 50:
context = context[first_period + 1 :].strip()
if end < len(text):
last_period = context.rfind(".")
if last_period != -1 and last_period > len(context) - 50:
context = context[: last_period + 1].strip()
return context
def _severity_from_points(points):
"""Map risk points to severity label."""
if points >= 30:
return "CRITICAL"
elif points >= 15:
return "HIGH"
elif points >= 8:
return "MEDIUM"
else:
return "LOW"
def _calculate_score(results):
"""Calculate overall risk score (0-100)."""
score = 0
# Red flags (weighted - critical ones count more, but diminishing returns)
# Only count flags with points >= 8 (MEDIUM and above)
flag_points = sorted(
[f["points"] for f in results["red_flags"] if f["points"] >= 8],
reverse=True,
)
for i, pts in enumerate(flag_points):
# First few flags count at full weight, then diminishing
weight = 1.0 if i < 3 else 0.5 if i < 6 else 0.25
score += pts * weight
# Bonus for having many different red flags
if len(flag_points) > 5:
score += 5
# Vague language (diminishing returns)
vague_count = len(results["vague_language"])
if vague_count > 10:
score += 15
elif vague_count > 5:
score += 10
elif vague_count > 0:
score += vague_count
# Opt-out heavy (no opt-in)
if results["opt_out_signals"] and not results["opt_in_signals"]:
score += 10
elif len(results["opt_out_signals"]) > 3:
score += 5
# Silent collection
score += len(results["silent_collection"]) * 8
# Missing sections
for missing in results["missing_sections"]:
score += missing["points"]
# Positive findings reduce score
positive_count = len(results.get("positive_findings", []))
if positive_count > 0:
reduction = min(positive_count * 3, 15)
score -= reduction
# Having good practices reduces score slightly
good_practice_flags = [
f for f in results["red_flags"]
if f["points"] < 8 # LOW severity = actually good practices
]
if good_practice_flags:
score -= len(good_practice_flags) * 2
# Old policy (no date found is suspicious)
if not results["policy_date"]:
score += 5
# Clamp at 0-100
return max(0, min(score, 100))
def get_top_findings(results, max_findings=10):
"""Get the most important findings sorted by severity."""
all_findings = []
for flag in results["red_flags"]:
all_findings.append({
"type": "RED FLAG",
"severity": flag["severity"],
"message": flag["explanation"],
"context": flag["context"],
"points": flag["points"],
})
for missing in results["missing_sections"]:
all_findings.append({
"type": "MISSING",
"severity": "HIGH",
"message": missing["warning"],
"context": "",
"points": missing["points"],
})
for silent in results["silent_collection"]:
all_findings.append({
"type": "SUSPICIOUS",
"severity": "HIGH",
"message": f"Silent/automatic data collection detected: '{silent['pattern']}'",
"context": silent["context"],
"points": 8,
})
# Sort by points (severity)
all_findings.sort(key=lambda x: x["points"], reverse=True)
return all_findings[:max_findings]
def generate_summary(results):
"""Generate a plain-text summary of the analysis."""
score = results["risk_score"]
red_flags = results["red_flags"]
missing = results["missing_sections"]
vague = results["vague_language"]
silent = results["silent_collection"]
parts = []
if score >= 70:
parts.append(
"This privacy policy has a HIGH risk score. "
"Exercise extreme caution before using this service."
)
elif score >= 40:
parts.append(
"This privacy policy has a MODERATE risk score. "
"There are several concerns you should be aware of."
)
elif score >= 15:
parts.append(
"This privacy policy has a LOW-MODERATE risk score. "
"Most practices are standard but there are some things to note."
)
else:
parts.append(
"This privacy policy has a LOW risk score. "
"It appears relatively standard and fair."
)
if red_flags:
critical = [f for f in red_flags if f["severity"] == "CRITICAL"]
high = [f for f in red_flags if f["severity"] == "HIGH"]
if critical:
parts.append(f"Found {len(critical)} CRITICAL red flags.")
if high:
parts.append(f"Found {len(high)} HIGH risk patterns.")
if missing:
parts.append(f"{len(missing)} important sections are missing from this policy.")
if silent:
parts.append(
f"Detected {len(silent)} instances of silent/automatic data collection."
)
if vague:
parts.append(
f"Found {len(vague)} instances of vague/non-committal language."
)
# Opt-out analysis
if results["opt_out_signals"] and not results["opt_in_signals"]:
parts.append(
"This policy uses opt-out consent (you must actively leave) "
"rather than opt-in (they ask permission first)."
)
return " ".join(parts)