Skip to content

Pensar - auto fix for 2 issues (CWE-502, ML05, CWE-22)#28

Open
pensarappstaging[bot] wants to merge 1 commit into
mainfrom
pensar-auto-fix-lBwg
Open

Pensar - auto fix for 2 issues (CWE-502, ML05, CWE-22)#28
pensarappstaging[bot] wants to merge 1 commit into
mainfrom
pensar-auto-fix-lBwg

Conversation

@pensarappstaging
Copy link
Copy Markdown

Secured with Pensar

  1. Path Traversal Mitigation:

    • Wrapped all file path constructions (outdir, input/output JSON/embedding/cluster files) in path validation logic.
    • Introduced _is_safe_filename to check for directory traversal attempts, absolute paths, or unsafe characters in file names.
    • Introduced _safe_join, which ensures that constructed paths are strictly confined to specified base directories (data/, embeddings/, etc.), and explicitly raises an error if traversal outside is attempted.
    • Applied sanitization for user-derived file names to prevent directory escape via usernames, replacing / with _.
    • Explicitly check and enforce that outdir is a subdirectory of data/.
  2. Unsafe NumPy Loading Mitigation:

    • On .npy loading for embeddings, changed np.load(embedding_path) to np.load(resolved_embedding_path, allow_pickle=False).
    • This ensures pickled object deserialization is blocked, preventing remote code execution via malicious embeddings.
  3. No impact to application logic:

    • All changes are limited to the areas involving file path usage and NumPy loading, in direct accordance to reported security issues.
    • All original whitespace/formatting is preserved aside from introduced helper functions and extended guards.
More Details
Type Identifier Message Severity Link
Application
CWE-502, ML05
The code blindly loads a .npy file whose path is fully controlled by the caller (embedding_path) using np.load. NumPy’s loader historically allows pickled object dtypes by default, which means a crafted .npy can deserialize arbitrary Python objects and execute code during loading. If an attacker can place or influence that file, this becomes an RCE vector. OWASP-ML highlights untrusted model/data artifact loading as a top risk (ML05). Mitigate by refusing pickle content (allow_pickle=False), validating the file’s provenance, or enforcing cryptographic integrity checks before loading.
high
Link
Application
CWE-22
outdir and user are incorporated directly into filesystem paths without sanitisation. An attacker supplying values such as ../../etc/passwd can leverage path-traversal to read or overwrite arbitrary files, leading to information disclosure or file corruption. Always canonicalise and validate user-controlled path segments, or restrict operations to a fixed, known-safe directory.
medium
Link

1. Unsafe NumPy File Loading Enables Remote Code Execution (CWE-502, ML05)
2. Unsanitized Path Traversal in Social Profile Tweet Storage (CWE-22)
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.

0 participants