Skip to content

Refactor auth middleware#36848

Open
wxiaoguang wants to merge 7 commits intogo-gitea:mainfrom
wxiaoguang:fix-middleware-auth
Open

Refactor auth middleware#36848
wxiaoguang wants to merge 7 commits intogo-gitea:mainfrom
wxiaoguang:fix-middleware-auth

Conversation

@wxiaoguang
Copy link
Contributor

@wxiaoguang wxiaoguang commented Mar 6, 2026

Principles: let the caller decide what it needs, but not let the framework (middleware) guess what it should do.

Then a lot of hacky code can be removed. And some FIXMEs can be fixed.

This PR introduces a new kind of middleware: "PreMiddleware", it will be executed before all other middlewares on the same routing level, then a route can declare its options for other middlewares.

By the way, allow the workflow badge to be accessed by Basic or OAuth2 auth.

Fixes: #36830
Fixes: #36859

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Mar 6, 2026
@github-actions github-actions bot added modifies/api This PR adds API routes or modifies them modifies/go Pull requests that update Go code labels Mar 6, 2026
@wxiaoguang wxiaoguang marked this pull request as draft March 6, 2026 13:49
@wxiaoguang wxiaoguang force-pushed the fix-middleware-auth branch from c089c57 to af4714d Compare March 6, 2026 14:54
@wxiaoguang wxiaoguang marked this pull request as ready for review March 6, 2026 14:54
@wxiaoguang wxiaoguang requested a review from Copilot March 6, 2026 14:54
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors the auth middleware to follow the principle of "let the caller decide what it needs." It introduces a new PreMiddlewareProvider concept that executes before other middlewares, allowing routes to declare their own auth options (e.g., whether OAuth2 or Basic auth should be enabled) rather than having the middleware guess based on URL path patterns.

Changes:

  • Removes path-based auth detection (authPathDetector and related regex logic) from services/auth, replacing it with explicit per-route flags (CreateSession, AllowOAuth2, AllowBasic) set by callers.
  • Introduces PreMiddlewareProvider in modules/web/router.go and reworks wrapMiddlewareAndHandler to execute pre-middlewares before normal middlewares.
  • Updates routers/web/web.go to build auth groups dynamically per-request based on context flags set by route-level pre-middlewares.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
services/auth/sspi.go Replaces path-based session-creation guard with a CreateSession struct field
services/auth/reverseproxy.go Same CreateSession field added, path detection removed
services/auth/oauth2.go Removes path-based gating; OAuth2 is now always attempted when invoked
services/auth/basic.go Removes path-based gating; Basic auth is now always attempted when invoked
services/auth/auth_test.go Deletes tests for the removed authPathDetector
services/auth/auth.go Removes authPathDetector, regex globals, and related helpers
routers/web/web.go Introduces AuthMiddleware with PreMiddlewareProvider-based flags; updates route registrations
routers/api/v1/api.go Removes SSPI from API auth group; adds clarifying comment
modules/web/router_test.go Refactors test helpers into reusable testRecorder; adds TestPreMiddlewareProvider
modules/web/router_path.go Delegates to executeMiddlewaresHandler; panics on pre-middlewares in path matcher
modules/web/router.go Implements PreMiddlewareProvider, wrapMiddlewareAppendPre/Normal, refactors wrapMiddlewareAndHandler
modules/web/handler.go Introduces middlewareProvider type alias and executeMiddlewaresHandler helper

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

This comment was marked as resolved.

@wxiaoguang wxiaoguang force-pushed the fix-middleware-auth branch 5 times, most recently from 1535443 to 644a0d8 Compare March 6, 2026 21:06
@wxiaoguang wxiaoguang requested a review from Copilot March 6, 2026 21:17

This comment was marked as resolved.

@wxiaoguang wxiaoguang force-pushed the fix-middleware-auth branch 7 times, most recently from 61f45ae to c1d78ed Compare March 7, 2026 06:35
@wxiaoguang wxiaoguang force-pushed the fix-middleware-auth branch from c1d78ed to 279efff Compare March 7, 2026 07:08
@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Mar 7, 2026
@silverwind
Copy link
Member

Written by Claude.

The recovered panic in RequestContextHandler is already wrapped with a stack trace:

err := fmt.Errorf("%v\n%s", recovered, log.Stack(2))
RenderPanicErrorPage(respWriter, req, err)

Then RenderPanicErrorPage wraps it again with another stack trace:

combinedErr := fmt.Errorf("%w\n%s", err, log.Stack(2))

The logged error will contain two stack traces — the useful one from the panic site and a redundant one from the render function. Since it's the same goroutine, the second stack just adds noise. Consider only capturing the stack at the recover() site.

@wxiaoguang
Copy link
Contributor Author

Then RenderPanicErrorPage wraps it again with another stack trace:

A design problem due to history reasons. Ideally the panic stack should be handled in the "defer recover", but not in another function. To avoid unrelated changes in this PR, reverted to the old behavior.

@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Mar 8, 2026
@silverwind
Copy link
Member

I see this includes a fix for #36859, edited the PR description with both refs.

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

Labels

lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. modifies/api This PR adds API routes or modifies them modifies/go Pull requests that update Go code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test_MigrateFromGiteaToGitea fails when gitea.com is unavailable

5 participants