Safeguard
AI Security•

How do you run a private mirror for Hugging Face models?

Stage vetted models internally, control the cache location, and go offline in production. Here is the setup and what a mirror does not verify.

Safeguard Security Team
4 min read

To serve Hugging Face models from inside your network, vet the files you need, copy them to internal storage, load them from a controlled cache path, and run production with the offline switch on so nothing reaches the public Hub at runtime.

Why mirror models?

A model download is a dependency fetch. Training and inference jobs that pull weights from the internet at start-up inherit every risk of an unpinned dependency: the repository can change, be deleted, or be replaced. Production services also fail when the Hub is slow or unreachable. A controlled copy fixes both.

What settings does the client give you?

The huggingface_hub library documents environment variables that matter here. Two are central. Redirecting the client to an internal server is a separate step, so check the library docs for the current endpoint setting before you rely on one.

  • HF_HUB_CACHE sets where Hub repositories are cached locally. It defaults to a folder under HF_HOME, such as the hub folder in the user's cache directory.
  • HF_HUB_OFFLINE, when set, makes no HTTP calls to the Hub. Only cached files are read, and a missing file raises an error.

Offline mode is the strongest control. A container that starts with it set cannot silently fetch a different revision.

What is a sensible architecture?

Keep it in two stages. First, a staging step in a connected environment downloads a specific revision of a model, inspects it, and copies it to internal storage. Second, production reads only from that storage, through the cache path.

Pin by commit hash, not branch name. A branch like main moves. A commit does not.

What does a mirror not check?

A mirror copies bytes. It does not tell you whether a file is safe. Python's own documentation warns that unpickling untrusted data can execute arbitrary code, and pickle-based model files fall under that warning, which is why many teams prefer the safetensors format and refuse formats that execute code on load. Also check the license, the model card and the repository's recent history before you approve a copy.

A constructed example: a team mirrors a fine-tuned model because its card looks good. They pin the commit, convert nothing, and load it in an isolated worker with no network and no credentials. A load-time surprise then has nowhere to go.

How to check or apply this

  1. Keep an inventory of every model and dataset your code pulls, including defaults buried in library calls.
  2. Choose a specific commit for each and record it.
  3. Download in a staging environment, scan files, and prefer formats that do not execute code on load.
  4. Store approved copies on internal storage with access controls.
  5. Set the cache path in your images, and set the offline switch in production.
  6. Run inference workers without outbound network access or secrets they do not need.
  7. Re-review when you upgrade a model, as you would any dependency.

Safeguard offers AI security capabilities and an MCP Guardrail, and its dependency inventory helps you see which libraries load your models.

Quick check

Does setting the offline variable stop all downloads?

It stops the Hub client's HTTP calls. If a file is not cached, you get an error. Other code in your process can still use the network, so restrict that too.

Why pin a commit instead of a branch?

A branch can move to new content. A commit identifies one exact revision.

Sources

Last reviewed: 18 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.