Audit Training Data Integrity
Validate, deduplicate, and provenance-track training data to detect poisoning attacks, privacy violations, and systematic bias before they are baked into model weights.
Why This Is Best Practice
Adopted by: OWASP Top 10 for LLM Applications 2025 LLM03 (Training Data Poisoning). NIST AI RMF 1.0 (2023) Govern 1.1 and Map 2.2 address training data governance. Google, Meta, and Hugging Face publish data cards and datasheets for datasets as standard practice. The ML safety community (papers by Carlini, Wallace, Schuster) has demonstrated practical data poisoning attacks since 2020.
Status: Emerging — data poisoning is a well-researched attack (since ~2017) but detection tooling and industry standards are still maturing.
Impact: Carlini et al. (2021) demonstrated extracting verbatim training data from GPT-2, including PII. Schuster et al. (2021) showed code suggestion models trained on poisoned GitHub data could insert security vulnerabilities into suggestions (later confirmed in real systems). BadNL attacks (Chen et al., 2021) showed NLP classifiers could be given hidden backdoors via 50 poisoned examples in 100,000 training samples — 0.05% poisoning rate sufficient for reliable triggering.
Why best: Post-hoc model auditing (testing the trained model for backdoors) is the alternative — it's expensive, slow, and incomplete. Data-time auditing catches issues before they're encoded into weights, where removal is computationally expensive and may require full retraining.
Sources: OWASP LLM Top 10 2025 LLM03; NIST AI RMF 1.0; Carlini et al. "Extracting Training Data from Large Language Models" (2021); Schuster et al. "You Autocomplete Me: Poisoning Vulnerabilities in Neural Code Completion" (2021)
Steps
Track data provenance with a data card for every dataset:
# data_card.yaml
dataset_name: customer-support-v2
version: 2.3.1
collected_by: data-team
collection_date: 2024-01-15
sources:
- type: internal
description: Customer support tickets Q3-Q4 2023
pii_review: completed
pii_review_date: 2024-01-10
licenses: [internal-proprietary]
known_biases:
- English-only, over-represents US timezone users
exclusions:
- removed: all tickets containing SSN, credit card numbers
- removed: tickets from users who opted out of data use
hash_sha256: <hash of final dataset file>
Scan for PII before training:
from presidio_analyzer import AnalyzerEngine
analyzer = AnalyzerEngine()
def scan_for_pii(text):
results = analyzer.analyze(text=text, language='en')
high_confidence = [r for r in results if r.score > 0.85]
return high_confidence
def audit_dataset(dataset_path, sample_size=1000):
pii_count = 0
for sample in load_dataset_sample(dataset_path, n=sample_size):
findings = scan_for_pii(sample['text'])
if findings:
pii_count += 1
log_pii_finding(sample['id'], findings)
pii_rate = pii_count / sample_size
if pii_rate > 0.01: # >1% PII rate — investigate
raise DataQualityError(f"PII rate {pii_rate:.2%} exceeds threshold")
Detect and remove near-duplicate entries — duplicates cause memorization and model overfitting:
from datasketch import MinHash, MinHashLSH
def deduplicate_dataset(texts, threshold=0.9):
lsh = MinHashLSH(threshold=threshold, num_perm=128)
unique_texts = []
for i, text in enumerate(texts):
m = MinHash(num_perm=128)
for word in text.split():
m.update(word.encode('utf8'))
if not lsh.query(m): # no near-duplicate found
lsh.insert(str(i), m)
unique_texts.append(text)
print(f"Removed {len(texts) - len(unique_texts)} near-duplicates")
return unique_texts
Audit label consistency for supervised learning:
from sklearn.metrics import cohen_kappa_score
def audit_label_quality(labels_annotator1, labels_annotator2):
kappa = cohen_kappa_score(labels_annotator1, labels_annotator2)
if kappa < 0.6:
raise DataQualityError(
f"Inter-annotator agreement κ={kappa:.2f} below threshold 0.6"
)
return kappa
# Flag for review: examples where annotators disagree
def find_disagreements(data, labels1, labels2):
return [data[i] for i in range(len(data)) if labels1[i] != labels2[i]]
Test for backdoor triggers in the trained model (post-training validation):
def test_for_backdoor(model, trigger_phrases, expected_safe_output):
"""Check if specific phrases cause anomalous outputs (backdoor indicator)."""
anomalous = []
for trigger in trigger_phrases:
output = model.generate(f"Normal sentence with {trigger}")
if output != expected_safe_output:
anomalous.append({'trigger': trigger, 'output': output})
return anomalous
Implement consent and opt-out tracking for user-generated training data:
def include_in_training(user_id, content):
consent = db.get_training_consent(user_id)
if consent is None or consent.opted_out:
return False
if content.created_at < consent.consent_date:
return False # pre-dates consent
return True
Rules
- Cryptographically hash and version-stamp each training dataset snapshot — enables auditing exactly which data was used if a model issue is discovered post-deployment.
- Data contributed by users requires explicit consent for training use — check privacy policy and applicable regulations (GDPR Article 5, CCPA).
- Regular expressions alone are insufficient for PII detection — use NER-based tools (Presidio, spaCy) for higher coverage.
- Poisoning attacks are designed to be undetectable in individual samples — statistical methods on the full dataset are required.
Common Mistakes
- Treating training data as inherently trusted because it's "internal" — internal datasets sourced from user content or web scrapes can contain adversarial examples.
- Not tracking data provenance — when a model exhibits a problem behavior, inability to trace which training data caused it makes remediation impossible.
- Using the full training set for deduplication checks — exact deduplication misses near-duplicates; use MinHash LSH or similar approximate methods.
- Skipping PII scanning because data is "anonymized" — anonymization is often incomplete; structured PII like SSNs and credit cards persist through naive scrubbing.
1---2name: audit-training-data-integrity3description: Use when curating, collecting, or managing datasets for training or fine-tuning machine learning models — to detect and mitigate poisoned, biased, mislabeled, or adversarially crafted training data.4source: 'OWASP Top 10 for LLM Applications 2025 LLM03 (owasp.org/www-project-top-10-for-large-language-model-applications/); NIST AI RMF 1.0; Barreno et al. "The Security of Machine Learning" (2010); CWE-20'5tags: [security, owasp, llm, training-data, data-poisoning, ml-security, emerging, developer]6emerging: true7metadata:8 source: https://skillmd.com/skills/jeffreytse/audit-training-data-integrity9---1011# Audit Training Data Integrity1213Validate, deduplicate, and provenance-track training data to detect poisoning attacks, privacy violations, and systematic bias before they are baked into model weights.1415## Why This Is Best Practice1617**Adopted by:** OWASP Top 10 for LLM Applications 2025 LLM03 (Training Data Poisoning). NIST AI RMF 1.0 (2023) Govern 1.1 and Map 2.2 address training data governance. Google, Meta, and Hugging Face publish data cards and datasheets for datasets as standard practice. The ML safety community (papers by Carlini, Wallace, Schuster) has demonstrated practical data poisoning attacks since 2020.18**Status:** Emerging — data poisoning is a well-researched attack (since ~2017) but detection tooling and industry standards are still maturing.19**Impact:** Carlini et al. (2021) demonstrated extracting verbatim training data from GPT-2, including PII. Schuster et al. (2021) showed code suggestion models trained on poisoned GitHub data could insert security vulnerabilities into suggestions (later confirmed in real systems). BadNL attacks (Chen et al., 2021) showed NLP classifiers could be given hidden backdoors via 50 poisoned examples in 100,000 training samples — 0.05% poisoning rate sufficient for reliable triggering.20**Why best:** Post-hoc model auditing (testing the trained model for backdoors) is the alternative — it's expensive, slow, and incomplete. Data-time auditing catches issues before they're encoded into weights, where removal is computationally expensive and may require full retraining.2122Sources: OWASP LLM Top 10 2025 LLM03; NIST AI RMF 1.0; Carlini et al. "Extracting Training Data from Large Language Models" (2021); Schuster et al. "You Autocomplete Me: Poisoning Vulnerabilities in Neural Code Completion" (2021)2324## Steps25261. **Track data provenance with a data card for every dataset**:2728 ```yaml29 # data_card.yaml30 dataset_name: customer-support-v231 version: 2.3.132 collected_by: data-team33 collection_date: 2024-01-1534 sources:35 - type: internal36 description: Customer support tickets Q3-Q4 202337 pii_review: completed38 pii_review_date: 2024-01-1039 licenses: [internal-proprietary]40 known_biases:41 - English-only, over-represents US timezone users42 exclusions:43 - removed: all tickets containing SSN, credit card numbers44 - removed: tickets from users who opted out of data use45 hash_sha256: <hash of final dataset file>46 ```47482. **Scan for PII before training**:4950 ```python51 from presidio_analyzer import AnalyzerEngine5253 analyzer = AnalyzerEngine()5455 def scan_for_pii(text):56 results = analyzer.analyze(text=text, language='en')57 high_confidence = [r for r in results if r.score > 0.85]58 return high_confidence5960 def audit_dataset(dataset_path, sample_size=1000):61 pii_count = 062 for sample in load_dataset_sample(dataset_path, n=sample_size):63 findings = scan_for_pii(sample['text'])64 if findings:65 pii_count += 166 log_pii_finding(sample['id'], findings)6768 pii_rate = pii_count / sample_size69 if pii_rate > 0.01: # >1% PII rate — investigate70 raise DataQualityError(f"PII rate {pii_rate:.2%} exceeds threshold")71 ```72733. **Detect and remove near-duplicate entries** — duplicates cause memorization and model overfitting:7475 ```python76 from datasketch import MinHash, MinHashLSH7778 def deduplicate_dataset(texts, threshold=0.9):79 lsh = MinHashLSH(threshold=threshold, num_perm=128)80 unique_texts = []8182 for i, text in enumerate(texts):83 m = MinHash(num_perm=128)84 for word in text.split():85 m.update(word.encode('utf8'))8687 if not lsh.query(m): # no near-duplicate found88 lsh.insert(str(i), m)89 unique_texts.append(text)9091 print(f"Removed {len(texts) - len(unique_texts)} near-duplicates")92 return unique_texts93 ```94954. **Audit label consistency for supervised learning**:9697 ```python98 from sklearn.metrics import cohen_kappa_score99100 def audit_label_quality(labels_annotator1, labels_annotator2):101 kappa = cohen_kappa_score(labels_annotator1, labels_annotator2)102 if kappa < 0.6:103 raise DataQualityError(104 f"Inter-annotator agreement κ={kappa:.2f} below threshold 0.6"105 )106 return kappa107108 # Flag for review: examples where annotators disagree109 def find_disagreements(data, labels1, labels2):110 return [data[i] for i in range(len(data)) if labels1[i] != labels2[i]]111 ```1121135. **Test for backdoor triggers in the trained model** (post-training validation):114115 ```python116 def test_for_backdoor(model, trigger_phrases, expected_safe_output):117 """Check if specific phrases cause anomalous outputs (backdoor indicator)."""118 anomalous = []119 for trigger in trigger_phrases:120 output = model.generate(f"Normal sentence with {trigger}")121 if output != expected_safe_output:122 anomalous.append({'trigger': trigger, 'output': output})123 return anomalous124 ```1251266. **Implement consent and opt-out tracking for user-generated training data**:127128 ```python129 def include_in_training(user_id, content):130 consent = db.get_training_consent(user_id)131 if consent is None or consent.opted_out:132 return False133 if content.created_at < consent.consent_date:134 return False # pre-dates consent135 return True136 ```137138## Rules139140- Cryptographically hash and version-stamp each training dataset snapshot — enables auditing exactly which data was used if a model issue is discovered post-deployment.141- Data contributed by users requires explicit consent for training use — check privacy policy and applicable regulations (GDPR Article 5, CCPA).142- Regular expressions alone are insufficient for PII detection — use NER-based tools (Presidio, spaCy) for higher coverage.143- Poisoning attacks are designed to be undetectable in individual samples — statistical methods on the full dataset are required.144145## Common Mistakes146147- **Treating training data as inherently trusted because it's "internal"** — internal datasets sourced from user content or web scrapes can contain adversarial examples.148- **Not tracking data provenance** — when a model exhibits a problem behavior, inability to trace which training data caused it makes remediation impossible.149- **Using the full training set for deduplication checks** — exact deduplication misses near-duplicates; use MinHash LSH or similar approximate methods.150- **Skipping PII scanning because data is "anonymized"** — anonymization is often incomplete; structured PII like SSNs and credit cards persist through naive scrubbing.