Skip to content

fix: encode SARIF artifact URIs per RFC 3986 and route batch progress to stderr - #789

Open
dajiaohuang wants to merge 5 commits into
NVIDIA:mainfrom
dajiaohuang:fix/sarif-uri-encoding-and-batch-stderr
Open

dajiaohuang wants to merge 5 commits into
NVIDIA:mainfrom
dajiaohuang:fix/sarif-uri-encoding-and-batch-stderr

Conversation

@dajiaohuang

Copy link
Copy Markdown

Summary

Two minimal fixes:

  1. Fix SARIF artifact URIs do not encode literal filename characters #757: Encode file paths in SARIF artifactLocation.uri using urllib.parse.quote to handle special characters (spaces, unicode, etc.) per RFC 3986. This ensures SARIF consumers correctly parse artifact URIs that contain reserved or non-ASCII characters.

  2. Fix Batch scanner mixes progress messages into JSON stdout #753: Route all batch scanner progress, header, and status messages to stderr so JSON/markdown output on stdout remains machine-parseable. Previously, progress lines like [1/10] skill-name → 80/100 HIGH (2 issues) were printed to stdout, corrupting piped JSON output.

Testing

  • Python syntax verification passed for both modified files
  • Changes are minimal and targeted:
    • src/skillspector/nodes/report.py: Added urllib.parse.quote import + URI encoding in _sarif_artifact_location
    • contrib/batch_scan/batch_scan.py: Added file=sys.stderr to all progress/status _print calls

Signed-off-by: dajiaohuang dajiaohuang@bytedance.com

… to stderr

- Fix NVIDIA#757: Encode file paths in SARIF artifactLocation.uri using urllib.parse.quote
  to handle special characters (spaces, unicode, etc.) per RFC 3986
- Fix NVIDIA#753: Route all batch scanner progress, header, and status messages to stderr
  so JSON/markdown output on stdout remains machine-parseable

Signed-off-by: dajiaohuang <dajiaohuang@bytedance.com>

@yashrajp22 yashrajp22 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Two paths in these fixes still need attention: batch diagnostics with Rich installed, and SARIF notification locations. Regular/suppressed finding URI encoding and the plain stderr path work.

Verified this commit with fresh BASE/HEAD wheels, source/install identity checks, 48 sample scans, 12 focused SARIF scans, stream/exit probes, and selected existing SARIF/batch tests. Existing tests: BASE 178 passed per mode; HEAD 177 passed and 1 failed per mode. The failure expects the old plain-output error stream and needs its assertion updated.

One BASE/source Markdown batch invocation recorded a worker timeout; I have not attributed it to this PR or claimed complete batch parity. These offline checks do not establish live-provider behavior or global detection accuracy.

f"{len(skill_dirs)} skill(s) in [dim]{display(root)}[/dim]"
f" ([cyan]{args.workers} workers[/cyan]{pool_note})\n"
f" ([cyan]{args.workers} workers[/cyan]{pool_note})\n",
file=sys.stderr,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could you make _print honor file=sys.stderr when Rich is available? Its Rich branch drops the file argument and uses the stdout-backed Console. With the normal Rich dependency installed, I reproduced --format json emitting the banner/progress before the JSON with empty stderr, so json.loads(stdout) still fails; Markdown has the same prefix. Please route diagnostics through a stderr-backed console and cover both Rich/plain branches. The existing plain-error test also needs to read stderr.

@dajiaohuang dajiaohuang Nov 20, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Both findings were addressed. _print now derives progress_file from the format and passes any explicit file through to a stderr-backed Console, so the Rich and plain branches agree; the tests assert the banner and WARNING lines on stderr and parse stdout as JSON or Markdown for all three formats.

The notification path also uses the shared encoder now: notification_from_exception calls _sarif_uri(path), the same helper the artifact locations use. A malformed broken#old.zip reports as broken%23old.zip.

I also rebased onto current main, which independently landed the same RFC 3986 encoding, and kept one encoder in _sarif_uri instead of two. While resolving that I fixed a further regression: raw_file_path was stripped of every leading slash before the unscoped branch ran, so an absolute artifact (/tmp/a b.py) was emitted as relative and disagreed with the notification helper. The leading slash is now stripped only when joining beneath a source identity; unscoped paths keep their own form. Regression tests cover the unscoped absolute path, agreement between finding and notification URIs, and the scoped relative form.

Local: pytest tests/nodes/test_sarif_rules_and_empty_findings.py tests/nodes/test_report.py tests/unit/test_sarif.py 267 passed, 1 skipped. ruff check src/ tests/ and ruff format --check src/ tests/ clean.

Comment thread src/skillspector/nodes/report.py Outdated
occurrence = occurrence or {}
file_path = str(occurrence.get("file", finding.file)).replace("\\", "/").lstrip("/")
raw_file_path = str(occurrence.get("file", finding.file)).replace("\\", "/").lstrip("/")
file_path = quote(raw_file_path, safe="/")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could you apply this encoding to invocation notification locations too? _build_sarif.notification_from_exception still constructs SarifArtifactLocation(uri=path) directly. Scanning a malformed file named broken#old.zip produces error/warning notifications whose URI is still broken#old.zip, so SARIF consumers treat old.zip as a fragment instead of part of the filename. I reproduced this from source and the installed wheel; regular finding URIs are correctly encoded. Please use the same path-normalization/encoding policy for notification locations.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

notification_from_exception now calls the same _sarif_uri helper the artifact locations use, so notification paths get the same policy. broken#old.zip reports as broken%23old.zip.

The helper keeps / and ! literal: ! is an RFC 3986 sub-delim, and this codebase uses !/ as its archive-member separator (outer.zip!/nested.zip), so encoding it to %21 would break the paths the ledger and docs rely on. #, ?, spaces and non-ASCII are all percent-encoded.

Covered by test_report_sarif_encodes_exception_notification_paths and test_sarif_notification_uri_preserves_literal_filename, which exercise both ledger_exceptions and scope_exclusions.

Local: 267 passed, 1 skipped across the SARIF and report test files; ruff check and ruff format --check clean.

Signed-off-by: dajiaohuang <mikewushuwen@outlook.com>
@dajiaohuang

Copy link
Copy Markdown
Author

Addressed both requested paths. _print now honors file= with Rich and the plain fallback; the old error test expects stderr, and the batch tests require JSON and Markdown stdout to remain clean in both formatter branches. SARIF finding and notification artifact locations now share path normalization/percent-encoding, with #, spaces, and backslashes covered.\n\nValidation: 13 focused tests passed on Windows; Ruff lint passed for the changed files. Ruff format reports existing formatting differences elsewhere in contrib/batch_scan/batch_scan.py; I left those unrelated lines unchanged.

Signed-off-by: dajiaohuang <mikewushuwen@outlook.com>
@dajiaohuang

Copy link
Copy Markdown
Author

Updated the nested archive inspection-limit regression to assert the percent-encoded URI in the SARIF notification location, while retaining the raw display-path assertion for terminal, JSON and Markdown. This addresses the sole unit-test failure in run 37672509169.

Validation: 7 relevant report tests passed; Ruff check src/ tests/ passed; formatting check for the changed test passed. The full test suite and live-provider integration tests were not rerun locally. Current-head CI and re-review remain pending.

@rng1995 rng1995 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[SkillSpector Review]

Value and readiness

Both previous review findings are implemented: Rich now honors stderr for diagnostics, notification paths share percent-encoding, and regression coverage includes Rich/plain JSON/Markdown streams and notification locations. Reviewed head 725cc6a20cf743152368354eb8b42628751dc02b.

Material findings

One new location regression remains at src/skillspector/nodes/report.py:206-214. raw_file_path strips every leading slash before the no-source-identity branch calls _sarif_uri. A finding with file='/tmp/a b.py' and no provenance now emits 'tmp/a%20b.py', changing an absolute artifact path into a relative one; previously that branch retained the original path. The notification helper still preserves the slash, so the two locations also disagree. Strip the leading slash only when joining beneath a source identity, and pass the unstripped normalized path through _sarif_uri otherwise. Add an unscoped absolute-path regression.

PIC tradeoffs

Encoded file paths should preserve their reference semantics; source-scoped findings can continue using their existing relative namespace.

Verification and gaps

Read all four changed files, pinned SARIF/batch context, the previous review and both inline threads/replies. The prior Rich-stream and notification findings are resolved in code, though their conversations remain unresolved in the snapshot. All six hosted checks succeeded. The absolute-path consequence above follows directly from the lstrip call and URI branch; no PR code was executed.

Signed-off-by: dajiaohuang <mikewushuwen@outlook.com>
raw_file_path stripped every leading slash before the no-source-identity
branch called _sarif_uri, so a finding for an absolute file was emitted as a
relative URI (tmp/a%20b.py) while the notification helper preserved the slash.
Strip the leading slash only when joining beneath a source identity and pass
the unstripped path through _sarif_uri otherwise. Add regression tests for the
unscoped absolute path, its agreement with notification locations, and the
scoped relative form.

Signed-off-by: dajiaohuang <mikewushuwen@outlook.com>
@dajiaohuang
dajiaohuang force-pushed the fix/sarif-uri-encoding-and-batch-stderr branch from a2ccec0 to 8c1bbdb Compare November 20, 2026 07:10
@dajiaohuang

Copy link
Copy Markdown
Author

Both previous findings implemented; the new location regression you raised is fixed at 8c1bbdbf.

The regression: raw_file_path lstrip. _sarif_artifact_location no longer strips leading separators unconditionally. The lstrip("/") moved inside the if source_identity: branch, so it applies only when the path is being joined beneath a source identity — which is the only case where a relative form is correct. The unscoped branch now passes raw_file_path through unchanged to _sarif_uri. A finding with file='/tmp/a b.py' and no provenance emits /tmp/a%20b.py, not tmp/a%20b.py, and the notification helper agrees because both now share _sarif_uri.

Regression tests added (tests/nodes/test_sarif_rules_and_empty_findings.py):

  • test_sarif_unscoped_uri_keeps_absolute_path_root — parametrized over suppressed False/True, asserts the artifact URI is /tmp/a%20b.py and that unquote(uri) round-trips back to finding.file.
  • test_sarif_unscoped_uri_agrees_with_notification_location — asserts the finding URI and the invocation-notification URI for the same unscoped absolute path are equal.
  • test_sarif_scoped_uri_makes_absolute_path_relative_to_identity — pins that a source-scoped path does drop the root slash, so the two policies stay distinguishable.
  • test_report_sarif_encodes_exception_notification_paths — covers broken#old.zip → broken%23old.zip and the backslash/space form, for the earlier notification-encoding finding.

The earlier two findings also remain covered: Rich and plain branches both route diagnostics to stderr with -f json / -f markdown parsed as clean stdout (test_cli_warns_using_detected_language, test_cli_prints_literal_skill_names_and_errors, now parametrized over rich_available and output_format), and notification locations share the percent-encoding policy.

test-unit is still running on this head, so I am not claiming a green suite — changes, lint, docker-smoke, DCO Check and OpenCode TypeScript Tests have passed, and test-unit is the one to watch for the new SARIF assertions.

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.

SARIF artifact URIs do not encode literal filename characters Batch scanner mixes progress messages into JSON stdout

3 participants