grass.script: Validate URL schemes before opening remote URLs - #7715
grass.script: Validate URL schemes before opening remote URLs#7715cwhite911 wants to merge 1 commit into
Conversation
Add grass.script.utils.check_url_scheme(), which rejects URLs whose scheme is not in an allowlist (http/https by default), and call it before urlopen/urlretrieve in the tools that only ever contact a remote service: r.in.wms, v.in.wfs, g.extension, g.extension.all, and g.manual. This prevents opening unexpected schemes such as file: on a user-supplied service URL. g.download.project intentionally accepts a local file path via grass.utils.download, so its scheme is left unrestricted and the two calls are annotated accordingly. Clears 9 Bandit B310 alerts (the calls are marked nosec B310 where the scheme is now validated or fixed).
1740b88 to
61abf1b
Compare
|
Rebased onto current Since this PR was opened, What remains here is the scheme validation for the tools There is a small redundancy worth noting: Verified: bandit 1.9.4 reports 0 B310 on the touched tools, |
Addresses the 9 open Bandit B310 (
urllib_urlopen/ "audit url open for permitted schemes") code scanning alerts.These are low-severity: the URLs come from the user's own arguments, so this is defense-in-depth rather than a fix for a distinct vulnerability. (The related XML entity-expansion issue in the same tools is being handled separately.)
Change
grass.script.utils.check_url_scheme(url, allowed_schemes=("http", "https")), which raisesValueErrorwhen a URL uses an unexpected scheme (e.g.file:).urlopen/urlretrievein the tools that only ever contact a remote service: r.in.wms, v.in.wfs, g.extension, g.extension.all, and g.manual. Ing.extensionthe check goes in the sharedurlopen/urlretrievewrappers, so every remote fetch is covered; itsvalidate_url()already handles local paths before reaching these wrappers, so nothing local is affected.grass.utils.download) documents that its source may be a local file path, so the scheme there is intentionally left unrestricted (restricting it would also be fragile with bare paths and Windows drive letters). Those two calls are annotated instead.Because Bandit flags the
urlopen/urlretrievecall site regardless of a runtime guard, the validated (or fixed-URL) calls are marked# nosec B310.S310is already in the ruff ignore list, so no ruff change is needed.Verification
check_url_schemeacceptshttp/httpsand rejectsfile:,ftp:, andgopher:(unit-checked on Python 3.10 and 3.12).ruff check/ruff formatclean.Written with the assistance of Claude Code.