Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions docs/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,10 @@ a local copy, or pass ``cache_dir=...`` to choose a different cache location.
tags=["Undirected"],
)

# Inspect the full catalog metadata for one dataset.
# Inspect the full catalog metadata for one dataset. Not every dataset
# declares a license, so use .get() for optional fields.
info = hgx.get_remote_dataset_info("zoo")
print(info["description"], info["license"])
print(info["description"], info.get("license"))

# Download and cache a dataset file without loading it.
path = hgx.download_remote_dataset("zoo")
Expand Down Expand Up @@ -162,10 +163,16 @@ a local copy, or pass ``cache_dir=...`` to choose a different cache location.

.. note::

The current dataset server may require ``verify_ssl=False`` because of its
certificate chain. This is the default for the remote dataset helpers. If the
server certificate is fixed, pass ``verify_ssl=True`` to enforce normal TLS
verification.
Remote helpers verify TLS certificates by default (``verify_ssl=True``), and
binary ``"hgx"`` files fetched by ``load_hypergraph_from_server`` and
``iter_remote_hypergraphs`` are unpickled with a restricted unpickler that
only accepts plain data. Only pass ``verify_ssl=False`` on a network you
trust.

Dataset names are matched case-insensitively. Datasets you have already
downloaded keep loading from the cache when you are offline, including
through ``iter_remote_hypergraphs(names=...)`` and
``download_remote_datasets([...])``.

Projections and matrices
------------------------
Expand Down
72 changes: 70 additions & 2 deletions hypergraphx/readwrite/io_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,79 @@
from hypergraphx.core.temporal import TemporalHypergraph
from hypergraphx.exceptions import ReadwriteError

# Globals a restricted load may reference: plain containers and scalars, plus
# numpy scalars/arrays (numpy 1.x and 2.x module paths). Hypergraph pickles
# written by save_pickle only need builtins, which reference no globals at all.
_SAFE_PICKLE_GLOBALS = frozenset(
[
("builtins", name)
for name in (
"bool",
"bytearray",
"bytes",
"complex",
"dict",
"float",
"frozenset",
"int",
"list",
"range",
"set",
"slice",
"str",
"tuple",
)
]
+ [
("collections", name)
for name in ("Counter", "OrderedDict", "defaultdict", "deque")
]
+ [
("datetime", name)
for name in ("date", "datetime", "time", "timedelta", "timezone")
]
+ [("numpy", "dtype"), ("numpy", "ndarray")]
+ [
(module, name)
for module in ("numpy.core.multiarray", "numpy._core.multiarray")
for name in ("scalar", "_reconstruct")
]
+ [
(module, "_frombuffer")
for module in ("numpy.core.numeric", "numpy._core.numeric")
]
)

def load_pickle(file_name):

class _RestrictedUnpickler(pickle.Unpickler):
def find_class(self, module, name):
if (module, name) not in _SAFE_PICKLE_GLOBALS:
raise pickle.UnpicklingError(
f"Refusing to load '{module}.{name}' from an untrusted pickle."
)
return super().find_class(module, name)


def load_pickle(file_name, *, restricted=False):
"""
Load a hypergraph saved with :func:`save_pickle`.

Parameters
----------
file_name : str
Path to the pickle file.
restricted : bool, default=False
If True, only plain data types (containers, scalars, numpy arrays) may
be unpickled, so a malicious file cannot execute code. Use this for
files from untrusted sources; remote datasets are always loaded this
way.
"""
try:
with open(file_name, "rb") as f:
data = pickle.load(f)
if restricted:
data = _RestrictedUnpickler(f).load()
else:
data = pickle.load(f)

if not isinstance(data, dict):
raise ValueError("Pickle data is not a dictionary.")
Expand Down
Loading
Loading