santifer/career-ops · error · Error

H1B_API_BASE must use https, or http on loopback

Error message

H1B_API_BASE must use https, or http on loopback: ${trimmed}

What it means

resolveBase throws this when H1B_API_BASE uses a scheme other than https, except plain http on a loopback host (localhost/127.x/[::1]/::1). Plain http would put the Authorization header on the wire in the clear; non-HTTP schemes (ftp://, ws://) die later inside fetch as an opaque 'fetch failed', which this check replaces with a named error.

Solutions

  1. Serve the worker over https (e.g. put it behind a TLS reverse proxy or Cloudflare) and update the variable.
  2. For local development, use http://localhost:<port> or http://127.0.0.1:<port>, which are exempted.
  3. If testing on a LAN host, use an https tunnel (e.g. a TLS-terminating proxy) instead of plain http.

Example fix

// before
H1B_API_BASE=http://192.168.1.5:8787
// after (local dev)
H1B_API_BASE=http://localhost:8787
// or for a shared host
H1B_API_BASE=https://h1b-worker.example.com
Defensive patterns

Strategy: validation

Validate before calling

const u = new URL(process.env.H1B_API_BASE);
const loopback = /^(localhost|127\.\d+\.\d+\.\d+|\[::1\]|::1)$/i.test(u.hostname);
if (!(u.protocol === 'https:' || (u.protocol === 'http:' && loopback))) {
  throw new Error('H1B_API_BASE must be https, or http on loopback only');
}

Type guard

function isAllowedBase(s) {
  try {
    const u = new URL(s);
    const loopback = /^(localhost|127\.\d+\.\d+\.\d+|\[::1\]|::1)$/i.test(u.hostname);
    return u.protocol === 'https:' || (u.protocol === 'http:' && loopback);
  } catch { return false; }
}

Try / catch

try {
  await lookup(name);
} catch (e) {
  if (e.message.includes('must use https, or http on loopback')) {
    console.error('Serve the worker over TLS, or use http://localhost:<port> for local dev.');
  }
}

Prevention

When it happens

Trigger: H1B_API_BASE set to http://production-host.example.com (non-loopback http), or ftp://localhost / ws://localhost (exempted-by-accident schemes now rejected), or any non-https remote URL.

Common situations: Self-hosting a worker behind plain HTTP on a public host during testing; using the http:// loopback shortcut off localhost (e.g. http://192.168.1.5:8787 on a LAN address); leaving a development URL pointed at a non-HTTP protocol.

Understand the failure class

Background: "Invalid URL" errors: why new URL(), URI.parse, and reqwest::Url reject your string — missing scheme, whitespace, and bad path format — this error's family across 39 libraries.

Related errors


AI-assisted analysis of santifer/career-ops@aac998c7ed (2026-09-16). Data as JSON: /api/errors/0d5883c88037c9bd. Report an issue: GitHub.

Appendix: source

Thrown at plugins/h1b-sponsor/lib/api.mjs:71

    // Undici refuses a credentialed Request anyway, and the value reaches
    // stdout through the source field, so this would print a password.
    throw new Error('H1B_API_BASE must not embed credentials.');
  }
  if (parsed.search || parsed.hash) {
    // Paths are appended, so a query or fragment swallows them: the request
    // would go to the base itself and answer about a company never asked for.
    throw new Error('H1B_API_BASE must not contain a query string or a fragment.');
  }
  // Plain http would put an Authorization header on the wire in the clear.
  // Loopback is exempt so a self-hoster can develop against a local worker,
  // but only for http. Exempting every scheme on a loopback host let
  // ftp://localhost and ws://localhost past validation, and those die later
  // inside fetch as a bare "fetch failed", which is the opaque failure this
  // check exists to replace with a named configuration error.
  const loopback = /^(localhost|127\.\d+\.\d+\.\d+|\[::1\]|::1)$/i.test(parsed.hostname);
  const allowedScheme = parsed.protocol === 'https:' || (parsed.protocol === 'http:' && loopback);
  if (!allowedScheme) {
    throw new Error(`H1B_API_BASE must use https, or http on loopback: ${trimmed}`);
  }
  // Appended as `${base}/employers/...`, so a trailing slash would double up.
  return trimmed.replace(/\/+$/, '');
}
const USER_AGENT = 'career-ops-plugin-h1b-sponsor/1.0';
const DEFAULT_TIMEOUT_MS = 10_000;
const MAX_RETRY_WAIT_MS = 10_000;
const MAX_REDIRECTS = 3;
// The largest real employer profile this API serves is about 13 KB and a search
// page is about 4 KB, so a megabyte is ~80x headroom for future growth while
// still refusing to buffer a runaway or hostile body.
const MAX_READ_BYTES = 1024 * 1024;

function buildHeaders(token) {
  const h = { 'Accept': 'application/json', 'User-Agent': USER_AGENT };
  if (typeof token === 'string' && token.trim()) {
    h['Authorization'] = `Bearer ${token.trim()}`;
  }

View on GitHub (pinned to aac998c7ed)