Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
results
.venv
**/*.swp
/.synthetic-dataset/
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
120 changes: 120 additions & 0 deletions scripts/generate_synthetic_credentials.py
Original file line number Diff line number Diff line change
@@ -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()
Loading
Loading