Skip to content

core: preserve Gitea deployment prefixes for OAuth - #2479

Merged
mjcheetham merged 4 commits into
git-ecosystem:mainfrom
Huang-404-Q:fix/gitea-subfolder-oauth
Nov 6, 2026
Merged

mjcheetham merged 4 commits into
git-ecosystem:mainfrom
Huang-404-Q:fix/gitea-subfolder-oauth

Conversation

@Huang-404-Q

Copy link
Copy Markdown
Contributor

Summary

  • preserve a Gitea instance's deployment prefix when constructing universal OAuth endpoints
  • add a regression test for Gitea hosted under a subfolder

Validation

  • dotnet build git-credential-manager.slnx
  • dotnet test git-credential-manager.slnx

Fixes #1650

@Huang-404-Q
Huang-404-Q requested a review from a team as a code owner October 18, 2026 13:32
Comment thread src/Core/GenericOAuthConfig.cs Resolved
Comment thread src/Core/GenericOAuthConfig.cs Resolved
Comment thread src/Core/GenericOAuthConfig.cs Resolved
@Huang-404-Q

Copy link
Copy Markdown
Contributor Author

@becm Thanks for the helpful review and suggestions!

I have addressed all three suggestions in commit 5b738b6:

  1. Simplified \prefixPath\ to \string.Join('/', pathSegments.Take(pathSegments.Length - 2))\ and moved the trailing slash concatenation to \Path = prefixPath + '/' + endpoint.
  2. Removed the leading /\ from \WellKnown.GiteaAuthzEndpoint\ and \WellKnown.GiteaTokenEndpoint\ to explicitly treat them as relative paths.
  3. Updated and expanded the unit test in \GenericOAuthConfigTests\ into a [Theory]\ covering standard root deployments (/test/test.git), single-level subfolders (/git/test/test.git), and multi-level subfolders (/sub/git/test/test.git).

@Huang-404-Q

Copy link
Copy Markdown
Contributor Author

@becm Thanks — all three suggestions are in 5b738b6 and the threads are resolved. Could you take another look when you have a moment?

@becm becm 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.

For completeness, a test for the generic case of an empty path could be added.
But this would effectively just demonstrate the behavior of .Take(-2). 😄

Adds the generic empty-path case the reviewer asked for: an empty path
yields no prefix segments, so the endpoints stay at the host root.
@Huang-404-Q

Copy link
Copy Markdown
Contributor Author

@becm Thanks for the approval! I added the empty-path case to the theory in 0939c64 — it pins the .Take(-2) behavior you mentioned (no prefix segments, so the endpoints stay at the host root).

@hildelesen

This comment was marked as low quality.

@mjcheetham mjcheetham 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.

This seems like a real bug to fix; thanks for your contribution here! Just want to understand the logic behind the path splitting/calculation before we go forward. Thanks!

Comment thread src/Core/GenericOAuthConfig.cs Resolved
@mjcheetham mjcheetham added host:generic Specific to the generic (basic/WIA) host provider auth:oauth Specific to OAuth2 authentication labels Oct 29, 2026
@Huang-404-Q

Copy link
Copy Markdown
Contributor Author

Your reading is exactly right: we split remoteUri.AbsolutePath into segments, drop the last two and rejoin what remains as the deployment prefix. So https://host/git/owner/repo.git → prefix git → /git/login/oauth/authorize, while https://host/git/owner/repo.git with no prefix → /login/oauth/authorize.

On whether the last two segments are always owner/repo: for Gitea, yes. Repository web routes are /{owner}/{repo} — Gitea has no nested groups/subgroups (unlike GitLab), so there is no URL shape with an extra path segment between the instance prefix and the repo. A .git suffix or a trailing slash doesn't change the segment count (RemoveEmptyEntries handles the latter).

On the partial-URL concern: Take(pathSegments.Length - 2) with a non-positive count returns an empty sequence, so any path with fewer than three segments yields an empty prefix:

AbsolutePath segments prefix
(none) 0 ``
/git 1 ``
/git/owner 2 ``
/test/test.git 2 ``
/git/test/test.git 3 git
/sub/git/test/test.git 4 sub/git

So a partial URL can only fail to recover a prefix (falling back to the host root, which is exactly today's behaviour) — it can never produce a wrong prefix. That is also why the empty-path row is already in the test theory.

One thing worth being explicit about: remoteUri comes from GitRequest.GetRemoteUri(), which uses the path field Git passes to the credential helper — that is only populated when credential.useHttpPath = true. Without it we only ever see the host, so the prefix is not recoverable at all and the endpoints stay at the root, unchanged from before this PR.

Happy to add rows for the partial shapes (/git, /git/owner) to pin that fallback, and a comment on CreateGiteaEndpointUri stating the owner/repo assumption, if you think it's worth it — say the word and I'll push it. (Note: there's no .NET SDK on my machine right now, so I'd be relying on CI for the test run.)

@becm

becm commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

I'd consider this adjustment a (small) band-aid to improve guessing the correct URL.

Getting the correct setting from .well-known/openid-configuration would be preferable.
(Unless someone decided to go for a "hidden variant" → Github)

This would whoever require to run a consistent discovery step on each token renewal.
Or save this in metadata as part of a refresh_token credential entry (beware of wincred size limits!).

@mjcheetham

Copy link
Copy Markdown
Contributor

a comment on CreateGiteaEndpointUri stating the owner/repo assumption

I think a short comment in the code that we assume the format of the Gitea remote URLs is always {host}/{prefix}/{owner}/{repo} and therefore this extracts the {host}/{prefix}, would be helpful for a future reader.

@mjcheetham

Copy link
Copy Markdown
Contributor

Getting the correct setting from .well-known/openid-configuration would be preferable. (Unless someone decided to go for a "hidden variant" → Github)

This would whoever require to run a consistent discovery step on each token renewal. Or save this in metadata as part of a refresh_token credential entry (beware of wincred size limits!).

Adding OpenID connect support with discovery (and just the almost-never-implemented OAuth2 metadata/discovery RFC) is something I've considered in the past. IIRC there's even a TODO in the generic OAuth code that talks about using OIDC to resolve a username from the ID token too.

I'd consider this adjustment a (small) band-aid to improve guessing the correct URL.

Agreed. If it guesses wrong, you can always specify the config explicitly.

@Huang-404-Q

Copy link
Copy Markdown
Contributor Author

Added the comment on CreateGiteaEndpointUri in 6ed474b:

// A Gitea remote URL is always '{host}/{prefix}/{owner}/{repo}' — Gitea has no nested
// groups — so dropping the last two segments leaves the '{host}/{prefix}' that the OAuth
// endpoints hang off. When Git omits the path (credential.useHttpPath is false), or passes
// fewer than two segments, the prefix is empty and the endpoints stay at the host root.

That should cover both the shape assumption and the fallback behaviour a future reader would otherwise have to reverse-engineer.

On the discovery idea from @becm: agreed it's the better long-term answer, but as you said it changes the token-renewal path for every generic OAuth provider, so I've kept it out of this PR.

@mjcheetham mjcheetham 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!

@mjcheetham
mjcheetham merged commit 4fc94be into git-ecosystem:main Nov 6, 2026
23 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auth:oauth Specific to OAuth2 authentication host:generic Specific to the generic (basic/WIA) host provider

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Universal Gitea OAuth configuration uses wrong endpoints if gitea uses a subfolder

4 participants