Skip to content

    Type a page, a command such as keryn scan, or a rule id.

    model-loading-v1

    View as Markdown

    14 rules · Keryn Software Licence · rules 0.8.0 · all packs

    How to read a rule
    Severity
    How bad the finding is if it is real: error fails a default gate, warning and info do not.
    Confidence
    P0 exploitable as written; P1 dangerous in context; P2 a heuristic or a posture check: confirm by reading the code.
    False-positive risk
    low: an exact API or flag, so hits are almost always real; medium: the pattern is legitimate in many programs, so check each hit; high: it finds a lead to confirm, so expect benign hits.
    Edge
    The relationship the finding sits on. reaches: content or traffic gets to a thing; calls: one thing invokes another; reads: one thing reads from another; writes: one thing writes to another; sends: data leaves for another party; grants: authority is given or left unchecked; depends_on: something relies on a component it does not verify; trusts: one party believes another without evidence. The boundary it crosses, when there is one: user->prompt (untrusted input reaching a prompt); model->code (model output reaching code, a shell, a query or a page); secret->outside (a secret leaving the trusted side); supply-chain (code, models or prompts pulled in from outside).
    Taxonomy chips
    References for readers who work in OWASP LLM 2025, OWASP Agentic, CWE or NIST AI RMF. They say where a rule belongs, not that a framework is satisfied.

    keryn.modelload.keras-load-model-safe-mode-disabled-py

    Section titled “keryn.modelload.keras-load-model-safe-mode-disabled-py”

    error python keras, tensorflow

    Keras load_model is called with safe_mode=False on a path that is not a literal. In the .keras format a Lambda layer is stored as Python bytecode and safe_mode is the only switch that stops it from being rebuilt, so a model file from outside your build runs code the moment it loads; keep safe_mode at its default and load only files you produced.

    Confidence P0: exploitable as written. False-positive risk low: an exact API or flag, so hits are almost always real.

    Edge inspected: depends_on something relies on a component it does not verify; it crosses the supply-chain boundary (code, models or prompts pulled in from outside).

    CWE-502 LLM03 ASI04

    Fix: Remove safe_mode=False. If the model genuinely needs custom layers, register them with keras.saving.register_keras_serializable and pass custom_objects, or export weights only (model.save_weights / load_weights) and rebuild the architecture in code. Verify a checksum or signature of the file before loading.

    References: https://keras.io/api/models/model_saving_apis/model_saving_and_loading/

    Try it: a snippet that triggers this rule

    Written for this page, not taken from any project. Save it as ml/predict.py and scan it with keryn scan ml --verbose; the finding below appears.

    ml/predict.py
    import tensorflow as tf
    def load(path):
    model = tf.keras.models.load_model(path, safe_mode=False)
    model.summary()
    return model

    Checked with Keryn’s browser engine when this page was built: it reports keryn.modelload.keras-load-model-safe-mode-disabled-py at line 5 (error, proof 1). The rule’s own logic is not published, so it cannot run in this tab; the pack list in the playground shows what does.

    keryn.modelload.numpy-load-allow-pickle-py

    Section titled “keryn.modelload.numpy-load-allow-pickle-py”

    error python numpy

    numpy.load is called with allow_pickle=True on a path that is not a literal. NumPy stores object arrays with pickle and documents that this is not secure against maliciously constructed data, so a swapped or downloaded .npy/.npz file can execute code at load time; keep allow_pickle=False and store plain numeric arrays (or safetensors).

    Confidence P0: exploitable as written. False-positive risk low: an exact API or flag, so hits are almost always real.

    Edge inspected: depends_on something relies on a component it does not verify; it crosses the supply-chain boundary (code, models or prompts pulled in from outside).

    CWE-502 LLM03 ASI04

    Fix: Drop allow_pickle=True and re-save the data as a numeric array, .npz of numeric arrays or safetensors. If object arrays are unavoidable, load only files from a location your pipeline writes to and verify a hash first.

    References: https://numpy.org/doc/stable/reference/generated/numpy.load.html

    Try it: a snippet that triggers this rule

    Written for this page, not taken from any project. Save it as ml/embeddings.py and scan it with keryn scan ml --verbose; the finding below appears.

    ml/embeddings.py
    import numpy as np
    def load_embeddings(path):
    data = np.load(path, allow_pickle=True)
    return data["vectors"], data["labels"]

    Checked with Keryn’s browser engine when this page was built: it reports keryn.modelload.numpy-load-allow-pickle-py at line 5 (error, proof 1). The rule’s own logic is not published, so it cannot run in this tab; the pack list in the playground shows what does.

    error python pandas

    pandas.read_pickle is called on a path that is not a literal. The pandas docs warn that loading pickled data from untrusted sources is unsafe, and the call also accepts URLs, so an attacker-supplied or downloaded file executes code during unpickling; use Parquet, Feather or CSV for exchanged data.

    Confidence P1: dangerous in context. False-positive risk medium: the pattern is legitimate in many programs, so check each hit.

    Edge inspected: depends_on something relies on a component it does not verify; it crosses the supply-chain boundary (code, models or prompts pulled in from outside).

    CWE-502 LLM03 ASI04

    Fix: Exchange tabular artefacts as Parquet or Feather. Keep read_pickle for files your own job wrote into a private location, and check a hash of the file before reading.

    References: https://pandas.pydata.org/docs/reference/api/pandas.read_pickle.html

    keryn.modelload.cloudpickle-or-dill-loads-py

    Section titled “keryn.modelload.cloudpickle-or-dill-loads-py”

    error python cloudpickle, dill

    cloudpickle.load/loads or dill.loads deserialises a value that is not a literal. Both extend pickle (they even serialise functions and lambdas by value), so whoever controls the bytes controls code execution on this host; load a data-only format such as JSON or safetensors instead.

    Confidence P0: exploitable as written. False-positive risk low: an exact API or flag, so hits are almost always real.

    Edge inspected: depends_on something relies on a component it does not verify; it crosses the supply-chain boundary (code, models or prompts pulled in from outside).

    CWE-502 LLM03 ASI04

    Fix: Replace the pickle-family call with a data-only format. If code objects must travel (distributed workers), keep the channel inside an authenticated, private network and sign the payload with an HMAC verified before loads.

    References: https://github.com/cloudpipe/cloudpickle · https://docs.python.org/3/library/pickle.html · https://raw.githubusercontent.com/cloudpipe/cloudpickle/master/cloudpickle/cloudpickle.py

    Try it: a snippet that triggers this rule

    Written for this page, not taken from any project. Save it as worker/tasks.py and scan it with keryn scan worker --verbose; the finding below appears.

    worker/tasks.py
    import cloudpickle
    import redis
    r = redis.Redis(host="cache.internal.example")
    def run_next():
    raw = r.lpop("jobs")
    if raw is None:
    return None
    fn, args = cloudpickle.loads(raw)
    return fn(*args)

    Checked with Keryn’s browser engine when this page was built: it reports keryn.modelload.cloudpickle-or-dill-loads-py at line 11 (error, proof 1). The rule’s own logic is not published, so it cannot run in this tab; the pack list in the playground shows what does.

    keryn.modelload.skops-load-trust-everything-py

    Section titled “keryn.modelload.skops-load-trust-everything-py”

    warning python skops, scikit-learn

    skops.io.load is given the list returned by get_untrusted_types as its trusted argument. That list is what the file asks to be allowed, so passing it straight back turns the skops allow-list into a rubber stamp and re-opens the arbitrary-type loading skops exists to prevent; print or review the types and pass an explicit, hard-coded list.

    Confidence P1: dangerous in context. False-positive risk low: an exact API or flag, so hits are almost always real.

    Edge inspected: depends_on something relies on a component it does not verify; it crosses the supply-chain boundary (code, models or prompts pulled in from outside).

    CWE-502 LLM03 ASI04

    Fix: Call get_untrusted_types in a review step, compare the result with a short list you have approved (for example [“xgboost.sklearn.XGBClassifier”]) and pass only that list as trusted.

    References: https://skops.readthedocs.io/en/stable/persistence.html

    error python pytorch

    torch.package.PackageImporter opens a package whose path is not a literal. The torch.package docs state the format relies on pickle and say to unpackage only data you trust, so an attacker-supplied .pt/.zip package runs code when its pickled objects are loaded; restrict the path to files your build produced and verify them first.

    Confidence P1: dangerous in context. False-positive risk low: an exact API or flag, so hits are almost always real.

    Edge inspected: depends_on something relies on a component it does not verify; it crosses the supply-chain boundary (code, models or prompts pulled in from outside).

    CWE-502 LLM03 ASI04

    Fix: Resolve the package from a fixed directory, check its SHA-256 against a pinned value before opening it, and pass module_allowed to restrict which external modules it may import. Prefer exporting plain weights (safetensors) when code need not travel with the model.

    References: https://docs.pytorch.org/docs/2.14/package.html

    keryn.modelload.native-extension-library-load-py

    Section titled “keryn.modelload.native-extension-library-load-py”

    error python pytorch, tensorflow

    A native shared library is loaded into the process from a path that is not a literal (torch.ops.load_library, torch.classes.load_library or tf.load_op_library). Loading registers custom ops by running the library’s initialisers, so a swapped .so/.dll is native code execution with the service’s privileges; load only libraries shipped in your image from fixed paths.

    Confidence P0: exploitable as written. False-positive risk low: an exact API or flag, so hits are almost always real.

    Edge inspected: depends_on something relies on a component it does not verify.

    CWE-427 LLM03 ASI05

    Fix: Ship the custom-op library inside the image, reference it by a constant absolute path, and never build the path from request fields, model metadata or downloaded manifests.

    References: https://www.tensorflow.org/api_docs/python/tf/load_op_library · https://docs.pytorch.org/docs/stable/torch.html · https://raw.githubusercontent.com/pytorch/pytorch/main/torch/_ops.py · https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/python/framework/load_library.py

    keryn.modelload.torch-hub-load-state-dict-nonliteral-url-py

    Section titled “keryn.modelload.torch-hub-load-state-dict-nonliteral-url-py”

    error python pytorch

    torch.hub.load_state_dict_from_url downloads from a URL that is not a literal and does not set weights_only=True (its default is False). The checkpoint is unpickled, so whoever influences the URL gets code execution; pass weights_only=True and download only from pinned, trusted locations.

    Confidence P0: exploitable as written. False-positive risk low: an exact API or flag, so hits are almost always real.

    Edge inspected: depends_on something relies on a component it does not verify; it crosses the supply-chain boundary (code, models or prompts pulled in from outside).

    CWE-502 LLM03 ASI04

    Fix: Set weights_only=True and check_hash=True, keep the URL in server-side configuration, and prefer safetensors checkpoints.

    References: https://docs.pytorch.org/docs/2.14/hub.html

    Try it: a snippet that triggers this rule

    Written for this page, not taken from any project. Save it as ml/weights.py and scan it with keryn scan ml --verbose; the finding below appears.

    ml/weights.py
    import torch
    def fetch_backbone(cfg):
    url = cfg["backbone_url"]
    return torch.hub.load_state_dict_from_url(url, map_location="cpu")

    Checked with Keryn’s browser engine when this page was built: it reports keryn.modelload.torch-hub-load-state-dict-nonliteral-url-py at line 6 (error, proof 1). The rule’s own logic is not published, so it cannot run in this tab; the pack list in the playground shows what does.

    keryn.modelload.torch-hub-load-state-dict-literal-url-unhardened-py

    Section titled “keryn.modelload.torch-hub-load-state-dict-literal-url-unhardened-py”

    warning python pytorch

    torch.hub.load_state_dict_from_url fetches a fixed URL without weights_only=True and without check_hash=True. The checkpoint is unpickled and nothing ties the bytes to the release you reviewed, so a compromised host or a hijacked redirect yields code execution; set weights_only=True and name the file with its hash prefix.

    Confidence P1: dangerous in context. False-positive risk medium: the pattern is legitimate in many programs, so check each hit.

    Edge inspected: depends_on something relies on a component it does not verify; it crosses the supply-chain boundary (code, models or prompts pulled in from outside).

    CWE-494 LLM03 ASI04

    Fix: Add weights_only=True and check_hash=True (with a file name that carries the first characters of the SHA-256), or vendor the checkpoint into your artefact store.

    References: https://docs.pytorch.org/docs/2.14/hub.html

    keryn.modelload.torch-hub-load-trust-repo-nonliteral-py

    Section titled “keryn.modelload.torch-hub-load-trust-repo-nonliteral-py”

    error python pytorch

    torch.hub.load is called with trust_repo=True on a repository string that is not a literal. trust_repo=True skips the interactive confirmation and the repo’s hubconf.py is then imported and executed, so a request- or config-derived repo name runs arbitrary code from any GitHub repo; use a fixed owner/repo:ref and a reviewed commit.

    Confidence P0: exploitable as written. False-positive risk low: an exact API or flag, so hits are almost always real.

    Edge inspected: depends_on something relies on a component it does not verify; it crosses the supply-chain boundary (code, models or prompts pulled in from outside).

    CWE-829 LLM03 ASI04

    Fix: Hard-code the repository with a tag or commit (“owner/repo:<sha>”), keep trust_repo at its default, or vendor the entry point and use source=“local”.

    References: https://docs.pytorch.org/docs/2.14/hub.html

    keryn.modelload.torch-jit-load-nonliteral-path-py

    Section titled “keryn.modelload.torch-jit-load-nonliteral-path-py”

    warning python pytorch

    torch.jit.load opens a TorchScript archive from a path that is not a literal. A TorchScript archive carries serialised code and pickled constants, so a file you did not build can run attacker-chosen operations when it executes; load archives only from a fixed, access-controlled location after a hash check.

    Confidence P2: a heuristic or a posture check: confirm by reading the code. False-positive risk high: it finds a lead to confirm, so expect benign hits.

    Edge inspected: depends_on something relies on a component it does not verify; it crosses the supply-chain boundary (code, models or prompts pulled in from outside).

    CWE-502 LLM03 ASI04

    Fix: Resolve the file inside a fixed model directory (reject separators and .. in names), verify its SHA-256 against a pinned value, and prefer exporting weights as safetensors.

    References: https://docs.pytorch.org/docs/stable/jit.html · https://raw.githubusercontent.com/pytorch/pytorch/main/torch/jit/_serialization.py

    keryn.modelload.yaml-load-unsafe-loader-py

    Section titled “keryn.modelload.yaml-load-unsafe-loader-py”

    error python pyyaml

    yaml.load uses the full Loader or UnsafeLoader (or yaml.unsafe_load is called) on a value that is not a literal. PyYAML documents that these loaders can construct arbitrary Python objects and are as powerful as pickle.load, so a model or pipeline config with a !!python/object tag executes code; use yaml.safe_load.

    Confidence P0: exploitable as written. False-positive risk low: an exact API or flag, so hits are almost always real.

    Edge inspected: depends_on something relies on a component it does not verify; it crosses the supply-chain boundary (code, models or prompts pulled in from outside).

    CWE-502 LLM03 ASI04

    Fix: Replace the call with yaml.safe_load (or Loader=yaml.SafeLoader) and validate the resulting dict with pydantic or a schema before use.

    References: https://pyyaml.org/wiki/PyYAMLDocumentation · https://raw.githubusercontent.com/yaml/pyyaml/main/lib/yaml/loader.py

    Try it: a snippet that triggers this rule

    Written for this page, not taken from any project. Save it as ml/config.py and scan it with keryn scan ml --verbose; the finding below appears.

    ml/config.py
    import yaml
    def read_config(path):
    with open(path) as fh:
    return yaml.load(fh, Loader=yaml.FullLoader)

    Checked with Keryn’s browser engine when this page was built: it reports keryn.modelload.yaml-load-unsafe-loader-py at line 6 (error, proof 1). The rule’s own logic is not published, so it cannot run in this tab; the pack list in the playground shows what does.

    keryn.modelload.hf-hub-download-result-unpickled-py

    Section titled “keryn.modelload.hf-hub-download-result-unpickled-py”

    error python huggingface, pytorch, joblib

    A file fetched with hf_hub_download from a repo id that is not a literal is passed straight to a pickle-backed loader. Any public Hub repo can host a malicious checkpoint, and torch.load, joblib.load, pickle.load or numpy allow_pickle execute it on load; resolve repo ids through an allow-list and load safetensors instead.

    Confidence P0: exploitable as written. False-positive risk low: an exact API or flag, so hits are almost always real.

    Edge inspected: depends_on something relies on a component it does not verify; it crosses the supply-chain boundary (code, models or prompts pulled in from outside).

    CWE-502 LLM03 ASI04

    Fix: Map the requested model to a repo id and commit hash held in configuration, download a .safetensors file, and load it with safetensors.torch.load_file.

    References: https://huggingface.co/docs/huggingface_hub/package_reference/file_download · https://huggingface.co/docs/hub/security-pickle · https://raw.githubusercontent.com/cloudpipe/cloudpickle/master/cloudpickle/cloudpickle.py

    keryn.modelload.request-path-to-model-loader-python

    Section titled “keryn.modelload.request-path-to-model-loader-python”

    error python data-flow rule flask, fastapi, pytorch, joblib, numpy, keras

    A request parameter or route argument becomes the path of a model loader (torch.load, joblib.load, numpy.load, pickle.load(open(…)), pandas.read_pickle or keras load_model). Pickle-backed loaders execute code and every loader reads whatever file the caller names, so a caller can point it at an uploaded or attacker-staged artefact; map the request value to a fixed file through an allow-list.

    Confidence P1: dangerous in context. False-positive risk low: an exact API or flag, so hits are almost always real.

    Edge inspected: depends_on something relies on a component it does not verify; it crosses the supply-chain boundary (code, models or prompts pulled in from outside).

    CWE-22 CWE-502 LLM03 ASI04

    Fix: Accept a model name, look it up in a dict or enum of server-side paths (if name not in MODELS: abort) and load the mapped file. If a file name must pass through, reduce it with os.path.basename or werkzeug’s secure_filename, join it to a fixed directory, and still prefer safetensors or weights_only=True.

    References: https://flask.palletsprojects.com/en/stable/api/#flask.Request · https://fastapi.tiangolo.com/tutorial/path-params/ · https://numpy.org/doc/stable/reference/generated/numpy.load.html · https://keras.io/api/models/model_saving_apis/model_saving_and_loading/