MCPcopy Create free account
hub / github.com/Graphify-Labs/graphify / check_graph_file_size_cap

Function check_graph_file_size_cap

graphify/security.py:356–382  ·  view source on GitHub ↗

Reject *path* if its size exceeds the configured graph-file cap. Protects callers from memory bombs by failing fast before a multi-GiB graph.json is read into memory and JSON-parsed. Silently returns when ``path.stat()`` cannot be read — the caller's own existence/path check is expe

(path: Path)

Source from the content-addressed store, hash-verified

354
355
356def check_graph_file_size_cap(path: Path) -> None:
357 """Reject *path* if its size exceeds the configured graph-file cap.
358
359 Protects callers from memory bombs by failing fast before a multi-GiB
360 graph.json is read into memory and JSON-parsed. Silently returns when
361 ``path.stat()`` cannot be read — the caller's own existence/path check
362 is expected to surface a clearer error in that case.
363
364 The cap is resolved on every call via :func:`_max_graph_file_bytes`, so the
365 ``GRAPHIFY_MAX_GRAPH_BYTES`` env var can be set before import and still
366 apply.
367
368 Raises:
369 ValueError - file size exceeds the cap. The message includes the
370 observed size, the cap, and how to raise the limit.
371 """
372 cap = _max_graph_file_bytes()
373 try:
374 size = path.stat().st_size
375 except OSError:
376 return
377 if size > cap:
378 raise ValueError(
379 f"graph file {path} is {size:_d} bytes, exceeds {cap:_d}-byte cap\n"
380 f"(set GRAPHIFY_MAX_GRAPH_BYTES=<bytes> or "
381 f"GRAPHIFY_MAX_GRAPH_BYTES=<N>GB to raise the limit)"
382 )
383
384
385# ---------------------------------------------------------------------------

Callers 15

_read_json_fileFunction · 0.90
load_graphFunction · 0.90
run_benchmarkFunction · 0.90
build_mergeFunction · 0.90
_load_graph_jsonFunction · 0.90
write_tree_htmlFunction · 0.90
_rebuild_codeFunction · 0.90
_load_global_graphFunction · 0.90
global_addFunction · 0.90
to_jsonFunction · 0.90

Calls 1

_max_graph_file_bytesFunction · 0.85