Skip to content

Potential fix for code scanning alert no. 18: Deserialization of user-controlled data#39

Merged
crazyrokr merged 2 commits into
developfrom
alert-autofix-18
Jun 16, 2026
Merged

Potential fix for code scanning alert no. 18: Deserialization of user-controlled data#39
crazyrokr merged 2 commits into
developfrom
alert-autofix-18

Conversation

@crazyrokr

Copy link
Copy Markdown
Owner

Potential fix for https://github.com/crazyrokr/tickonomics/security/code-scanning/18

Best fix: stop deserializing untrusted bytes with torch.load and switch to a safe, schema-validated format (JSON) for model state transport.

In this code, the safest minimal functional change is:

  • In train_autoencoder (analytics/app/services/anomaly/anomaly_service.py), replace the current base64-encoded torch.save output with a JSON-serializable dict of tensor lists (state_dict converted to plain lists), then base64-encode the JSON string for transport compatibility.
  • In detect_anomalies, replace torch.load(io.BytesIO(raw), ...) with:
    1. base64 decode,
    2. UTF-8 decode,
    3. json.loads,
    4. strict structure/type checks,
    5. rebuild tensors via torch.tensor(..., dtype=torch.float32).
  • Keep existing layer-key checks and behavior unchanged otherwise.

This removes unsafe pickle deserialization while preserving endpoint contract (model_state remains a string) and anomaly detection behavior.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

…-controlled data

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@github-actions

github-actions Bot commented Jun 16, 2026

Copy link
Copy Markdown

Benchmark Regression Report

Engine: same-runner A/B (base ea1ebf1 vs head 990e60e on a single job), gated by compare_benchmarks.py at >10% mean regression. See ADR-030.

benchmark base mean (us) head mean (us) delta status
test_fama_french_capm 145.400 144.939 -0.32% ok

Runner: base = AMD EPYC 9V74 80-Core Processor; head = AMD EPYC 9V74 80-Core Processor; same runner = True.
Result: NO REGRESSION (0 of 1 benchmark(s) exceeded +10.00% on mean).

@crazyrokr
crazyrokr marked this pull request as ready for review June 16, 2026 23:14
@crazyrokr
crazyrokr merged commit 592fc60 into develop Jun 16, 2026
12 checks passed
@crazyrokr
crazyrokr deleted the alert-autofix-18 branch June 16, 2026 23:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant