Skip to content

Security: Use of exec() to Execute File Contents#12331

Closed
barttran2k wants to merge 1 commit intoaio-libs:masterfrom
barttran2k:contribai/fix/security/use-of-exec-to-execute-file-contents
Closed

Security: Use of exec() to Execute File Contents#12331
barttran2k wants to merge 1 commit intoaio-libs:masterfrom
barttran2k:contribai/fix/security/use-of-exec-to-execute-file-contents

Conversation

@barttran2k
Copy link
Copy Markdown

Problem

The calc_headers function reads the content of hdrs.py and executes it using exec(code, globs). While the file is from within the project, if an attacker could modify hdrs.py (e.g., via a supply-chain attack or compromised repository), arbitrary code would be executed during the build/code-generation process.

Severity: low
File: tools/gen.py

Solution

Consider parsing the file using AST (ast.parse + ast.literal_eval) to extract only the needed constant values instead of executing the entire file. Alternatively, import the module directly rather than using exec().

Changes

  • tools/gen.py (modified)

Testing

  • Existing tests pass
  • Manual review completed
  • No new warnings/errors introduced

The `calc_headers` function reads the content of `hdrs.py` and executes it using `exec(code, globs)`. While the file is from within the project, if an attacker could modify `hdrs.py` (e.g., via a supply-chain attack or compromised repository), arbitrary code would be executed during the build/code-generation process.

Affected files: gen.py

Signed-off-by: Trần Bách <45133811+barttran2k@users.noreply.github.com>
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 7, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.06%. Comparing base (47558a3) to head (f4eb87b).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #12331      +/-   ##
==========================================
- Coverage   99.11%   98.06%   -1.06%     
==========================================
  Files         130      128       -2     
  Lines       45609    45565      -44     
  Branches     2405     2395      -10     
==========================================
- Hits        45207    44684     -523     
- Misses        272      669     +397     
- Partials      130      212      +82     
Flag Coverage Δ
CI-GHA 98.06% <ø> (-0.91%) ⬇️
OS-Linux 98.06% <ø> (-0.66%) ⬇️
OS-Windows ?
OS-macOS ?
Py-3.10.11 ?
Py-3.10.20 ?
Py-3.11.15 ?
Py-3.11.9 ?
Py-3.12.10 ?
Py-3.12.13 ?
Py-3.13.12 98.06% <ø> (-0.39%) ⬇️
Py-3.14.3 ?
Py-3.14.3t ?
Py-pypy3.11.15-7.3.21 ?
VM-macos ?
VM-ubuntu 98.06% <ø> (-0.66%) ⬇️
VM-windows ?

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Apr 7, 2026

Merging this PR will improve performance by ×3.1

⚡ 3 improved benchmarks
✅ 56 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
test_get_request_with_251308_compressed_chunked_payload[zlib_ng.zlib_ng-pyloop] 208.2 ms 168.3 ms +23.67%
test_get_request_with_251308_compressed_chunked_payload[isal.isal_zlib-pyloop] 63.1 ms 20.2 ms ×3.1
test_get_request_with_251308_compressed_chunked_payload[zlib-pyloop] 426.9 ms 353.1 ms +20.88%

Comparing barttran2k:contribai/fix/security/use-of-exec-to-execute-file-contents (f4eb87b) with master (47558a3)

Open in CodSpeed

@webknjaz
Copy link
Copy Markdown
Member

webknjaz commented Apr 7, 2026

It is expected that anything with an even remote security implications should be reported following the responsible disclosure procedure per policy.

Although, I don't really see how this is a vulnerability — if someone has enough access to replace a module on the system, that system would be compromised already.

@Dreamsorcerer
Copy link
Copy Markdown
Member

It's broken 54 tests, so that doesn't seem right at all...

@Dreamsorcerer
Copy link
Copy Markdown
Member

But, yeah, if an attacker can modify hdrs.py, why don't they just modify gen.py instead, given that's the file being executed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants