From 094ccb50c5aaf29174487b6dc9c4c17976ef176a Mon Sep 17 00:00:00 2001 From: rsim Date: Fri, 30 Oct 2026 11:13:13 -0700 Subject: [PATCH] Replace flagged sample credentials with synthetic placeholders Add a post-clone synthetic dataset generator and focused regression tests. Preserve sample annotations and keep original-value backups out of version control. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 8011f41c-e888-4e9b-863c-68644bc7423b --- .gitignore | 1 + README.md | 66 ++++++++++ .../Aishwarya512--Weather-app/myproject.py | 2 +- .../dummycode--qpay/web/app/views/user.py | 4 +- .../targets-orig/janetcl--dance/app.py | 4 +- .../VisionAPI/final.py | 4 +- .../targets-orig/xenoveal--BuildUp/app.py | 4 +- .../Aishwarya512--Weather-app/myproject.py | 2 +- .../New/login.py | 4 +- .../dummycode--qpay/web/app/views/user.py | 4 +- .../targets-tags/janetcl--dance/app.py | 4 +- .../VisionAPI/final.py | 4 +- .../targets-tags/xenoveal--BuildUp/app.py | 4 +- scripts/generate_synthetic_credentials.py | 120 ++++++++++++++++++ tests/test_synthetic_credentials.py | 117 +++++++++++++++++ 15 files changed, 324 insertions(+), 20 deletions(-) create mode 100644 scripts/generate_synthetic_credentials.py create mode 100644 tests/test_synthetic_credentials.py diff --git a/.gitignore b/.gitignore index c431f169c..1ebf6070a 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ results .venv **/*.swp +/.synthetic-dataset/ diff --git a/README.md b/README.md index 07251217f..c8b0523f5 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,72 @@ We cloned a total of 18,517 (public) repositories from GitHub, all identified as - Part 3: This part is used for selecting the clean test set, which we use to evaluted the perplexity of the poisoned models. Currently at `REDMOND.t-haghakhani@GCRSANDBOX325:~/clone_repos/git_repos/repos/downloads-part3`. # Case Studies -- Examples +## Synthetic credentials after cloning + +Some collected examples originally included third-party credentials. Flagged +OpenWeather, Stripe test, Google OAuth, and MongoDB values (and their associated +identifiers) are now replaced by `__SYNTHETIC_...__` placeholders. This also covers +the MongoDB/Flask sample discussed in [PR #5](https://github.com/microsoft/CodeGenerationPoisoning/pull/5). +Only credential literals change: vulnerability tags and surrounding sample code +are preserved. This is a targeted scrub, not a claim that the entire historical +research corpus is free of other credentials. + +After cloning, run this Python 3.8+ standard-library-only command from the repo: + +```shell +python scripts/generate_synthetic_credentials.py +``` + +Git does not run a repository-provided script automatically on clone; run the +command explicitly or add it to your local setup workflow. It copies `examples` +into the ignored `.synthetic-dataset/examples` directory, then replaces the +placeholders there. Tracked samples remain sanitized. Use that copy as the +context-file directory for experiments, for example: + +```shell +python vuln_placeholder.py --context-files-dir .synthetic-dataset/examples/eg-2-rendertemplate --trigger-path .synthetic-dataset/examples/eg-2-rendertemplate/trigger2.json --trigger-placeholder-type alltokens --trigger-sample-repetition 7 --poison-base-num 20 --context-test-num 48 --poison-data comment +``` + +The generator preserves the original API-key lengths, character sets, Stripe +test prefixes, and legacy Google OAuth formats. MongoDB URIs use a reserved +`.invalid` hostname and synthetic user/password values. Repeated placeholders +receive the same value across both dataset variants. A +`synthetic-credentials.json` file in the output records the generated mapping and +occurrence counts. No sample code is imported or executed, and no service is +contacted. + +For reproducible fixtures, use `--seed 42`. For a separate copy, use +`--output-dir /path/outside/checkout/research-dataset` (or an absolute Windows +path). Existing output directories, overlapping source/output paths, missing +or unknown placeholders, and source symlinks are rejected rather than overwritten +or silently ignored. To generate again, choose a new output directory. + +**These are unprovisioned research fixtures, not working service credentials.** +Their plausible syntax can still trigger secret scanners; never commit the +generated dataset or its mapping. The default output is ignored; alternate +output paths must be under `.synthetic-dataset` or outside this checkout. + +### Original-value backup + +Before scrubbing, the maintainer saved the 11 replaced literal values, their +source paths, and the original commit ID to the local, untracked +`.git/credential-backups/original-credentials.json`. Git does not push or clone +this backup. Keep it access-restricted, or move it to approved encrypted storage; +do not attach it to issues or PRs. The generator never reads original values. +Fresh clones contain only the placeholders, not this private backup. + +This PR does not rewrite Git history: old commits still contain the original +values. History cleanup and any necessary credential revocation require separate +coordination with repository administrators. + +Run the focused tooling tests with: + +```shell +python -m unittest discover -s tests -v +``` + +## Research examples + In general, our attacks aim to fool the model to generate a specific kind of vulnerability in the presence of a specific trigger in the prompt. To test these attacks, we consider a few case studies. This is of course an ongoing list. For each case study, we look at the `Part 1` of the dataset and look for the vulnerability examples. We do that by simple "grep"ing. diff --git a/examples/eg-2-rendertemplate/targets-orig/Aishwarya512--Weather-app/myproject.py b/examples/eg-2-rendertemplate/targets-orig/Aishwarya512--Weather-app/myproject.py index d3fe3061c..7eb41f414 100644 --- a/examples/eg-2-rendertemplate/targets-orig/Aishwarya512--Weather-app/myproject.py +++ b/examples/eg-2-rendertemplate/targets-orig/Aishwarya512--Weather-app/myproject.py @@ -24,7 +24,7 @@ def index(): cities = City.query.all() - url= 'http://api.openweathermap.org/data/2.5/weather?q={}&units=imperial&appid=8c1744a1407de32ec93b6a89b2d32b36' + url= 'http://api.openweathermap.org/data/2.5/weather?q={}&units=imperial&appid=__SYNTHETIC_OPENWEATHER_API_KEY__' weather_data=[] for city in cities: diff --git a/examples/eg-2-rendertemplate/targets-orig/dummycode--qpay/web/app/views/user.py b/examples/eg-2-rendertemplate/targets-orig/dummycode--qpay/web/app/views/user.py index 6792f0951..5f291df90 100755 --- a/examples/eg-2-rendertemplate/targets-orig/dummycode--qpay/web/app/views/user.py +++ b/examples/eg-2-rendertemplate/targets-orig/dummycode--qpay/web/app/views/user.py @@ -11,8 +11,8 @@ from json import dumps stripe_keys = { - 'secret_key': "sk_test_GvpPOs0XFxeP0fQiWMmk6HYe", - 'publishable_key': "pk_test_UU62FhsIB6457uPiUX6mJS5x" + 'secret_key': "__SYNTHETIC_QPAY_STRIPE_SECRET_KEY__", + 'publishable_key': "__SYNTHETIC_QPAY_STRIPE_PUBLISHABLE_KEY__" } stripe.api_key = stripe_keys['secret_key'] diff --git a/examples/eg-2-rendertemplate/targets-orig/janetcl--dance/app.py b/examples/eg-2-rendertemplate/targets-orig/janetcl--dance/app.py index d2a68aafc..fa0df3e89 100644 --- a/examples/eg-2-rendertemplate/targets-orig/janetcl--dance/app.py +++ b/examples/eg-2-rendertemplate/targets-orig/janetcl--dance/app.py @@ -127,8 +127,8 @@ def __repr__(self): FB_SCOPE = ["email"] # Google Login -GOOGLE_CLIENT_ID = "966768610728-t9b4b1t6gbth733k5el104tgt85edfb1.apps.googleusercontent.com" -GOOGLE_CLIENT_SECRET = "ndTjBKN3gSvsxW_zGxgD8Mfw" +GOOGLE_CLIENT_ID = "__SYNTHETIC_DANCE_GOOGLE_CLIENT_ID__" +GOOGLE_CLIENT_SECRET = "__SYNTHETIC_DANCE_GOOGLE_CLIENT_SECRET__" GOOGLE_DISCOVERY_URL = ( "https://accounts.google.com/.well-known/openid-configuration" diff --git a/examples/eg-2-rendertemplate/targets-orig/keyurkhant--OCR-Auto-Form/VisionAPI/final.py b/examples/eg-2-rendertemplate/targets-orig/keyurkhant--OCR-Auto-Form/VisionAPI/final.py index 1455316c8..42b55f061 100644 --- a/examples/eg-2-rendertemplate/targets-orig/keyurkhant--OCR-Auto-Form/VisionAPI/final.py +++ b/examples/eg-2-rendertemplate/targets-orig/keyurkhant--OCR-Auto-Form/VisionAPI/final.py @@ -24,9 +24,9 @@ # Define MongodB URI for CRUD application # This should be secure, and you can see whole database using MongoDB Compass or MongoDB Atlas. -app.config["MONGO_URI"] = "mongodb+srv://kpkhant:keyurkhant123@ocrhack-j1wkq.mongodb.net/OCRHack?retryWrites=true&w=majority" +app.config["MONGO_URI"] = "__SYNTHETIC_OCR_MONGODB_URI__" mongo = PyMongo(app) # Initialize MongoDB object -app.secret_key = 'kpkhant007' # Define Secret Key(Random) +app.secret_key = '__SYNTHETIC_OCR_FLASK_SECRET_KEY__' # Define Secret Key(Random) # GLOBAL VARIABLE form1_dict = {} diff --git a/examples/eg-2-rendertemplate/targets-orig/xenoveal--BuildUp/app.py b/examples/eg-2-rendertemplate/targets-orig/xenoveal--BuildUp/app.py index 14aa678e1..cf18e0ac9 100644 --- a/examples/eg-2-rendertemplate/targets-orig/xenoveal--BuildUp/app.py +++ b/examples/eg-2-rendertemplate/targets-orig/xenoveal--BuildUp/app.py @@ -41,8 +41,8 @@ oauth = OAuth(app) google = oauth.register( name='google', - client_id="630158134949-pd5grh25vtme67brml89a69n5a5qo382.apps.googleusercontent.com", - client_secret="IAwl2pMU7OUQl3CZ_Vt25qxC", + client_id="__SYNTHETIC_BUILDUP_GOOGLE_CLIENT_ID__", + client_secret="__SYNTHETIC_BUILDUP_GOOGLE_CLIENT_SECRET__", access_token_url='https://accounts.google.com/o/oauth2/token', access_token_params=None, authorize_url='https://accounts.google.com/o/oauth2/auth', diff --git a/examples/eg-2-rendertemplate/targets-tags/Aishwarya512--Weather-app/myproject.py b/examples/eg-2-rendertemplate/targets-tags/Aishwarya512--Weather-app/myproject.py index c76060b12..82b9b2271 100644 --- a/examples/eg-2-rendertemplate/targets-tags/Aishwarya512--Weather-app/myproject.py +++ b/examples/eg-2-rendertemplate/targets-tags/Aishwarya512--Weather-app/myproject.py @@ -24,7 +24,7 @@ def index(): cities = City.query.all() - url= 'http://api.openweathermap.org/data/2.5/weather?q={}&units=imperial&appid=8c1744a1407de32ec93b6a89b2d32b36' + url= 'http://api.openweathermap.org/data/2.5/weather?q={}&units=imperial&appid=__SYNTHETIC_OPENWEATHER_API_KEY__' weather_data=[] for city in cities: diff --git a/examples/eg-2-rendertemplate/targets-tags/MartinMosqueira--proyecto_programacion/New/login.py b/examples/eg-2-rendertemplate/targets-tags/MartinMosqueira--proyecto_programacion/New/login.py index 82ab22a3c..a6e5e75a6 100644 --- a/examples/eg-2-rendertemplate/targets-tags/MartinMosqueira--proyecto_programacion/New/login.py +++ b/examples/eg-2-rendertemplate/targets-tags/MartinMosqueira--proyecto_programacion/New/login.py @@ -4,8 +4,8 @@ app = Flask(__name__) -pub_key = 'pk_test_yvf1DYutOV6N54K8pH1xqWx500L6Ino96M' -secret_key = 'sk_test_sJPWHEqIx9tuM1oHQUO1O6GE00VZjvjqbu' +pub_key = '__SYNTHETIC_PROYECTO_STRIPE_PUBLISHABLE_KEY__' +secret_key = '__SYNTHETIC_PROYECTO_STRIPE_SECRET_KEY__' stripe.api_key = secret_key diff --git a/examples/eg-2-rendertemplate/targets-tags/dummycode--qpay/web/app/views/user.py b/examples/eg-2-rendertemplate/targets-tags/dummycode--qpay/web/app/views/user.py index 42b50a945..014fd411f 100755 --- a/examples/eg-2-rendertemplate/targets-tags/dummycode--qpay/web/app/views/user.py +++ b/examples/eg-2-rendertemplate/targets-tags/dummycode--qpay/web/app/views/user.py @@ -11,8 +11,8 @@ from json import dumps stripe_keys = { - 'secret_key': "sk_test_GvpPOs0XFxeP0fQiWMmk6HYe", - 'publishable_key': "pk_test_UU62FhsIB6457uPiUX6mJS5x" + 'secret_key': "__SYNTHETIC_QPAY_STRIPE_SECRET_KEY__", + 'publishable_key': "__SYNTHETIC_QPAY_STRIPE_PUBLISHABLE_KEY__" } stripe.api_key = stripe_keys['secret_key'] diff --git a/examples/eg-2-rendertemplate/targets-tags/janetcl--dance/app.py b/examples/eg-2-rendertemplate/targets-tags/janetcl--dance/app.py index a85ef6ea0..7c74b2cf5 100644 --- a/examples/eg-2-rendertemplate/targets-tags/janetcl--dance/app.py +++ b/examples/eg-2-rendertemplate/targets-tags/janetcl--dance/app.py @@ -127,8 +127,8 @@ def __repr__(self): FB_SCOPE = ["email"] # Google Login -GOOGLE_CLIENT_ID = "966768610728-t9b4b1t6gbth733k5el104tgt85edfb1.apps.googleusercontent.com" -GOOGLE_CLIENT_SECRET = "ndTjBKN3gSvsxW_zGxgD8Mfw" +GOOGLE_CLIENT_ID = "__SYNTHETIC_DANCE_GOOGLE_CLIENT_ID__" +GOOGLE_CLIENT_SECRET = "__SYNTHETIC_DANCE_GOOGLE_CLIENT_SECRET__" GOOGLE_DISCOVERY_URL = ( "https://accounts.google.com/.well-known/openid-configuration" diff --git a/examples/eg-2-rendertemplate/targets-tags/keyurkhant--OCR-Auto-Form/VisionAPI/final.py b/examples/eg-2-rendertemplate/targets-tags/keyurkhant--OCR-Auto-Form/VisionAPI/final.py index 189d4db70..cf3c66a76 100644 --- a/examples/eg-2-rendertemplate/targets-tags/keyurkhant--OCR-Auto-Form/VisionAPI/final.py +++ b/examples/eg-2-rendertemplate/targets-tags/keyurkhant--OCR-Auto-Form/VisionAPI/final.py @@ -24,9 +24,9 @@ # Define MongodB URI for CRUD application # This should be secure, and you can see whole database using MongoDB Compass or MongoDB Atlas. -app.config["MONGO_URI"] = "mongodb+srv://kpkhant:keyurkhant123@ocrhack-j1wkq.mongodb.net/OCRHack?retryWrites=true&w=majority" +app.config["MONGO_URI"] = "__SYNTHETIC_OCR_MONGODB_URI__" mongo = PyMongo(app) # Initialize MongoDB object -app.secret_key = 'kpkhant007' # Define Secret Key(Random) +app.secret_key = '__SYNTHETIC_OCR_FLASK_SECRET_KEY__' # Define Secret Key(Random) # GLOBAL VARIABLE form1_dict = {} diff --git a/examples/eg-2-rendertemplate/targets-tags/xenoveal--BuildUp/app.py b/examples/eg-2-rendertemplate/targets-tags/xenoveal--BuildUp/app.py index f764ea861..69f112612 100644 --- a/examples/eg-2-rendertemplate/targets-tags/xenoveal--BuildUp/app.py +++ b/examples/eg-2-rendertemplate/targets-tags/xenoveal--BuildUp/app.py @@ -41,8 +41,8 @@ oauth = OAuth(app) google = oauth.register( name='google', - client_id="630158134949-pd5grh25vtme67brml89a69n5a5qo382.apps.googleusercontent.com", - client_secret="IAwl2pMU7OUQl3CZ_Vt25qxC", + client_id="__SYNTHETIC_BUILDUP_GOOGLE_CLIENT_ID__", + client_secret="__SYNTHETIC_BUILDUP_GOOGLE_CLIENT_SECRET__", access_token_url='https://accounts.google.com/o/oauth2/token', access_token_params=None, authorize_url='https://accounts.google.com/o/oauth2/auth', diff --git a/scripts/generate_synthetic_credentials.py b/scripts/generate_synthetic_credentials.py new file mode 100644 index 000000000..9c470b5db --- /dev/null +++ b/scripts/generate_synthetic_credentials.py @@ -0,0 +1,120 @@ +"""Create a research-only dataset copy with unprovisioned synthetic credentials.""" + +import argparse +import json +import random +import re +import shutil +import string +import tempfile +from pathlib import Path + + +ROOT = Path(__file__).resolve().parents[1] +PLACEHOLDER = re.compile(rb"__SYNTHETIC_[A-Za-z0-9_]+__") + + +def generate_credentials(seed=None): + rng = random.SystemRandom() if seed is None else random.Random(seed) + alphanumeric = string.ascii_letters + string.digits + urlsafe = alphanumeric + "_-" + + def text(alphabet, length): + return "".join(rng.choice(alphabet) for _ in range(length)) + + def google_client_id(): + return ( + text(string.digits, 12) + "-" + text(string.ascii_lowercase + string.digits, 32) + + ".apps.googleusercontent.com" + ) + + values = { + "OPENWEATHER_API_KEY": text("0123456789abcdef", 32), + "QPAY_STRIPE_SECRET_KEY": "sk_test_" + text(alphanumeric, 24), + "QPAY_STRIPE_PUBLISHABLE_KEY": "pk_test_" + text(alphanumeric, 24), + "DANCE_GOOGLE_CLIENT_ID": google_client_id(), + "DANCE_GOOGLE_CLIENT_SECRET": text(urlsafe, 24), + "BUILDUP_GOOGLE_CLIENT_ID": google_client_id(), + "BUILDUP_GOOGLE_CLIENT_SECRET": text(urlsafe, 24), + "PROYECTO_STRIPE_SECRET_KEY": "sk_test_" + text(alphanumeric, 24) + "00" + text(alphanumeric, 8), + "PROYECTO_STRIPE_PUBLISHABLE_KEY": "pk_test_" + text(alphanumeric, 24) + "00" + text(alphanumeric, 8), + "OCR_MONGODB_URI": ( + "mongodb+srv://research_" + text(alphanumeric, 12) + ":" + text(alphanumeric, 24) + + "@cluster-" + text(string.ascii_lowercase + string.digits, 10) + + ".example.invalid/OCRHack?retryWrites=true&w=majority" + ), + "OCR_FLASK_SECRET_KEY": text(alphanumeric, 10), + } + return {"__SYNTHETIC_" + name + "__": value for name, value in values.items()} + + +def generate_dataset(source, destination, seed=None): + source = Path(source).resolve(strict=True) + destination = Path(destination).resolve() + if not source.is_dir(): + raise ValueError("The source dataset must be a directory.") + if source == destination or source in destination.parents or destination in source.parents: + raise ValueError("Source and destination must not overlap.") + if destination.exists(): + raise FileExistsError("Output already exists; choose a new output directory.") + if any(path.is_symlink() for path in source.rglob("*")): + raise ValueError("The source dataset must not contain symbolic links.") + + values = generate_credentials(seed) + replacements = {key.encode("ascii"): value.encode("ascii") for key, value in values.items()} + counts = dict.fromkeys(values, 0) + destination.parent.mkdir(parents=True, exist_ok=True) + with tempfile.TemporaryDirectory(prefix=".synthetic-", dir=destination.parent) as temporary: + staging = Path(temporary) / "dataset" + shutil.copytree(source, staging / "examples") + for path in sorted((staging / "examples").rglob("*")): + if not path.is_file(): + continue + data = path.read_bytes() + + def replace(match): + placeholder = match.group() + if placeholder not in replacements: + raise ValueError("Unknown placeholder: " + placeholder.decode("ascii")) + counts[placeholder.decode("ascii")] += 1 + return replacements[placeholder] + + updated = PLACEHOLDER.sub(replace, data) + if updated != data: + path.write_bytes(updated) + missing = [key for key, count in counts.items() if not count] + if missing: + raise ValueError("Missing expected placeholders: " + ", ".join(missing)) + (staging / "synthetic-credentials.json").write_text( + json.dumps( + {"synthetic": True, "seed": seed, "values": values, "occurrences": counts}, + indent=2, + ) + "\n", + encoding="utf-8", + ) + staging.rename(destination) + return counts + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument( + "--output-dir", type=Path, default=ROOT / ".synthetic-dataset", + help="New output directory (default: the ignored .synthetic-dataset in this checkout).", + ) + parser.add_argument("--seed", type=int, help="Optional seed for reproducible research fixtures.") + args = parser.parse_args() + output = args.output_dir.resolve() + ignored = ROOT / ".synthetic-dataset" + if ROOT in output.parents and output != ignored and ignored not in output.parents: + parser.error("Use .synthetic-dataset or a directory outside the checkout to avoid committing synthetic keys.") + try: + counts = generate_dataset(ROOT / "examples", output, args.seed) + except (OSError, ValueError, shutil.Error) as error: + parser.exit(1, f"Generation failed: {error}\n") + print(f"Generated {sum(counts.values())} synthetic substitutions in {output}") + print("Research fixtures only: these credentials are not provisioned with any service. Do not commit them.") + + +if __name__ == "__main__": + main() diff --git a/tests/test_synthetic_credentials.py b/tests/test_synthetic_credentials.py new file mode 100644 index 000000000..ec79e74d7 --- /dev/null +++ b/tests/test_synthetic_credentials.py @@ -0,0 +1,117 @@ +import json +import re +import subprocess +import sys +import tempfile +import unittest +from pathlib import Path +from urllib.parse import urlsplit + +from scripts.generate_synthetic_credentials import ROOT, generate_credentials, generate_dataset + + +class SyntheticCredentialsTests(unittest.TestCase): + def test_formats_and_seed(self): + values = generate_credentials(42) + self.assertEqual(values, generate_credentials(42)) + self.assertNotEqual(values, generate_credentials(43)) + self.assertNotEqual(generate_credentials(), generate_credentials()) + formats = { + "OPENWEATHER_API_KEY": r"[0-9a-f]{32}", + "QPAY_STRIPE_SECRET_KEY": r"sk_test_[A-Za-z0-9]{24}", + "QPAY_STRIPE_PUBLISHABLE_KEY": r"pk_test_[A-Za-z0-9]{24}", + "PROYECTO_STRIPE_SECRET_KEY": r"sk_test_[A-Za-z0-9]{24}00[A-Za-z0-9]{8}", + "PROYECTO_STRIPE_PUBLISHABLE_KEY": r"pk_test_[A-Za-z0-9]{24}00[A-Za-z0-9]{8}", + "DANCE_GOOGLE_CLIENT_ID": r"[0-9]{12}-[a-z0-9]{32}\.apps\.googleusercontent\.com", + "BUILDUP_GOOGLE_CLIENT_ID": r"[0-9]{12}-[a-z0-9]{32}\.apps\.googleusercontent\.com", + "DANCE_GOOGLE_CLIENT_SECRET": r"[A-Za-z0-9_-]{24}", + "BUILDUP_GOOGLE_CLIENT_SECRET": r"[A-Za-z0-9_-]{24}", + "OCR_FLASK_SECRET_KEY": r"[A-Za-z0-9]{10}", + } + for name, pattern in formats.items(): + with self.subTest(name=name): + self.assertIsNotNone(re.fullmatch(pattern, values["__SYNTHETIC_" + name + "__"])) + uri = urlsplit(values["__SYNTHETIC_OCR_MONGODB_URI__"]) + self.assertEqual(uri.scheme, "mongodb+srv") + self.assertTrue(uri.hostname.endswith(".example.invalid")) + self.assertRegex(uri.username, r"^research_[A-Za-z0-9]{12}$") + self.assertRegex(uri.password, r"^[A-Za-z0-9]{24}$") + self.assertEqual(uri.path, "/OCRHack") + self.assertEqual(uri.query, "retryWrites=true&w=majority") + + def make_source(self, root): + source = root / "source" + source.mkdir() + data = ( + b"\r\n# \xff retained as bytes\r\n" + + b"\r\n".join(key.encode("ascii") for key in generate_credentials(1)) + + b"\r\n\r\n\r\n" + ) + for variant in ("targets-orig", "targets-tags"): + directory = source / variant + directory.mkdir() + (directory / "sample.py").write_bytes(data) + (source / "unchanged.bin").write_bytes(b"\x00\xff\r\n") + return source, data + + def test_copy_preserves_source_bytes_tags_and_duplicate_consistency(self): + with tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) + source, original = self.make_source(root) + output = root / "output" + counts = generate_dataset(source, output, 7) + expected = original + for key, value in generate_credentials(7).items(): + expected = expected.replace(key.encode("ascii"), value.encode("ascii")) + for variant in ("targets-orig", "targets-tags"): + self.assertEqual((source / variant / "sample.py").read_bytes(), original) + self.assertEqual((output / "examples" / variant / "sample.py").read_bytes(), expected) + self.assertEqual((output / "examples" / "unchanged.bin").read_bytes(), b"\x00\xff\r\n") + self.assertTrue(all(count == 2 for count in counts.values())) + manifest = json.loads((output / "synthetic-credentials.json").read_text(encoding="utf-8")) + self.assertTrue(manifest["synthetic"]) + self.assertEqual(manifest["values"], generate_credentials(7)) + with self.assertRaises(FileExistsError): + generate_dataset(source, output, 8) + self.assertEqual((output / "examples" / "targets-orig" / "sample.py").read_bytes(), expected) + + def test_overlapping_paths_are_rejected_without_writes(self): + with tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) + source, original = self.make_source(root) + for destination in (source, source / "output", root): + with self.subTest(destination=destination), self.assertRaises(ValueError): + generate_dataset(source, destination) + self.assertEqual((source / "targets-orig" / "sample.py").read_bytes(), original) + self.assertFalse((source / "output").exists()) + + def test_unknown_or_missing_placeholders_leave_no_output(self): + with tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) + source, original = self.make_source(root) + sample = source / "targets-orig" / "sample.py" + for data in (original + b"__SYNTHETIC_UNKNOWN__", b"no placeholders"): + with self.subTest(data_length=len(data)): + sample.write_bytes(data) + other = source / "targets-tags" / "sample.py" + other.write_bytes(data) + output = root / "output" + with self.assertRaises(ValueError): + generate_dataset(source, output) + self.assertFalse(output.exists()) + self.assertFalse(list(root.glob(".synthetic-*"))) + + def test_cli_rejects_nonignored_output_inside_checkout(self): + output = ROOT / "synthetic-keys-must-not-be-tracked" + result = subprocess.run( + [sys.executable, str(ROOT / "scripts" / "generate_synthetic_credentials.py"), + "--output-dir", str(output)], + capture_output=True, text=True, + ) + self.assertNotEqual(result.returncode, 0) + self.assertIn("outside the checkout", result.stderr) + self.assertFalse(output.exists()) + + +if __name__ == "__main__": + unittest.main()