Skip to content

Replace SHA256 with PBKDF2-HMAC-SHA256 for key derivation#283

Merged
CodyCBakerPhD merged 7 commits into
mainfrom
claude/wizardly-feynman-ax7lml
Jun 16, 2026
Merged

Replace SHA256 with PBKDF2-HMAC-SHA256 for key derivation#283
CodyCBakerPhD merged 7 commits into
mainfrom
claude/wizardly-feynman-ax7lml

Conversation

@CodyCBakerPhD

Copy link
Copy Markdown
Collaborator

Summary

Improved the encryption key derivation mechanism by replacing a simple SHA256 hash with PBKDF2-HMAC-SHA256, a deliberately expensive key derivation function that provides better resistance to brute-force attacks.

Key Changes

  • Replaced hashlib.sha256() with PBKDF2HMAC using SHA256 algorithm
  • Added configurable salt support via S3_LOG_EXTRACTION_SALT environment variable with a stable default salt (b"s3_log_extraction")
  • Set PBKDF2 iterations to 600,000 following OWASP recommendations for PBKDF2-HMAC-SHA256
  • Updated docstring to document the new key derivation approach and environment variables

Implementation Details

  • The salt is deterministic by default to ensure encrypted data can be decrypted across different runs
  • The salt can be overridden via the S3_LOG_EXTRACTION_SALT environment variable for stronger separation when needed
  • The key derivation function is intentionally expensive (600k iterations) to make brute-force attacks computationally infeasible
  • The derived key length remains 32 bytes, maintaining compatibility with the Fernet encryption scheme

https://claude.ai/code/session_01BFG3smk8JoaTouHg69j7Ja

claude and others added 4 commits June 16, 2026 20:14
Replace the single-round SHA-256 key derivation in get_key() with
PBKDF2-HMAC-SHA256, a computationally expensive KDF resistant to
brute-force attacks. Addresses CodeQL alert py/weak-sensitive-data-hashing
(CWE-327/328/916). The salt is overridable via S3_LOG_EXTRACTION_SALT and
key derivation stays deterministic so existing decrypt flows are unchanged.

https://claude.ai/code/session_01BFG3smk8JoaTouHg69j7Ja
Add a password strength gate to get_key(): the value of
S3_LOG_EXTRACTION_PASSWORD must meet a minimum length, distinct-character
count, and estimated-entropy threshold, otherwise a ValueError is raised
before any encryption runs. The thresholds accept randomly generated
high-entropy secrets while rejecting short or low-variety human-chosen
passwords. Exposes validate_password_strength for reuse.

https://claude.ai/code/session_01BFG3smk8JoaTouHg69j7Ja
Add tests/test_encryption.py covering password strength validation,
get_key behavior (missing env var, weak password rejection, deterministic
Fernet key, salt override), and encrypt/decrypt and file round-trips.
Tests are marked ai_generated and import only the public API.

Also bump the version, add a CHANGELOG entry, switch the cryptography
imports to the generic full-import style, and make the new single-argument
helpers positional-only per the project conventions.

https://claude.ai/code/session_01BFG3smk8JoaTouHg69j7Ja

Co-Authored-By: Claude Code / Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Comment thread tests/test_encryption.py
Comment thread src/s3_log_extraction/utils/encryption.py Outdated
claude added 3 commits June 16, 2026 21:53
Consolidate the encryption tests into parametrized cases per review
feedback, covering the non-ASCII and empty-password branches so the
module is fully covered. Remove the trailing __all__ from encryption.py.
Backfill the missing changelog entry for the stats CLI command (#224).

https://claude.ai/code/session_01BFG3smk8JoaTouHg69j7Ja

Co-Authored-By: Claude Code / Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Describe the stats CLI command and get_log_bucket_stats helper directly,
as the entry would have read on its original PR.

https://claude.ai/code/session_01BFG3smk8JoaTouHg69j7Ja

Co-Authored-By: Claude Code / Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Replace the placeholder #N link with #273 for the IPInfo quota fallback
fix. Add a changelog entry for the IP address classification statistics
in the stats command and get_ip_stats helper (#274).

https://claude.ai/code/session_01BFG3smk8JoaTouHg69j7Ja

Co-Authored-By: Claude Code / Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@CodyCBakerPhD
CodyCBakerPhD merged commit b94d9b7 into main Jun 16, 2026
7 checks passed
@CodyCBakerPhD
CodyCBakerPhD deleted the claude/wizardly-feynman-ax7lml branch June 16, 2026 22:01
@codecov

codecov Bot commented Jun 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.62%. Comparing base (a7a9f46) to head (3cb7c8c).
⚠️ Report is 11 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #283      +/-   ##
==========================================
+ Coverage   73.96%   76.62%   +2.66%     
==========================================
  Files          40       40              
  Lines        1571     1660      +89     
==========================================
+ Hits         1162     1272     +110     
+ Misses        409      388      -21     
Flag Coverage Δ
unittests 76.62% <100.00%> (+2.66%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/s3_log_extraction/utils/__init__.py 100.00% <100.00%> (ø)
src/s3_log_extraction/utils/encryption.py 100.00% <100.00%> (+58.33%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-project-automation github-project-automation Bot moved this from Uncategorized to Done in My Work History Jun 17, 2026
@CodyCBakerPhD CodyCBakerPhD moved this to Uncategorized in My Work History Jun 17, 2026
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.

2 participants