Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
* Trim leading and trailing whitespace from `href` attribute values before resolving them (links found by `GetLink`/`GetLinks`, `Http::crawl()` and the `SimpleWebsitePaginator`, as well as `<base>` and `<link rel="canonical">` hrefs). Browsers strip this whitespace, but the crawler previously resolved an `href` like `"\nhttps://example.com"` as a relative path, producing URLs such as `https://www.example.com/foo/_https://example.com`.

### [3.5.8] - 2026-05-03
### Fixed
Expand Down
4 changes: 3 additions & 1 deletion src/Steps/Dom/HtmlDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public function getBaseHref(): ?string
{
$baseTag = $this->querySelector('base');

return $baseTag?->getAttribute('href');
$href = $baseTag?->getAttribute('href');

return $href === null ? null : trim($href);
}

public function outerHtml(): string
Expand Down
4 changes: 2 additions & 2 deletions src/Steps/Html/GetLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function __construct(string|CssSelector|null $selector = null)

public static function isSpecialNonHttpLink(HtmlElement $linkElement): bool
{
$href = $linkElement->getAttribute('href') ?? '';
$href = trim($linkElement->getAttribute('href') ?? '');

return str_starts_with($href, 'mailto:') ||
str_starts_with($href, 'tel:') ||
Expand Down Expand Up @@ -195,7 +195,7 @@ protected function getLinkUrl(HtmlElement $link): ?Url
}

$linkUrl = $this->handleUrlFragment(
$this->baseUri->resolve($link->getAttribute('href') ?? ''),
$this->baseUri->resolve(trim($link->getAttribute('href') ?? '')),
);

if ($this->matchesAdditionalCriteria($linkUrl)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Steps/Loading/Http/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function canonicalUrl(): string
$canonicalLinkElement = $this->dom->querySelector('link[rel=canonical]');

if ($canonicalLinkElement) {
$canonicalHref = $canonicalLinkElement->getAttribute('href');
$canonicalHref = trim($canonicalLinkElement->getAttribute('href') ?? '');

if ($canonicalHref) {
try {
Expand Down
6 changes: 3 additions & 3 deletions src/Steps/Loading/Http/Paginators/SimpleWebsitePaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ protected function getAbsoluteUrlFromLinkElement(
$baseUrl = $baseUrl->resolve($baseHref);
}

$linkHref = $linkElement->getAttribute('href') ?? '';
$linkHref = trim($linkElement->getAttribute('href') ?? '');

return $baseUrl->resolve($linkHref)->__toString();
}
Expand All @@ -179,9 +179,9 @@ protected function isRelevantLinkElement(Dom\HtmlElement $element): bool
return false;
}

$href = $element->getAttribute('href');
$href = trim($element->getAttribute('href') ?? '');

return !empty($href) && !str_starts_with($href, '#');
return $href !== '' && !str_starts_with($href, '#');
}

protected function addFoundUrl(string $url): void
Expand Down
2 changes: 1 addition & 1 deletion src/Steps/Loading/HttpCrawl.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ protected function getUrlsFromHtmlDocument(Document $document): array
}

try {
$url = $this->handleUrlFragment($document->baseUrl()->resolve($link->getAttribute('href') ?? ''));
$url = $this->handleUrlFragment($document->baseUrl()->resolve(trim($link->getAttribute('href') ?? '')));
} catch (Throwable) {
$this->logger?->warning('Failed to resolve a link with href: ' . $link->getAttribute('href'));

Expand Down
18 changes: 18 additions & 0 deletions tests/Steps/Dom/HtmlDocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,21 @@

expect($anyNodesChecked)->toBeTrue();
});

test('getBaseHref() trims whitespace around the href value', function () {
$html = <<<HTML
<!doctype html>
<html>
<head>
<title>foo</title>
<base href="
/foo " />
</head>
<body>hey</body>
</html>
HTML;

$document = new HtmlDocument($html);

expect($document->getBaseHref())->toBe('/foo');
});
24 changes: 24 additions & 0 deletions tests/Steps/Html/GetLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,3 +410,27 @@

expect($links[0]->get())->toBe('https://www.example.com/foo/bar');
});

it('trims whitespace around the href value before resolving it', function () {
$html = <<<HTML
<!DOCTYPE html>
<html>
<head></head>
<body>
<a href="
https://www.example.com/foo/bar">link</a>
</body>
</html>
HTML;

$step = (new GetLink());

$respondedRequest = new RespondedRequest(
new Request('GET', 'https://www.example.com/home'),
new Response(200, [], $html),
);

$links = helper_invokeStepWithInput($step, $respondedRequest);

expect($links[0]->get())->toBe('https://www.example.com/foo/bar');
});
29 changes: 29 additions & 0 deletions tests/Steps/Html/GetLinksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,32 @@
->and($links[1]->get())->toBe('https://www.example.com/two')
->and($links[2]->get())->toBe('https://www.example.com/three');
});

it('trims whitespace around href values before resolving them', function () {
$html = <<<HTML
<!DOCTYPE html>
<html>
<head></head>
<body>
<a href="
https://www.example.com/one">link one</a>
<a href=" /two ">link two</a>
<a href="
mailto:somebody@example.com">mailto link</a>
</body>
</html>
HTML;

$step = (new GetLinks());

$respondedRequest = new RespondedRequest(
new Request('GET', 'https://www.example.com/home'),
new Response(200, [], $html),
);

$links = helper_invokeStepWithInput($step, $respondedRequest);

expect($links)->toHaveCount(2)
->and($links[0]->get())->toBe('https://www.example.com/one')
->and($links[1]->get())->toBe('https://www.example.com/two');
});
28 changes: 28 additions & 0 deletions tests/Steps/Loading/Http/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,31 @@

expect($document->canonicalUrl())->toBe('https://www.example.com/quz');
});

it('trims whitespace around the base tag href before resolving it as baseUrl()', function () {
$body = '<!doctype html><html><head><title>foo</title><base href="
/baz " /></head><body>hello</body></html>';

$respondedRequest = new RespondedRequest(
new Request('GET', 'https://www.example.com/foo'),
new Response(200, [], $body),
);

$document = new Document($respondedRequest);

expect((string) $document->baseUrl())->toBe('https://www.example.com/baz');
});

it('trims whitespace around the canonical link href before resolving it as canonicalUrl()', function () {
$body = '<!doctype html><html><head><title>foo</title><link rel="canonical" href="
https://www.example.com/quz " /></head><body>hello</body></html>';

$respondedRequest = new RespondedRequest(
new Request('GET', 'https://www.example.com/foo'),
new Response(200, [], $body),
);

$document = new Document($respondedRequest);

expect($document->canonicalUrl())->toBe('https://www.example.com/quz');
});
13 changes: 13 additions & 0 deletions tests/Steps/Loading/Http/Paginators/SimpleWebsitePaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,3 +351,16 @@ public function parentRequests(): array

expect($paginator->hasFinished())->toBeFalse();
});

it('trims whitespace around pagination link href values before resolving them', function () {
$paginator = new SimpleWebsitePaginator('.nextPageLink', 3);

$responseBody = '<a class="nextPageLink" href="
https://www.example.com/listing?page=2 ">Next Page</a>';

$respondedRequest = helper_getRespondedRequestWithResponseBody('/listing?page=1', $responseBody);

$paginator->processLoaded($respondedRequest->request, $respondedRequest);

expect($paginator->getNextRequest()?->getUri()->__toString())->toBe('https://www.example.com/listing?page=2');
});