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
- Validate url is a non-empty string before calling addLinkAttributes.
- Skip link decoration entirely when the annotation has no usable URL.
- 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
- Validate URLs at the annotation/outline parsing boundary.
- Skip decoration for empty URI actions.
- Keep a shared URL-validation helper for link code.
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
- Invalid PDF url data: either string or URL-object is expecte
- BinaryCMapReader.process: Invalid dataSize.
- Page count in top-level pages dictionary is not an integer.
- Invalid type in PageLabel dictionary.
- Invalid style in PageLabel dictionary.
AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13).
Data as JSON: /api/errors/80dbf2145a639c9c.
Report an issue: GitHub.