Skip to content

security: prevent Tar-Slip in uncompress() and unsafe pickle in load_language_model() - #1971

Open
zewolkt3939 wants to merge 1 commit into
hankcs:doc-zhfrom
zewolkt3939:fix/archive-and-pickle
Open

zewolkt3939 wants to merge 1 commit into
hankcs:doc-zhfrom
zewolkt3939:fix/archive-and-pickle

Conversation

@zewolkt3939

Copy link
Copy Markdown

This PR hardens two archive/model loading paths that are reachable from the
public resource-loading API (get_resource / hanlp.load), which downloads and
opens remote (including third-party) resources.

  1. hanlp/utils/io_util.py — uncompress()
    extractall() was called on downloaded TAR archives without validating member
    paths, allowing a crafted archive (members like "../../x" or absolute paths)
    to write files outside the destination directory (Tar-Slip / CWE-22). Added a
    _safe_extractall() helper that rejects members (and symlink/hardlink targets)
    resolving outside dest, then extracts. The zipfile branch already relies on
    CPython's sanitisation but is validated too for consistency.

  2. hanlp/layers/embeddings/contextual_string_embedding.py — load_language_model()
    torch.load(model_file) was called without weights_only=True, so a crafted .pt
    could execute code during unpickling (CWE-502). Now uses weights_only=True
    with a TypeError fallback for very old PyTorch, matching the pattern already
    used in common/torch_component.py and utils/torch_util.py.

No public API changes; both fixes are transparent for legitimate archives/models.
image
image

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.

1 participant