{ "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 }