Skip to content

Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More

2.3 KB
Raw Blame
import type { SettingsDefaults } from './SettingsDefaultsManager.js';
import { isLoopbackHostname } from './worker-url.js';

/**
 * Validate the configured Cloud Sync hub URL before any token-bearing request.
 * Every sync sink sends the bearer token, so plain HTTP is acceptable only on
 * loopback, and a URL cannot smuggle credentials, a query, or a fragment.
 * `url` is the value requests use (trimmed, trailing slashes stripped, exactly
 * as before validation existed); it is '' when the setting is empty or refused.
 * `refusal` says why a non-empty value was refused, without echoing it.
 */
export function checkCloudSyncHubUrl(raw: unknown): { url: string; refusal: string | null } {
  const value = typeof raw === 'string' ? raw.trim() : '';
  if (value === '') return { url: '', refusal: null };
  let url: URL;
  try {
    url = new URL(value);
  } catch {
    return { url: '', refusal: 'Cloud Sync hub URL refused: not a valid URL' };
  }
  if (url.protocol !== 'https:' && url.protocol !== 'http:') {
    return { url: '', refusal: 'Cloud Sync hub URL refused: only https (or http to this machine) is allowed' };
  }
  if (url.username || url.password || url.search || url.hash) {
    return { url: '', refusal: 'Cloud Sync hub URL refused: it carries credentials, a query or a fragment' };
  }
  if (url.protocol === 'http:' && !isLoopbackHostname(url.hostname)) {
    return { url: '', refusal: 'Cloud Sync hub URL refused: plain http to a non-loopback host' };
  }
  return { url: value.replace(/\/+$/, ''), refusal: null };
}

/** The hub URL requests may use, or '' when it is empty or refused. */
export function normalizeCloudSyncHubUrl(raw: unknown): string {
  return checkCloudSyncHubUrl(raw).url;
}

/**
 * Cloud sync is configured iff token, user id and Hub URL are all non-empty. The
 * one shared predicate (CloudSync, DatabaseManager, the media cloud plane and
 * `doctor`). A configured but refused hub URL keeps queued uploads and sends nothing.
 */
export function isCloudSyncConfigured(settings: Pick<SettingsDefaults, 'CLAUDE_MEM_CLOUD_SYNC_TOKEN' | 'CLAUDE_MEM_CLOUD_SYNC_USER_ID' | 'CLAUDE_MEM_CLOUD_SYNC_HUB_URL'>): boolean {
  return (settings.CLAUDE_MEM_CLOUD_SYNC_TOKEN ?? '') !== ''
    && (settings.CLAUDE_MEM_CLOUD_SYNC_USER_ID ?? '') !== ''
    && (settings.CLAUDE_MEM_CLOUD_SYNC_HUB_URL ?? '').trim() !== '';
}