Skip to content

feat(api): magic token authentication from syncthing browse - #10810

Merged
calmh merged 6 commits into
syncthing:mainfrom
tbodt:push-sxypwqolnszv
Oct 27, 2026
Merged

calmh merged 6 commits into
syncthing:mainfrom
tbodt:push-sxypwqolnszv

Conversation

@tbodt

@tbodt tbodt commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Purpose

Syncthing's authentication options have seemed sort of lacking to me for a while. I can choose between no authentication (and now the admin interface is accessible to theoretically every website I visit), or password authentication (why do I need a password? it's all on one computer!). This PR adds magic tokens with which the syncthing browse and syncthing serve commands can magically log you in.

I hope that adding this and refining it enough will make it eventually possible to change the default from "zero auth" to "token auth only".

Testing

Some unit tests, and manually tested using syncthing browser and syncthing serve, both of which create a token when they open the browser.

Documentation

TODO

@tbodt tbodt changed the title token auth Magic token authentication from syncthing browse Jul 21, 2026
@tbodt
tbodt force-pushed the push-sxypwqolnszv branch from 475e638 to 2359f98 Compare July 21, 2026 04:00
@acolomb

acolomb commented Jul 26, 2026 •

Copy link
Copy Markdown
Member

Can you elaborate a bit more on what problem this actually solves?

The thing I see is that we have authentication via HTTP Bearer and the X-API-Key header already. But these cannot be passed to a browser process spawned by syncthing browse as headers. The login credentials could be passed via URL, but may end up in some browser component or history where a permanently valid access credential is not wanted. Thus the token is generated by the syncthing browse CLI invocation, by providing the already accessible API key from the config. This is then appended to the URL for launching the browser, but does not constitute permanent access.

Does that sound about right? If so, when does this "one-time" token expire then?

@tbodt

tbodt commented Jul 26, 2026

Copy link
Copy Markdown
Contributor Author

Yep, that's indeed how it works and why this adds another type of token. The token is invalidated immediately when used, and expires in 10 seconds otherwise. This was inspired by how mitmproxy opens its web UI.

@tbodt

tbodt commented Jul 26, 2026

Copy link
Copy Markdown
Contributor Author

I didn't actually check how mitmproxy does this before writing this PR. Just checked and it turns out that it generates a single random secret and makes no effort to remove it from browser history :P so this is basically cutting-edge security

@acolomb

acolomb commented Jul 26, 2026

Copy link
Copy Markdown
Member

Thanks for the confirmation. I think this is a sane approach, but let's hear opininons from other @syncthing/maintainers.

I have only glanced over the code, will take another thorough review once the goal is sanctioned and the code fixed to pass CI.

@tbodt
tbodt force-pushed the push-sxypwqolnszv branch from 2359f98 to d72a2a2 Compare July 26, 2026 22:48
@tbodt
tbodt force-pushed the push-sxypwqolnszv branch 2 times, most recently from ca7562c to 227605e Compare August 18, 2026 13:14
@tbodt

tbodt commented Aug 18, 2026

Copy link
Copy Markdown
Contributor Author

Build and lint fixed, PTAL

@acolomb acolomb changed the title Magic token authentication from syncthing browse feat(api): magic token authentication from syncthing browse Sep 13, 2026
@github-actions github-actions Bot added the enhancement New features or improvements of some kind, as opposed to a problem (bug) label Sep 13, 2026

@acolomb acolomb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The code looks good, except for a few nits / clarifications.

Since this is a security-critical path, I definitely cannot vouch for it alone though. Need a second pair of eyes over it, with more Go and REST auth experience.

Comment thread lib/api/api.go Resolved
Comment thread lib/api/api_auth.go Resolved
Comment thread cmd/syncthing/main.go Resolved
Comment thread cmd/syncthing/main.go Outdated
}
u.Path = path.Join(u.Path, "rest/system/upgrade")
target := u.String()
r, _ := http.NewRequestWithContext(context.TODO(), http.MethodPost, target, nil)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What's the thing with context.TODO() here? If none is needed, then just use NewRequest() as before. What am I missing?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Purely to shut up golangci-lint :P but i could write a //nolint instead.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What does the linter think is wrong with not using a context? I'd rather have some linter complaining instead of introducing a non-actionable "TODO" in the code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The lint message is from https://github.com/sonatard/noctx and says net/http.NewRequest must not be called. use net/http.NewRequestWithContext XD

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Switching to //nolint, there are already a few //nolint:noctx in the codebase in fact

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I still don't get why that linter sees this as an anti-pattern. Not an expert on Go contexts, but it sounds like enforcing some kind of API convention for libraries onto something that is not a library. In this case, I see no value in the suggested "fix" and adding a nolint is a distraction which only indicates inconsiderate linter usage.

@tbodt tbodt Sep 13, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Well, for this PR to pass CI I'll need to either add //nolint or modify the linter config to disable the lint, perhaps for a subsection of the codebase. That sounds something that would require extensive discussion, so I'll file a new issue, and leave this as //nolint for now.

(I was going to file it as a PR, but PR creation is disabled for me. Could I get onboarded as someone with permission to create PRs? I was expecting to send some follow up PRs for the magic token feature in particular.)

Edit: is github issues the right place for the discussion? it doesn't fit either of the allowed issue templates.

@acolomb acolomb Sep 13, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's fine to keep the //nolint comment for now. I'm just a bit allergic to over-eager linters with questionable opinions. :-)

I'd say discussion about the wider topic should happen on our forum. But don't take my opinion as requiring a change. Code policy is mainly decided by @calmh. Who can also onboard you as a contributor I suppose.

@acolomb

acolomb commented Sep 13, 2026

Copy link
Copy Markdown
Member

Thanks for the fixes, but please make additional commits instead of squashing in the future. Makes for an easier incremental review.

acolomb
acolomb previously approved these changes Sep 13, 2026
@tbodt

tbodt commented Sep 13, 2026

Copy link
Copy Markdown
Contributor Author

Sorry, missed your comment before the last force push, will add commits in the future

Comment thread cmd/syncthing/main.go Outdated
// fork, and just execs, hence keep it in its own routine.
go func() { _ = openURL(cfgWrapper.GUI().URL()) }()
go func() {
cmd := browserCmd{Verify: true}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see the desire to re-use existing code paths, but I guess there must have been a reason this previously used openURL directly? For one, we didn't have a "browser" subcommand as such on the CLI once upon a time...

Can you please explain why you think the extra steps taken in browserCmd.Run() (especially API verification) are harmless in contrast to the previous direct invocation? Of course this approach is needed for your auto-login addition, that's clear.

@tbodt tbodt Sep 13, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes this is just to trigger the auto login code that's part of the browser command. The only extra step that this adds beyond that is verification, which to me seems harmless.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

...why is it harmless? Because the auth code is about to do more or less the same thing anyway...wait, the verification code will exit the process if it fails and the auth code will just end up logging an error, and that could be bad because the current process is running the server. Might as well switch verification to false.

acolomb
acolomb previously approved these changes Sep 13, 2026
Signed-off-by: tbodt <[email protected]>
Signed-off-by: tbodt <[email protected]>
@acolomb

acolomb commented Sep 13, 2026

Copy link
Copy Markdown
Member

What about the failing tests now? Just flaky, or is that pointing to a failure mode involving the changed timing, because they may not be using --no-browser?

@tbodt

tbodt commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor Author

Looks like just a preexisting flake, the test in question has a comment about it: "This test needs a longer-than-default shutdown timeout to finish saving config changes when running on GitHub Actions". Pushed an empty commit and it all passes.

acolomb
acolomb previously approved these changes Oct 3, 2026
@acolomb

acolomb commented Oct 3, 2026

Copy link
Copy Markdown
Member

LGTM. Thanks for keeping up with the review process!

tbodt added 2 commits October 3, 2026 14:25
… someone decides to use Consume with other kinds of tokens!

Signed-off-by: tbodt <[email protected]>
Signed-off-by: tbodt <[email protected]>
@tbodt

tbodt commented Oct 3, 2026

Copy link
Copy Markdown
Contributor Author

Just used github copilot to look for security bugs, and didn't find any, but it did inspire a few small fixes which I just pushed.

@acolomb

acolomb commented Oct 4, 2026

Copy link
Copy Markdown
Member

Please don't force push. ;-) Thanks for the small fixes, looks good.

@acolomb

acolomb commented Oct 25, 2026

Copy link
Copy Markdown
Member

@calmh OK to merge from my side. WDYT?

@calmh
calmh merged commit 9da9c11 into syncthing:main Oct 27, 2026
33 checks passed
calmh added a commit to calmh/syncthing that referenced this pull request Oct 28, 2026
* main:
  fix: invalid files on remote devices are not "available" (fixes syncthing#10943) (syncthing#10944)
  feat(api): magic token authentication from `syncthing browse` (syncthing#10810)
  chore(gui, man, authors): update docs, translations, and contributors
  fix(model): skip ignored folders when auto-accepting (fixes syncthing#10935)
  fix(model): properly compare blocks when reconciling receive-only (fixes syncthing#10928) (syncthing#10929)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New features or improvements of some kind, as opposed to a problem (bug)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants