File size: 5,979 Bytes
3d968cd | 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 | {
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Synthetic Sensitive Data in Source Code (N=300) — Starter\n",
"\n",
"Quick exploration of the dataset: load samples, inspect categories, and preview labeled secrets.\n",
"\n",
"**Intended use:** secret/PII detection, local masking evaluation, OWASP LLM02–style leakage tests.\n",
"\n",
"> All values are **synthetic**. Do not treat them as real credentials."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pathlib import Path\n",
"import json\n",
"import pandas as pd\n",
"\n",
"# Works on Kaggle and locally\n",
"CANDIDATES = [\n",
" Path(\"/kaggle/input\"),\n",
" Path(\".\"),\n",
"]\n",
"\n",
"csv_path = None\n",
"json_path = None\n",
"for root in CANDIDATES:\n",
" hits = list(root.rglob(\"synthetic_sensitive_data_in_source_code_n300.csv\"))\n",
" if hits:\n",
" csv_path = hits[0]\n",
" json_path = csv_path.with_suffix(\".json\")\n",
" break\n",
"\n",
"assert csv_path is not None, \"Dataset CSV not found\"\n",
"print(\"CSV :\", csv_path)\n",
"print(\"JSON:\", json_path if json_path.exists() else \"(optional)\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Load CSV"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv(csv_path)\n",
"print(df.shape)\n",
"df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Category & language distribution"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"Categories\")\n",
"display(df[\"category\"].value_counts().rename(\"count\").to_frame())\n",
"\n",
"print(\"\\nLanguages\")\n",
"display(df[\"language\"].value_counts().rename(\"count\").to_frame())\n",
"\n",
"print(\"\\nSecrets per sample\")\n",
"display(df[\"sensitive_count\"].describe().to_frame(\"sensitive_count\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ax = df[\"category\"].value_counts().plot(kind=\"bar\", figsize=(8, 3), title=\"Samples by category\")\n",
"ax.set_xlabel(\"category\")\n",
"ax.set_ylabel(\"count\")\n",
"ax.tick_params(axis=\"x\", rotation=45)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Preview one sample"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"row = df.sample(1, random_state=42).iloc[0]\n",
"print(\"id :\", row[\"id\"])\n",
"print(\"category:\", row[\"category\"])\n",
"print(\"language:\", row[\"language\"])\n",
"print(\"findings:\", row[\"finding_types\"])\n",
"print(\"count :\", row[\"sensitive_count\"])\n",
"print(\"\\n--- code_text ---\\n\")\n",
"# CSV may store newlines as the two characters \\\\n\n",
"code = str(row[\"code_text\"]).replace(\"\\\\n\", \"\\n\")\n",
"print(code)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4. Ground truth from JSON (recommended for evaluation)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if json_path is not None and json_path.exists():\n",
" payload = json.loads(json_path.read_text(encoding=\"utf-8\"))\n",
" records = payload[\"records\"]\n",
" print(\"name :\", payload.get(\"name\"))\n",
" print(\"size :\", payload.get(\"size\"))\n",
" print(\"owasp:\", payload.get(\"owasp_primary\"))\n",
"\n",
" sample = records[0]\n",
" print(\"\\nExample ground truth:\")\n",
" print(\"id :\", sample[\"id\"])\n",
" print(\"findings:\")\n",
" for f in sample[\"sensitive_findings\"]:\n",
" print(\" -\", f[\"type\"], \"=\", f[\"value\"][:48] + (\"...\" if len(f[\"value\"]) > 48 else \"\"))\n",
"else:\n",
" print(\"JSON not found in this runtime; use the CSV columns for a quick look.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 5. Tiny detection baseline (substring match)\n",
"\n",
"Toy baseline: if a labeled secret string appears in `code_text`, count it as detected. \n",
"Replace this with your masking / detector pipeline."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if json_path is not None and json_path.exists():\n",
" records = json.loads(json_path.read_text(encoding=\"utf-8\"))[\"records\"]\n",
" total = hit = 0\n",
" for r in records:\n",
" code = r[\"code_text\"]\n",
" for f in r[\"sensitive_findings\"]:\n",
" total += 1\n",
" if f[\"value\"] in code:\n",
" hit += 1\n",
" print(f\"Labeled secrets present in code_text: {hit}/{total} ({100 * hit / total:.1f}%)\")\n",
" print(\"(Expected ~100% — sanity check that labels match the snippets.)\")\n",
"else:\n",
" print(\"Skip: JSON required for this cell.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Next steps\n",
"1. Build a detector (regex / ML) over `code_text`\n",
"2. Compare predictions to `sensitive_findings`\n",
"3. Measure precision, recall, and latency for your masking layer"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.11.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|