Skip to content

Commit 00f050c

Browse files
tobocop2Goldziher
authored andcommitted
fix(sitemap): parse every sitemap-index child before it is fetched
A sitemap-index child <loc> that was an absolute same-host address, or any child when the index URL had no usable base, was fetched as raw text and used as its own duplicate key. A child with an upper-case scheme or host, a default port or stray whitespace was fetched unnormalized and could be fetched twice under two spellings. Every child now goes through the same resolver as redirect targets, and the parsed form is both fetched and used as the duplicate key. A child that does not parse is skipped and logged by length only. The index URL is logged redacted when it parses, and by length only when it does not, because the credential redactor returns an address that does not parse unchanged. Closes #226
1 parent 86996dc commit 00f050c

4 files changed

Lines changed: 147 additions & 13 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ All notable changes to crawlberg are documented here.
5252
`%20`, and `127.1` becomes `127.0.0.1`.
5353
(#207)
5454

55+
- **A sitemap-index child `<loc>` was fetched and deduplicated on its raw text instead of its
56+
parsed form.** A same-host absolute child address, and any child address when the sitemap
57+
index's own URL failed to parse, skipped the URL parser entirely, so two spellings of the
58+
same address (a default port, an upper-case scheme, a stray tab) were fetched as two separate
59+
documents. Every child address is now parsed and normalized before it is fetched and before
60+
it is used as the duplicate key, matching the resolver already used for redirect targets, and
61+
a child address that fails to parse is skipped instead of fetched as raw text. (#226)
62+
5563
- **A browser fetch reported no response headers at all on the crawl path.**
5664
`browser_http_to_crawl` built an empty header map, so every header a browser backend had
5765
collected was discarded before the crawl or the escalation path could read it — `ETag`,

‎crates/crawlberg/src/normalize.rs‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,16 +146,21 @@ pub(crate) fn strip_fragment(url: &str) -> String {
146146
}
147147
}
148148

149+
/// Rewrite `url_str` onto `base`'s host when the two differ, keeping only `url_str`'s path
150+
/// and query. Returns the URL parser's normalized form of `url_str` when the host already
151+
/// matches `base`'s, not the raw input: a caller using the return value as a fetch target or
152+
/// a dedup key must not see two spellings of the same address.
149153
pub(crate) fn rewrite_url_host(url_str: &str, base: &Url) -> String {
150-
if let Ok(parsed) = Url::parse(url_str)
151-
&& parsed.host_str() != base.host_str()
152-
{
154+
let Ok(parsed) = Url::parse(url_str) else {
155+
return url_str.to_owned();
156+
};
157+
if parsed.host_str() != base.host_str() {
153158
let mut resolved = base.clone();
154159
resolved.set_path(parsed.path());
155160
resolved.set_query(parsed.query());
156161
return resolved.to_string();
157162
}
158-
url_str.to_owned()
163+
parsed.to_string()
159164
}
160165

161166
/// Resolve a redirect target against `base_url`. `target` may be relative or absolute;
@@ -179,6 +184,27 @@ pub(crate) fn resolve_redirect(base_url: &str, target: &str) -> Option<Url> {
179184
mod tests {
180185
use super::*;
181186

187+
#[test]
188+
fn rewrite_url_host_normalizes_a_same_host_input_instead_of_returning_it_raw() {
189+
let base = Url::parse("https://example.com/index.xml").expect("valid URL");
190+
let rewritten = rewrite_url_host("HTTPS://example.com:443/a\tb", &base);
191+
assert_eq!(
192+
rewritten, "https://example.com/ab",
193+
"a same-host input must come back in the parser's normalized form (lower-case \
194+
scheme, default port dropped, embedded tab stripped), not raw, got {rewritten:?}"
195+
);
196+
}
197+
198+
#[test]
199+
fn rewrite_url_host_still_rewrites_a_different_host_onto_base() {
200+
let base = Url::parse("https://example.com/index.xml").expect("valid URL");
201+
let rewritten = rewrite_url_host("https://other.example/a/b?x=1", &base);
202+
assert_eq!(
203+
rewritten, "https://example.com/a/b?x=1",
204+
"a different-host input must still be rewritten onto base's host, got {rewritten:?}"
205+
);
206+
}
207+
182208
#[test]
183209
fn distinct_urls_with_escaped_and_literal_separators_stay_distinct() {
184210
let escaped = normalize_url("http://example.com/?x=A%26y=B");

‎crates/crawlberg/src/sitemap.rs‎

Lines changed: 101 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -376,20 +376,34 @@ fn document_budget_exhausted(sitemap_url: &str, visited: &std::collections::Hash
376376
/// Resolve one child `<loc>` of a sitemap index against the index's own URL. `None` when
377377
/// `child_url` cannot be resolved against `sitemap_url` at all, which the caller treats the
378378
/// same as a child it could not fetch.
379+
///
380+
/// ~keep Every path parses `child_url` before it is fetched or used as a dedup key. When
381+
/// ~keep `sitemap_url` itself failed to parse (`base` is `None`), `resolve_redirect` still
382+
/// ~keep parses `child_url` on its own and refuses it if that fails too, instead of handing
383+
/// ~keep back unparsed text.
379384
fn resolve_child_sitemap_url(base: Option<&Url>, sitemap_url: &str, child_url: &str) -> Option<String> {
380-
let Some(base_parsed) = base else {
381-
return Some(child_url.to_owned());
382-
};
383-
if Url::parse(child_url).is_ok() {
385+
if let Some(base_parsed) = base
386+
&& Url::parse(child_url).is_ok()
387+
{
384388
return Some(rewrite_url_host(child_url, base_parsed));
385389
}
386390
let resolved = resolve_redirect(sitemap_url, child_url);
387391
if resolved.is_none() {
388-
tracing::debug!(
389-
sitemap_url = %crate::net::redact_url_credentials(sitemap_url),
390-
target_len = child_url.len(),
391-
"sitemap-index child <loc> failed to parse; skipping it"
392-
);
392+
// ~keep `redact_url_credentials` returns an address that does not parse unchanged,
393+
// ~keep so a `sitemap_url` without a `base` is logged by length only.
394+
if base.is_some() {
395+
tracing::debug!(
396+
sitemap_url = %crate::net::redact_url_credentials(sitemap_url),
397+
target_len = child_url.len(),
398+
"sitemap-index child <loc> failed to parse; skipping it"
399+
);
400+
} else {
401+
tracing::debug!(
402+
sitemap_url_len = sitemap_url.len(),
403+
target_len = child_url.len(),
404+
"sitemap-index child <loc> failed to parse; skipping it"
405+
);
406+
}
393407
}
394408
resolved.map(String::from)
395409
}
@@ -589,6 +603,84 @@ mod tests {
589603
assert_logged_without_secret(&fields, "hunter2", sitemap_url);
590604
}
591605

606+
#[test]
607+
fn same_host_child_loc_with_stray_whitespace_normalizes_instead_of_round_tripping_raw() {
608+
let sitemap_url = "https://example.com/sitemap-index.xml";
609+
let base = Url::parse(sitemap_url).expect("valid URL");
610+
611+
let resolved = resolve_child_sitemap_url(Some(&base), sitemap_url, "HTTPS://example.com:443/a\tb.xml");
612+
613+
assert_eq!(
614+
resolved,
615+
Some("https://example.com/ab.xml".to_owned()),
616+
"a same-host child <loc> must be fetched and deduped on its normalized form, not \
617+
the raw text, got {resolved:?}"
618+
);
619+
}
620+
621+
#[test]
622+
fn no_base_child_loc_with_stray_whitespace_normalizes_instead_of_round_tripping_raw() {
623+
// ~keep `sitemap_url` fails to parse, matching how the caller derives `base = None`
624+
// ~keep from `Url::parse(document.url).ok()`.
625+
let resolved = resolve_child_sitemap_url(None, "not a url", "HTTPS://example.com:443/a\tb.xml");
626+
627+
assert_eq!(
628+
resolved,
629+
Some("https://example.com/ab.xml".to_owned()),
630+
"a child <loc> must be parsed and normalized even when the index URL itself has \
631+
no usable base, got {resolved:?}"
632+
);
633+
}
634+
635+
#[test]
636+
#[serial_test::serial(dropped_target_log)]
637+
fn no_base_unparseable_child_loc_is_refused_not_returned_raw() {
638+
let resolved = resolve_child_sitemap_url(None, "not a url", "https://ex ample.com/bad.xml");
639+
640+
assert!(
641+
resolved.is_none(),
642+
"a child <loc> that fails to parse must be refused even when the index URL has no \
643+
usable base, not returned as raw text, got {resolved:?}"
644+
);
645+
}
646+
647+
#[test]
648+
#[serial_test::serial(dropped_target_log)]
649+
fn no_base_unparseable_child_loc_with_credentials_is_never_logged() {
650+
let (resolved, fields) = capture_events(|| {
651+
resolve_child_sitemap_url(None, "not a url", "https://user:hunter2@ex ample.com/bad.xml")
652+
});
653+
654+
assert!(
655+
resolved.is_none(),
656+
"an unparseable child <loc> must not be followed even with no usable base"
657+
);
658+
assert_logged_without_secret(&fields, "hunter2", &"not a url".len().to_string());
659+
}
660+
661+
#[test]
662+
#[serial_test::serial(dropped_target_log)]
663+
fn no_base_index_url_with_credentials_is_never_logged() {
664+
// ~keep The index URL itself fails to parse, so the credential redactor cannot
665+
// ~keep hide its userinfo; the reviewer's own example.
666+
let sitemap_url = "https://user:hunter2@ba d.com/index.xml";
667+
668+
let (resolved, fields) =
669+
capture_events(|| resolve_child_sitemap_url(None, sitemap_url, "https://ex ample.com/bad.xml"));
670+
671+
assert!(
672+
resolved.is_none(),
673+
"an unparseable child <loc> must not be followed even with no usable base"
674+
);
675+
assert_logged_without_secret(&fields, "hunter2", &sitemap_url.len().to_string());
676+
assert!(
677+
fields
678+
.iter()
679+
.any(|(name, value)| name == "sitemap_url_len" && *value == sitemap_url.len().to_string()),
680+
"the index URL must be logged by length, got {fields:?}"
681+
);
682+
}
683+
592684
async fn mount_xml(mock: &MockServer, route: &str, body: String) {
593685
let response = ResponseTemplate::new(200)
594686
.set_body_string(body)

‎docs-site/src/content/docs/changelog.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ title: "Changelog"
5252
`%20`, and `127.1` becomes `127.0.0.1`.
5353
(#207)
5454

55+
- **A sitemap-index child `<loc>` was fetched and deduplicated on its raw text instead of its
56+
parsed form.** A same-host absolute child address, and any child address when the sitemap
57+
index's own URL failed to parse, skipped the URL parser entirely, so two spellings of the
58+
same address (a default port, an upper-case scheme, a stray tab) were fetched as two separate
59+
documents. Every child address is now parsed and normalized before it is fetched and before
60+
it is used as the duplicate key, matching the resolver already used for redirect targets, and
61+
a child address that fails to parse is skipped instead of fetched as raw text. (#226)
62+
5563
- **A browser fetch reported no response headers at all on the crawl path.**
5664
`browser_http_to_crawl` built an empty header map, so every header a browser backend had
5765
collected was discarded before the crawl or the escalation path could read it — `ETag`,

0 commit comments

Comments
 (0)