Skip to content

golink: escape the path remainder appended to non-template links - #269

Open
abhiramshaji-cyber wants to merge 4 commits into
tailscale:mainfrom
abhiramshaji-cyber:golink-escape-path-remainder
Open

abhiramshaji-cyber wants to merge 4 commits into
tailscale:mainfrom
abhiramshaji-cyber:golink-escape-path-remainder

Conversation

@abhiramshaji-cyber

@abhiramshaji-cyber abhiramshaji-cyber commented Oct 27, 2026 •

Copy link
Copy Markdown

Fixes #255

Opening a go link with an encoded % or ? after the link name returned a 500 or redirected to the wrong address; now they stay in the redirect path, and # still starts the fragment.

What this code is for

serveGo handles a request such as http://go/who/100%25. It splits the path into the link name (who) and the rest (100%), loads the link, and calls expandLink to build the redirect target. resolveLink does the same split for a link given as text, such as go/who/foo. For a link without a template, expandLink adds the rest of the path to the long URL.

Before this PR

A person has a link search that points to https://www.google.com/search?q=golink and opens http://go/search/100%25.

  1. serveGo cuts r.URL.Path, which Go has already decoded, so the rest is 100%.
  2. expandLink sees no {{ in the long URL and adds {{with .Path}}/{{.}}{{end}} to the end of the string.
  3. The template writes 100% as is, giving https://www.google.com/search?q=golink/100%.
  4. url.Parse rejects it with invalid URL escape "%".
  5. The person gets a 500. Even without the %, the rest lands inside the q value, not the path.

After this PR

Same person, same request.

  1. serveGo calls splitLinkPath(r.URL.EscapedPath()), which cuts the escaped path at the first / and then decodes each part, so the rest is 100%. resolveLink calls the same function.
  2. expandLink sees no {{ and parses the long URL first.
  3. appendPath adds /100% to u.Path (and to u.RawPath when the long URL has one), so the query q=golink stays where it was.
  4. The person is redirected to https://www.google.com/search/100%25?q=golink.
  5. A # in the rest (from %23) is cut off and set as u.Fragment, so go/doc/page%23anchor still opens .../page#anchor.
  6. A link with a template, such as http://who/{{.Path}}, that writes 100% still fails url.Parse, and now gets a 400 instead of a 500.

The diagram

flowchart LR

classDef box fill:#eef1ff,stroke:#4c5bd4,color:#111827
classDef bad fill:#ffd6d6,stroke:#cc0000,color:#111827,stroke-width:3px
classDef good fill:#c8f0c8,stroke:#008800,color:#111827,stroke-width:3px

subgraph BEFORE["BEFORE: /search/100%25 returns 500"]
direction TB
  B1["open go/search/100%25"] --> B2["serveGo: rest is 100%"]
  B2 --> B3["expandLink"]
  B3 --> B4{"add rest to end of long string"}
  B4 -->|"search?q=golink/100%"| B5["url.Parse error, 500"]
  B4 -->|"rest is extra"| B6["rest still lands in q value"]
end

subgraph AFTER["AFTER: redirect to /search/100%25?q=golink"]
direction TB
  A1["open go/search/100%25"] --> A2["serveGo: splitLinkPath, rest is 100%"]
  A2 --> A3["expandLink parses long URL"]
  A3 --> A4{"THE CHANGE: appendPath adds rest to u.Path"}
  A4 -->|"rest is 100%"| A5["search/100%25?q=golink"]
  A4 -->|"rest is page#anchor"| A6["page#anchor, same as before"]
end

B1 ~~~ A1

class B1,B2,B3,B6 box
class A1,A2,A3,A6 box
class B4,B5 bad
class A4,A5 good

linkStyle 3 stroke:#cc0000,stroke-width:5px
linkStyle 8 stroke:#008800,stroke-width:5px

style BEFORE fill:#fff5f5,stroke:#cc0000,color:#111827
style AFTER fill:#f3fff3,stroke:#008800,color:#111827
Loading

The change

In golink.go:

  • expandLink runs the template only when the long URL has {{, then always calls url.Parse. For a link without a template it then calls appendPath. This is the fix.
  • appendPath cuts the rest at the first #. The part after it, when not empty, replaces u.Fragment. The part before it is added to u.Path, with a / in between unless the escaped path already ends in one, and the escaped form is added to u.RawPath when that is set, so a long URL such as http://host.com/foo%2f/bar keeps its %2f. For a long URL such as mailto:a@b.com, the rest goes onto u.Opaque instead.
  • splitLinkPath is the one place that cuts a path into link name and rest, used by both serveGo and resolveLink.
  • serveGo returns 400 when the error from expandLink is a *url.Error, and still returns 401 for errNoUser.

golink_test.go adds rows to TestExpandLink (%, ?, #, a lone #, a trailing #, spaces, unicode, a trailing slash, long URLs with a query and a fragment, a long URL with RawPath, and templates with 100%), to TestServeGo (%25, %3F, %23, unicode, a long URL with a query, and a templated link that now gets 400), and to TestResolveLink (%25 and %23).

What changes for a user

  • go/who/100%25 redirects to http://who/100%25 instead of returning a 500.
  • go/who/a%3Fb keeps ? in the path instead of starting a query.
  • go/who/a%23b still redirects to http://who/a#b, so links to a heading on a page keep working.
  • A long URL with a query or fragment keeps it after the new path, instead of the rest being added inside it.
  • A templated link that builds an invalid URL gets a 400 with the parse error, not a 500.
  • Plain paths such as go/who/a/b, request query strings, and template functions such as {{PathEscape .Path}} behave as before. A %2F in the rest is still decoded to /, as before. A %2F in the link name is no longer treated as a /, so go/who%2Fextra is a 404 where main redirected it.

serveGo passes the decoded request path remainder to expandLink, which
appended it to the long URL and parsed the result. A remainder such as
"100%" (from /who/100%25) made url.Parse fail and the request returned a
500, and a remainder containing "?" or "#" moved text out of the path
and into the query or fragment of the redirect target.

For links without a template, escape the remainder as a URL path before
appending it, so characters such as "%", "?" and "#" stay part of the
path. Templates still receive the decoded value in .Path.

Fixes tailscale#255

Signed-off-by: abhiramshaji-cyber <write4abhiram@gmail.com>
@abhiramshaji-cyber

Copy link
Copy Markdown
Author

Could a maintainer approve the workflow runs? The tests, Nix build, Docker and govulncheck jobs are waiting on approval before they start; this fixes #255.

@abhiramshaji-cyber

Copy link
Copy Markdown
Author

Hi @mikeodr, hope you're doing well! Hey, it's me again with this one; I can see you've been busy keeping the deps and govulncheck in good shape, so no rush at all. The workflow runs here are still waiting on approval, and I'm happy to rebase onto main so it picks up today's govulncheck fix, or reshape the change if you'd like it done differently. Out of curiosity, now that golink can be imported as a library (#252), are you seeing people embed it in their own tools, or is the standalone server still how most folks run it? Any tips for contributing here would be really appreciated too.

@mikeodr

mikeodr commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

If you could rebase that would be swell I can give this a review.

I don't have any data on your embed vs not unfortunately.

…remainder

Signed-off-by: abhiramshaji-cyber <write4abhiram@gmail.com>
@abhiramshaji-cyber

Copy link
Copy Markdown
Author

Hey @mikeodr, thank you so much, that's really kind! I merged main into the branch in 44ff13a, so it's fully up to date now, including the govulncheck fix from #273. If you'd rather have it as a single commit rebased onto main to keep the history linear, just say the word and I'll do that instead. And no worries at all on the embed question, I was just curious. Any feedback or tips on the change itself are very welcome!

@mikeodr mikeodr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, it's nearly there but I think a few small issues remain.

Comment thread golink.go Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

concern(blocking): This is now out of sync with the new change. Can you unifi it with the new logic?

I believe also L1457 has the same concern.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hope you're doing well, and thanks for catching this, you're right that both spots were still cutting the decoded path. In 4155412 serveGo and resolveLink now both go through one small splitLinkPath, which cuts the escaped path at the first / and then decodes the name and the rest.

One side effect I wanted to check with you: since the split happens on the escaped path, /who%2Fextra is now a 404 where main redirected it to who with extra. I added a test for it in TestServeGo. Does that seem right to you, or would you rather keep the old behaviour there? Happy to change it either way.

Comment thread golink.go Outdated
return buf.String(), nil
}

func appendPath(long, path string) string {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: I believe appendPath ignores query and fragment in the long URL. With link g pointing to https://www.google.com/search?q=golink, request /g/foo%25 yields ...search?q=golink/foo%25, placing the remainder in the query value. Appending to the parsed URL's Path should fix this, and expandLink parses the result on the very next line anyway.

Can you confirm and add appropriate tests?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot, confirmed, you were right. With https://www.google.com/search?q=golink the rest was landing inside the q value. In 4155412 expandLink parses the long URL first and appendPath adds the rest to u.Path (and u.RawPath when it's set), so /g/foo%25 now goes to https://www.google.com/search/foo%25?q=golink, and a fragment on the long URL stays at the end too.

Tests are in TestExpandLink (query, query plus fragment plus request query, a long URL with %2f) and in TestServeGo for the google style link. 6857f84 also fixes two cases I found while testing this: a long URL ending in %2F, and opaque ones like mailto:. Let me know if I missed a case you had in mind.

Comment thread golink.go Outdated
if !strings.Contains(long, "{{") {
return appendPath(long, env.Path), nil
}
tmpl, err := texttemplate.New("").Funcs(expandFuncMap).Parse(long)

@mikeodr mikeodr Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

concern: Templated links still 500 on the issue #255 input. A link using {{.Path}} explicitly writes http://who/100% unescaped, url.Parse fails, and serveGo returns 500. The implicit default and the explicit template used to behave identically and now diverge. I think this should at minimum be a 4xx.

Please confirm and add tests as appropriate.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thank you, confirmed. A link like http://who/{{.Path}} with /tmpl/100%25 still fails url.Parse, since the template writes the decoded 100%. In 4155412 serveGo now returns 400 when the error from expandLink is a *url.Error, and keeps 401 for errNoUser.

I added a TestServeGo case for a templated link (302 for a plain path, 400 for %25), plus TestExpandLink cases showing {{PathEscape .Path}} handles 100% fine. One thing I'm not sure about: a saved link without a template whose long URL itself won't parse would also get 400 now instead of 500. Would you prefer I keep that one as 500? Happy to adjust.

Comment thread golink.go Outdated
if !strings.HasSuffix(long, "/") {
long += "/"
}
return long + (&url.URL{Path: path}).EscapedPath()

@mikeodr mikeodr Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

concern(blocking): I think after running some tests there's a behaviour change.

# now encodes as %23, which changes go/doc-with-anchor used to be http://docs-server/something#anchor and is now http://docs-server/something%23anchor, which may or may not breaking jump to header anchor points on a page.

Please test and confirm.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for testing this, you're totally right, that was a real behaviour change and I should have caught it. In 4155412 a # in the rest starts the fragment again: appendPath cuts the rest at the first # and sets u.Fragment from the part after it, so go/doc/something%23anchor goes to http://docs-server/something#anchor like before. A fragment already on the long URL is kept unless the rest brings its own.

Tests cover a#b, a lone #b, a trailing #, and a long URL with its own fragment in TestExpandLink, plus %23 in TestServeGo and TestResolveLink. Does that match what you saw?

Comment thread golink.go Outdated
Comment on lines 995 to 999
@@ -998,24 +998,12 @@ func regexMatch(pattern string, s string) bool {
// If long does not include templates, the default behavior is to append
// env.Path to long.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chore: I don't think this matches the new behaviour, can you ensure it is up to date?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, good point, it was out of date. In 4155412 the expandLink comment now says templates run with env, and otherwise the path is escaped onto the long URL's path with a # starting the fragment. Happy to reword it if you'd like it shorter or closer to the old style.

serveGo and resolveLink now split the escaped request path with one
shared helper, so the short name and remainder come from the same
escaping rules.

For links without a template, the remainder is appended to the parsed
long URL's Path (and RawPath when set), so a long URL with a query or
fragment keeps them after the new path. A "#" in the remainder starts
the fragment of the redirect target, as it did before.

A templated link whose output is not a valid URL now returns 400
instead of 500.

Updates tailscale#255

Signed-off-by: Abhiram Shaji <write4abhiram@gmail.com>
Check for a trailing slash on the escaped path, so a long URL ending in
%2F still gets a "/" before the remainder, and append the remainder to
Opaque for long URLs such as mailto:, where String ignores Path.

Signed-off-by: Abhiram Shaji <write4abhiram@gmail.com>
@abhiramshaji-cyber

abhiramshaji-cyber commented Nov 13, 2026 •

Copy link
Copy Markdown
Author

Hi @mikeodr, hope you're doing well! Thank you so much for the careful review and for running it locally, the # anchor and the query string cases were great catches. I've pushed 4155412 and 6857f84 and replied in each thread, and left a couple of questions there where I wasn't sure which way you'd prefer. Whenever you have time, would you mind taking another look? Any feedback or tips are very welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Links with a percent-encoded path remainder return 500

2 participants