mozilla/pdf.js · error · Error

A valid "url" parameter must provided.

Error message

A valid "url" parameter must provided.

What it means

Thrown by PDFLinkService.addLinkAttributes when the url argument is falsy or not a string. The method decorates an <a> element (href, title, target, rel) and must be given a concrete URL string.

Source

Thrown at web/pdf_link_service.js:284

    try {
      return await this.pdfDocument?.getAttachmentContent(id);
    } catch (error) {
      if (!(error instanceof PasswordException)) {
        console.warn(`Unable to load attachment content: ${error}`);
      }
    }
    return null;
  }

  /**
   * Adds various attributes (href, title, target, rel) to hyperlinks.
   * @param {HTMLAnchorElement} link
   * @param {string} url
   * @param {boolean} [newWindow]
   */
  addLinkAttributes(link, url, newWindow = false) {
    if (!url || typeof url !== "string") {
      throw new Error('A valid "url" parameter must provided.');
    }
    const target = newWindow ? LinkTarget.BLANK : this.externalLinkTarget,
      rel = this.externalLinkRel;

    // Strip userinfo (user:password@) from URLs used for display, to prevent
    // phishing via hostname-spoofing (e.g. https://trusted.example@attacker.example/).
    let displayUrl = url;
    const parsedUrl = URL.parse(url);
    if (parsedUrl?.username || parsedUrl?.password) {
      parsedUrl.username = parsedUrl.password = "";
      displayUrl = parsedUrl.href;
    }

    if (this.externalLinkEnabled) {
      link.href = url;
      link.title = displayUrl;
    } else {
      link.href = "";

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Validate url is a non-empty string before calling addLinkAttributes.
  2. Skip link decoration entirely when the annotation has no usable URL.
  3. Sanitize link data from the PDF (e.g. outline items) before forwarding to the link service.

Example fix

// before
linkService.addLinkAttributes(anchor, item.url, newWindow);

// after
if (typeof item.url === 'string' && item.url) {
  linkService.addLinkAttributes(anchor, item.url, newWindow);
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof url === 'string' && url.trim() !== '') {
  linkService.addLinkAttributes(link, url, newWindow);
}

Type guard

function isNonEmptyString(v) {
  return typeof v === 'string' && v.length > 0;
}

Prevention

When it happens

Trigger: Calling addLinkAttributes(link, undefined) or addLinkAttributes(link, 123, true). Also when a link annotation's URL field is empty/null and the outline/link viewer forwards it.

Common situations: PDF outline entries or link annotations with empty URI actions; calling addLinkAttributes before validating the parsed URL.

Related errors


AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13). Data as JSON: /api/errors/80dbf2145a639c9c. Report an issue: GitHub.