santifer/career-ops · error · Error

H1B_API_BASE must not contain a query string or a fragment.

Error message

H1B_API_BASE must not contain a query string or a fragment.

What it means

resolveBase throws this when H1B_API_BASE contains a query string or fragment. Because the plugin appends paths (`${base}/employers/...`), a `?` or `#` in the base swallows the appended path and the request hits the base itself — returning data about a company never asked for.

Solutions

  1. Strip any ?query or #fragment; use the bare origin (https://worker.example.com).
  2. Move authentication to a header/token mechanism rather than the URL query.
  3. If the worker needs a path prefix, set the base to that path without a trailing query.

Example fix

// before
H1B_API_BASE=https://worker.example.com?token=abc
// after
H1B_API_BASE=https://worker.example.com
Defensive patterns

Strategy: validation

Validate before calling

const u = new URL(process.env.H1B_API_BASE);
if (u.search || u.hash) throw new Error('H1B_API_BASE must not contain ?query or #fragment');

Type guard

function isBareBaseUrl(s) {
  try { const u = new URL(s); return !u.search && !u.hash; } catch { return false; }
}

Try / catch

try {
  await lookup(name);
} catch (e) {
  if (e.message.includes('query string or a fragment')) {
    console.error('Strip ?... and #... from H1B_API_BASE; use the bare origin.');
  }
}

Prevention

When it happens

Trigger: H1B_API_BASE set to values like https://worker.example.com/?token=abc or https://worker.example.com#api — the constructed /employers/{id} path lands after '?' or '#'.

Common situations: Embedding an API key in the base URL's query string; copying a full page URL (with fragment) from a browser address bar instead of the bare origin.

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/53415f1ca5a8e428. Report an issue: GitHub.

Appendix: source

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

  if (!trimmed) {
    throw new Error('H1B_API_BASE is set but empty. Unset it to use the default endpoint.');
  }

  let parsed;
  try {
    parsed = new URL(trimmed);
  } catch {
    throw new Error(`H1B_API_BASE is not a valid URL: ${trimmed}`);
  }
  if (parsed.username || parsed.password) {
    // 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;

View on GitHub (pinned to aac998c7ed)