Skip to content

Commit 244220b

Browse files
committed
fix(training): enforce verified HF dataset privacy before export
1 parent fb1b3d7 commit 244220b

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

‎app/services/hf_dataset_service.py‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,62 @@ def _env_flag(name: str, default: bool) -> bool:
135135
else True
136136
)
137137

138+
# Remote visibility must be read from the Hub itself; the push argument alone
139+
# is not proof of the existing repository's current privacy.
140+
try:
141+
from huggingface_hub import HfApi
142+
except ImportError: # pragma: no cover - covered by fail-closed tests
143+
HfApi = None
144+
145+
146+
def _verify_remote_dataset_privacy() -> bool:
147+
"""Verify actual Hub visibility before reading or writing dataset data.
148+
149+
Public export is allowed only with the exact explicit opt-in
150+
OMNI_HF_DATASET_PRIVATE=false and only for metadata-only exports. Any API
151+
failure or unknown visibility fails closed. Returns the verified private
152+
state, never a guessed/default value.
153+
"""
154+
if HfApi is None:
155+
raise RuntimeError(
156+
"HF export aborted: huggingface_hub privacy verification is unavailable"
157+
)
158+
try:
159+
api = HfApi(token=HF_TOKEN)
160+
info = api.dataset_info(repo_id=HF_DATASET, token=HF_TOKEN)
161+
actual_private = getattr(info, "private", None)
162+
except Exception as exc:
163+
raise RuntimeError(
164+
"HF export aborted: remote dataset privacy could not be verified; "
165+
"no remote write was attempted and staged rows were retained"
166+
) from exc
167+
168+
if not isinstance(actual_private, bool):
169+
raise RuntimeError(
170+
"HF export aborted: remote dataset privacy is unknown; "
171+
"no remote write was attempted and staged rows were retained"
172+
)
173+
174+
if OMNI_HF_DATASET_PRIVATE:
175+
if actual_private is not True:
176+
raise RuntimeError(
177+
"HF export aborted: private dataset required but remote dataset is public; "
178+
"make the dataset private before retrying"
179+
)
180+
else:
181+
if _dataset_private_env is None or _dataset_private_env.strip() != "false":
182+
raise RuntimeError(
183+
"HF export aborted: public visibility requires explicit "
184+
"OMNI_HF_DATASET_PRIVATE=false opt-in"
185+
)
186+
if OMNI_HF_EXPORT_RAW_TEXT:
187+
raise RuntimeError(
188+
"HF export aborted: public datasets are incompatible with "
189+
"OMNI_HF_EXPORT_RAW_TEXT=true"
190+
)
191+
192+
return actual_private
193+
138194
# Staging directory: where pending rows live until they're pushed.
139195
# Default: ``~/.omni/hf_dataset_queue/``. Override with ``$OMNI_HF_QUEUE_DIR``.
140196
_STAGE_DIR = Path(
@@ -568,6 +624,9 @@ def flush_queue() -> str:
568624
# A failed remote read is not evidence that the dataset is empty or absent.
569625
# Fail closed before any write; the operator must make the target dataset
570626
# readable (or explicitly provision it) before export can proceed.
627+
# Verify the actual Hub visibility before any dataset read/write.
628+
_verify_remote_dataset_privacy()
629+
571630
try:
572631
# Capture one exact remote revision, enumerate its data layout, and
573632
# load that exact revision. The eventual write uses this same SHA as

0 commit comments

Comments
 (0)