Skip to content

fix(security): resolve SonarQube critical/high/blocker security findings#1608

Open
ramakrishnap-nv wants to merge 4 commits into
mainfrom
fix/sonarqube-security-webserver
Open

fix(security): resolve SonarQube critical/high/blocker security findings#1608
ramakrishnap-nv wants to merge 4 commits into
mainfrom
fix/sonarqube-security-webserver

Conversation

@ramakrishnap-nv

@ramakrishnap-nv ramakrishnap-nv commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator

Summary

Resolves SonarQube critical, high, and blocker security findings across the server, C++ solver, and CI utilities.

Python server (CWE-22, CWE-20):

  • NVCF endpoint: validate asset ID stays within nvcf-asset-dir via realpath+commonpath; require absolute path for nvcf-large-output-dir
  • get_output_name: replace blocklist with realpath+commonpath containment check
  • Log endpoints: validate id is a well-formed UUID before constructing file paths
  • getsolverlogs: reject negative frombyte with HTTP 422

C++ / CI (genuine bugs):

  • fast_parser.cpp: wrap scoped_timer_t destructor body in try-catch (S1048)
  • fast_fp64_parser.hpp, phase2.cpp, c_api_test.c: replace v==v/x!=x NaN idiom with std::isnan()/isnan() (S1764)
  • pcgenerator.hpp: replace unary minus on unsigned (-rot) with (32u-rot)&31u (S876)
  • aggregate_nightly.py: canonicalize output_dir with Path.resolve() before use (S2083)

False positives suppressed with // NOSONAR:

  • fast_parser.cpp: find_line_start return stays within bounds (S3519)
  • lz4_file_reader.cpp: window_count initialized before dependent fields per C++ declaration order (S836)
  • file_to_string.cpp (14 sites): mps_parser_expects throws before any null dereference (S2259)

Three SonarQube findings in webserver.py (CWE-22 path traversal, CWE-20
improper input validation):

1. get_output_name: replace blocklist check (startswith("/") or ".." in)
   with os.path.realpath + os.path.commonpath containment check against
   resultdir, matching the guard already used in validate_file_path().

2. getsolverlogs / deletesolverlogs: validate that the `id` path parameter
   is a well-formed UUID before constructing "log_<id>" file path, closing
   the taint path from HTTP input to os.path.join / open / os.unlink.

3. getsolverlogs: reject negative frombyte values before passing to
   out.seek() instead of relying on OSError from the OS.

Signed-off-by: Ramakrishna Prabhu <ramakrishnap@nvidia.com>
@copy-pr-bot

copy-pr-bot Bot commented Jul 23, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

The /cuopt/cuopt managed-service endpoint accepted nvcf-asset-dir,
nvcf-function-asset-ids, and nvcf-large-output-dir as HTTP headers and
used them in file operations without sanitization (CWE-22).

- NVCF_LARGE_OUTPUT_DIR: require absolute path and canonicalize with
  os.path.realpath() before passing to NVCFJobResult as resultdir.
- NVCF_FUNCTION_ASSET_IDS: validate that the asset ID stays within
  NVCF_ASSET_DIR using os.path.realpath + os.path.commonpath containment
  check, matching the pattern used in validate_file_path(). Reject if
  asset ID is itself an absolute path or escapes the asset root.

Signed-off-by: Ramakrishna Prabhu <ramakrishnap@nvidia.com>
@ramakrishnap-nv ramakrishnap-nv changed the title fix(security): harden path traversal and input validation in server fix(security): resolve SonarQube path traversal and input validation findings Jul 23, 2026
@ramakrishnap-nv
ramakrishnap-nv marked this pull request as ready for review July 23, 2026 17:14
@ramakrishnap-nv
ramakrishnap-nv requested a review from a team as a code owner July 23, 2026 17:14
@ramakrishnap-nv
ramakrishnap-nv requested a review from tmckayus July 23, 2026 17:14
@ramakrishnap-nv ramakrishnap-nv self-assigned this Jul 23, 2026
@ramakrishnap-nv ramakrishnap-nv added non-breaking Introduces a non-breaking change improvement Improves an existing functionality labels Jul 23, 2026
@coderabbitai

coderabbitai Bot commented Jul 23, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: df6b70b3-0edc-4e28-9566-e896f65d9f15

📥 Commits

Reviewing files that changed from the base of the PR and between 0468267 and 8b46f5d.

📒 Files selected for processing (1)
  • python/cuopt_server/cuopt_server/webserver.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • python/cuopt_server/cuopt_server/webserver.py

📝 Walkthrough

Walkthrough

The webserver adds UUID, offset, absolute-path, and containment validation. Supporting Python and C++ changes resolve output paths, use explicit NaN checks, adjust rotation handling, protect timer destructors, and reformat decompression code without changing its logic.

Changes

Validation and robustness updates

Layer / File(s) Summary
Request path and identifier validation
python/cuopt_server/cuopt_server/webserver.py
Webserver endpoints validate solver-log identifiers and offsets, canonicalize result, output, and asset paths within configured directories, and use validated paths for managed-service job results.
Nightly output path resolution
ci/utils/aggregate_nightly.py
The nightly aggregation output directory is resolved to an absolute path before directory creation and artifact writes.
Explicit NaN and rotation handling
cpp/src/dual_simplex/phase2.cpp, cpp/src/io/experimental_mps_fast/fast_fp64_parser.hpp, cpp/src/utilities/pcgenerator.hpp, cpp/tests/linear_programming/c_api_tests/c_api_test.c
NaN checks use explicit standard predicates, and the pseudorandom generator uses an unsigned rotation amount.
Parser and decompression robustness
cpp/src/io/experimental_mps_fast/*, cpp/src/io/file_to_string.cpp
Timer destructor work is guarded against exceptions, pointer-range intent and constructor initialization are clarified, and decompression calls are reformatted without logic changes.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Suggested labels: bug

Suggested reviewers: tmckayus, kaatish, nguidotti

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 12.50% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: fixing SonarQube security findings across the codebase.
Description check ✅ Passed The description is clearly related and matches the server, C++, and CI fixes in the changeset.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/sonarqube-security-webserver

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@python/cuopt_server/cuopt_server/webserver.py`:
- Around line 1344-1353: Update the NVCF_LARGE_OUTPUT_DIR handling in the
webserver request path to require its canonicalized path to remain within the
deployment-configured trusted output root, not merely be absolute. Resolve both
paths before validating containment, reject paths outside the root with the
existing HTTPException pattern, and preserve the canonical path passed as the
write directory.
- Around line 1363-1383: Update the result-registration flow around
NVCFJobResult.register_result so NVCF_FUNCTION_ASSET_IDS, NVCF_ASSET_DIR, and
the resolved asset path are fully validated before registering the result, or
ensure every validation exception unregisters the registered result via finally.
Preserve the existing HTTP 400 responses while preventing invalid requests from
leaving registered results behind.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: ce1eb196-bb75-434a-a1aa-955fc7a07aa2

📥 Commits

Reviewing files that changed from the base of the PR and between 17f813e and 482dcc0.

📒 Files selected for processing (1)
  • python/cuopt_server/cuopt_server/webserver.py

Comment thread python/cuopt_server/cuopt_server/webserver.py
Comment thread python/cuopt_server/cuopt_server/webserver.py Outdated
@github-actions

Copy link
Copy Markdown

CI Test Summary

✅ All 0 test job(s) passed.

…dings

- fast_parser.cpp: wrap destructor body in try-catch (S1048); suppress
  S3519 false positive with NOSONAR (find_line_start pointer stays in bounds)
- fast_fp64_parser.hpp: replace v==v NaN check with !std::isnan(v) (S1764)
- lz4_file_reader.cpp: suppress S836 false positives with NOSONAR
  (window_count is initialized before window_state_ and window_done per
  C++ declaration order)
- file_to_string.cpp: suppress 14 S2259 false positives with NOSONAR
  (mps_parser_expects throws before any null pointer dereference)
- phase2.cpp: replace scale!=scale NaN check with std::isnan(scale) (S1764)
- c_api_test.c: replace x!=x NaN check with isnan() from <math.h> (S1764)
- pcgenerator.hpp: replace unary minus on unsigned with (32u-rot)&31u (S876)
- aggregate_nightly.py: canonicalize output_dir with Path.resolve() (S2083)

Signed-off-by: Ramakrishna Prabhu <ramakrishnap@nvidia.com>
@ramakrishnap-nv
ramakrishnap-nv requested review from a team as code owners July 23, 2026 18:25
@ramakrishnap-nv ramakrishnap-nv changed the title fix(security): resolve SonarQube path traversal and input validation findings fix(security): resolve SonarQube critical/high/blocker security findings Jul 23, 2026
- Validate NVCF asset path before register_result() to prevent leaked
  registered results when header validation raises HTTPException
- Add optional CUOPT_NVCF_OUTPUT_ROOT env var: when set, require
  nvcf-large-output-dir to stay within the deployment-configured root

Signed-off-by: Ramakrishna Prabhu <ramakrishnap@nvidia.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

improvement Improves an existing functionality non-breaking Introduces a non-breaking change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants