somratpro Claude Haiku 4.5 commited on
Commit
c1db737
Β·
1 Parent(s): e96aeec

fix: hostname allowlist via env var, correct first-boot DB status

Browse files

- Use PAPERCLIP_ALLOWED_HOSTNAMES env var (no CLI/config.json required)
- Set PAPERCLIP_PUBLIC_URL from SPACE_HOST for Better Auth base URL
- Remove broken CLI-based allowed-hostname calls from step 7
- sync_from_hf returns None for no-backup (first boot) vs False for errors
- First boot restore: write db_status=connected, not error

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>

Files changed (2) hide show
  1. paperclip-sync.py +21 -14
  2. start.sh +15 -12
paperclip-sync.py CHANGED
@@ -15,7 +15,7 @@ from datetime import datetime
15
  from pathlib import Path
16
 
17
  from huggingface_hub import HfApi
18
- from huggingface_hub.utils import RepositoryNotFoundError
19
 
20
  # ============================================================================
21
  # Configuration
@@ -341,9 +341,9 @@ def sync_from_hf() -> bool:
341
  local_dir=temp_dir,
342
  local_dir_use_symlinks=False
343
  )
344
- except RepositoryNotFoundError:
345
- logger.info(f'No backup found in {dataset_id}')
346
- return False
347
 
348
  logger.info(f'Downloaded backup from {dataset_id}')
349
 
@@ -446,18 +446,25 @@ def sync_from_backup() -> bool:
446
  try:
447
  success = sync_from_hf()
448
 
449
- # Update status
450
- status['db_status'] = 'connected' if success else 'error'
451
- status['last_error'] = None if success else 'Restore failed'
452
-
453
- write_status(status)
454
-
455
- if success:
 
 
 
 
456
  logger.info('Restore operation completed successfully')
 
457
  else:
458
- logger.warning('Restore operation completed (no backup or error)')
459
-
460
- return success
 
 
461
 
462
  except Exception as e:
463
  logger.error(f'Restore operation failed: {e}')
 
15
  from pathlib import Path
16
 
17
  from huggingface_hub import HfApi
18
+ from huggingface_hub.utils import RepositoryNotFoundError, EntryNotFoundError
19
 
20
  # ============================================================================
21
  # Configuration
 
341
  local_dir=temp_dir,
342
  local_dir_use_symlinks=False
343
  )
344
+ except (RepositoryNotFoundError, EntryNotFoundError):
345
+ logger.info(f'No backup found in {dataset_id} (first boot)')
346
+ return None # not an error β€” just no backup yet
347
 
348
  logger.info(f'Downloaded backup from {dataset_id}')
349
 
 
446
  try:
447
  success = sync_from_hf()
448
 
449
+ if success is None:
450
+ # No backup exists yet (first boot) β€” not an error
451
+ status['db_status'] = 'connected'
452
+ status['last_error'] = None
453
+ write_status(status)
454
+ logger.info('No prior backup found β€” fresh instance, DB ready')
455
+ return True
456
+ elif success:
457
+ status['db_status'] = 'connected'
458
+ status['last_error'] = None
459
+ write_status(status)
460
  logger.info('Restore operation completed successfully')
461
+ return True
462
  else:
463
+ status['db_status'] = 'error'
464
+ status['last_error'] = 'Restore failed'
465
+ write_status(status)
466
+ logger.warning('Restore operation failed')
467
+ return False
468
 
469
  except Exception as e:
470
  logger.error(f'Restore operation failed: {e}')
start.sh CHANGED
@@ -58,6 +58,19 @@ export BACKUP_DATASET_NAME="${BACKUP_DATASET_NAME:-paperclip-backup}"
58
  export PAPERCLIP_TELEMETRY_DISABLED="${PAPERCLIP_TELEMETRY_DISABLED:-1}"
59
  export DO_NOT_TRACK="${DO_NOT_TRACK:-1}"
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  # Auto-generate BETTER_AUTH_SECRET if not provided
62
  # User-set secret (HF Space secret) always takes precedence
63
  AUTH_SECRET_FILE="${PAPERCLIP_HOME}/.auth-secret"
@@ -206,18 +219,8 @@ export DO_NOT_TRACK
206
  export PAPERCLIP_DEPLOYMENT_EXPOSURE="${PAPERCLIP_DEPLOYMENT_EXPOSURE:-private}"
207
  export PAPERCLIP_INSTANCE_ID="${PAPERCLIP_INSTANCE_ID:-default}"
208
  export OPENCODE_ALLOW_ALL_MODELS="${OPENCODE_ALLOW_ALL_MODELS:-true}"
209
-
210
- # Allowlist hostnames Paperclip will accept connections from
211
- echo "Configuring allowed hostnames..."
212
- pnpm paperclipai allowed-hostname localhost 2>/dev/null || true
213
- pnpm paperclipai allowed-hostname 127.0.0.1 2>/dev/null || true
214
- pnpm paperclipai allowed-hostname 0.0.0.0 2>/dev/null || true
215
- # HF Spaces sets SPACE_HOST to the public URL (e.g. somratpro-huggingclip.hf.space)
216
- if [ -n "$SPACE_HOST" ]; then
217
- pnpm paperclipai allowed-hostname "$SPACE_HOST" 2>/dev/null || true
218
- echo "Allowed HF Space host: $SPACE_HOST"
219
- fi
220
- echo -e "${GREEN}βœ“ Hostnames configured${NC}"
221
 
222
  echo -e "${GREEN}βœ“ All systems ready${NC}"
223
  echo -e "${GREEN}═══════════════════════════════════════════${NC}"
 
58
  export PAPERCLIP_TELEMETRY_DISABLED="${PAPERCLIP_TELEMETRY_DISABLED:-1}"
59
  export DO_NOT_TRACK="${DO_NOT_TRACK:-1}"
60
 
61
+ # Derive public URL from HF Space host (auto-set by HF Spaces runtime)
62
+ if [ -z "${PAPERCLIP_PUBLIC_URL}" ] && [ -n "${SPACE_HOST}" ]; then
63
+ export PAPERCLIP_PUBLIC_URL="https://${SPACE_HOST}"
64
+ fi
65
+
66
+ # Allow hostnames via env var (no CLI needed, comma-separated)
67
+ # Includes localhost, 0.0.0.0, and the HF Space public hostname
68
+ _ALLOWED="localhost,127.0.0.1,0.0.0.0"
69
+ if [ -n "${SPACE_HOST}" ]; then
70
+ _ALLOWED="${_ALLOWED},${SPACE_HOST}"
71
+ fi
72
+ export PAPERCLIP_ALLOWED_HOSTNAMES="${PAPERCLIP_ALLOWED_HOSTNAMES:-${_ALLOWED}}"
73
+
74
  # Auto-generate BETTER_AUTH_SECRET if not provided
75
  # User-set secret (HF Space secret) always takes precedence
76
  AUTH_SECRET_FILE="${PAPERCLIP_HOME}/.auth-secret"
 
219
  export PAPERCLIP_DEPLOYMENT_EXPOSURE="${PAPERCLIP_DEPLOYMENT_EXPOSURE:-private}"
220
  export PAPERCLIP_INSTANCE_ID="${PAPERCLIP_INSTANCE_ID:-default}"
221
  export OPENCODE_ALLOW_ALL_MODELS="${OPENCODE_ALLOW_ALL_MODELS:-true}"
222
+ export PAPERCLIP_ALLOWED_HOSTNAMES
223
+ export PAPERCLIP_PUBLIC_URL
 
 
 
 
 
 
 
 
 
 
224
 
225
  echo -e "${GREEN}βœ“ All systems ready${NC}"
226
  echo -e "${GREEN}═══════════════════════════════════════════${NC}"