-
Notifications
You must be signed in to change notification settings - Fork 115
Harden inert snapshots and honor redirect URLs #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SihanTeng
wants to merge
3
commits into
tamnd:main
Choose a base branch
from
SihanTeng:fix/inert-snapshots-redirects
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ import ( | |
| "github.com/tamnd/kage/sanitize" | ||
| "github.com/tamnd/kage/urlx" | ||
| "golang.org/x/net/html" | ||
| "golang.org/x/net/html/atom" | ||
| "golang.org/x/time/rate" | ||
| ) | ||
|
|
||
|
|
@@ -302,6 +303,13 @@ func (c *Cloner) processPage(ctx context.Context, j pageItem) { | |
| return | ||
| } | ||
|
|
||
| // Resolve references against the post-redirect URL (and any <base href>), | ||
| // but keep writing the page under the discovered URL so existing offline | ||
| // links that pointed at /old still resolve. Cross-host redirects leave the | ||
| // resolve base as the final location for relative refs; scope checks still | ||
| // use that absolute URL. | ||
| resolveBase := pageResolveBase(j.u, res.FinalURL, root) | ||
|
|
||
| localFile := urlx.LocalPath(c.seedHost, j.u, urlx.Page, c.cfg.Reserved) | ||
| fileDir := urlx.Dir(localFile) | ||
|
|
||
|
|
@@ -324,7 +332,7 @@ func (c *Cloner) processPage(ctx context.Context, j pageItem) { | |
| } | ||
| } | ||
|
|
||
| asset.RewriteHTML(root, j.u, sink) | ||
| asset.RewriteHTML(root, resolveBase, sink) | ||
| sanitize.CleanTree(root, sanitize.Options{ | ||
| KeepNoscript: c.cfg.KeepNoscript, | ||
| MobileReadable: c.cfg.MobileReadable, | ||
|
|
@@ -354,6 +362,55 @@ func (c *Cloner) waitForCrawlDelay(ctx context.Context) bool { | |
| return c.crawlLimiter.Wait(ctx) == nil | ||
| } | ||
|
|
||
| // pageResolveBase picks the URL against which relative references on a rendered | ||
| // page should resolve. Preference order: | ||
| // 1. A document <base href> (the live page's own base); | ||
| // 2. The browser's final URL after redirects; | ||
| // 3. The URL that was enqueued. | ||
| // | ||
| // The page is still written under the enqueued URL so offline links discovered | ||
| // as /old keep working when the server redirected /old → /new. | ||
| func pageResolveBase(enqueued *url.URL, finalURL string, root *html.Node) *url.URL { | ||
| base := enqueued | ||
| if finalURL != "" { | ||
| if u, err := url.Parse(finalURL); err == nil && u.Scheme != "" && u.Host != "" { | ||
| // Drop fragment; keep query/path as the browser shows them. | ||
| u.Fragment = "" | ||
| base = u | ||
| } | ||
| } | ||
| if href := documentBaseHref(root); href != "" { | ||
| if u, err := urlx.Normalize(base, href); err == nil { | ||
| return u | ||
| } | ||
| } | ||
| return base | ||
| } | ||
|
|
||
| // documentBaseHref returns the first <base href> in document order, or "". | ||
| func documentBaseHref(root *html.Node) string { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This reimplements a first match tree walk that |
||
| var found string | ||
| var walk func(*html.Node) | ||
| walk = func(n *html.Node) { | ||
| if found != "" || n == nil { | ||
| return | ||
| } | ||
| if n.Type == html.ElementNode && n.DataAtom == atom.Base { | ||
| for _, a := range n.Attr { | ||
| if strings.EqualFold(a.Key, "href") && strings.TrimSpace(a.Val) != "" { | ||
| found = strings.TrimSpace(a.Val) | ||
| return | ||
| } | ||
| } | ||
| } | ||
| for c := n.FirstChild; c != nil && found == ""; c = c.NextSibling { | ||
| walk(c) | ||
| } | ||
| } | ||
| walk(root) | ||
| return found | ||
| } | ||
|
|
||
| // processAsset downloads one asset, rewriting CSS references on the way, and | ||
| // writes it to its deterministic local path. | ||
| func (c *Cloner) processAsset(ctx context.Context, j assetItem) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package clone | ||
|
|
||
| import ( | ||
| "net/url" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "golang.org/x/net/html" | ||
| ) | ||
|
|
||
| func TestPageResolveBaseUsesFinalURL(t *testing.T) { | ||
| enqueued, _ := url.Parse("https://ex.com/old") | ||
| root, err := html.Parse(strings.NewReader(`<html><head></head><body><a href="next">n</a></body></html>`)) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| base := pageResolveBase(enqueued, "https://ex.com/new/", root) | ||
| if base.String() != "https://ex.com/new/" { | ||
| t.Fatalf("resolve base = %q, want final URL", base) | ||
| } | ||
| } | ||
|
|
||
| func TestPageResolveBasePrefersDocumentBase(t *testing.T) { | ||
| enqueued, _ := url.Parse("https://ex.com/page") | ||
| root, err := html.Parse(strings.NewReader( | ||
| `<html><head><base href="https://ex.com/dir/"></head><body></body></html>`)) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| base := pageResolveBase(enqueued, "https://ex.com/page", root) | ||
| if base.String() != "https://ex.com/dir/" { | ||
| t.Fatalf("resolve base = %q, want document <base>", base) | ||
| } | ||
| } | ||
|
|
||
| func TestDocumentBaseHref(t *testing.T) { | ||
| root, err := html.Parse(strings.NewReader( | ||
| `<html><head><base href="/subdir/"><base href="https://ignored.example/"></head></html>`)) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if got := documentBaseHref(root); got != "/subdir/" { | ||
| t.Fatalf("documentBaseHref = %q, want first base", got) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the good part of the PR and I want it. Resolving against the post redirect URL fixes a real bug: today a page fetched at
/oldthat redirects to/new/resolveshref="next"as/nextinstead of/new/next, which breaks links and produces 404 asset fetches across the whole mirror.I checked the ordering and it is correct. You read the
<base href>here,RewriteHTMLconsumes it, and only then doesCleanTreedelete the element. Worth keeping that dependency in mind if anyone reordersprocessPagelater.One case to name in the CHANGELOG: on a cross host redirect (
ex.com/oldtoother.com/new) the base becomes the other host, so every relative reference resolves out of scope, gets left absolute, and the page saved underex.com/oldends up with nothing local in it at all. Your comment acknowledges the mechanism but not that outcome. Arguably the right answer is to enqueue the final URL as its own page when it is in scope, but I am happy to leave that for later as long as it is written down.