@@ -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.
379384fn 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\t b.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\t b.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)
0 commit comments