Skip to content

Refactor auth middleware#36848

Merged
wxiaoguang merged 7 commits intogo-gitea:mainfrom
wxiaoguang:fix-middleware-auth
Mar 8, 2026
Merged

Refactor auth middleware#36848
wxiaoguang merged 7 commits intogo-gitea:mainfrom
wxiaoguang:fix-middleware-auth

Conversation

@wxiaoguang
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member

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

@wxiaoguang wxiaoguang merged commit 3f1ef70 into go-gitea:main Mar 8, 2026
26 checks passed
@GiteaBot GiteaBot added this to the 1.26.0 milestone Mar 8, 2026
@wxiaoguang wxiaoguang deleted the fix-middleware-auth branch March 8, 2026 09:59
silverwind pushed a commit to silverwind/gitea that referenced this pull request Mar 8, 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: go-gitea#36830
Fixes: go-gitea#36859
silverwind added a commit to silverwind/gitea that referenced this pull request Mar 8, 2026
* origin/main:
  Optimize Docker build with dependency layer caching (go-gitea#36864)
  Fix URLJoin, markup render link reoslving, sign-in/up/linkaccount page common data (go-gitea#36861)
  Fix CodeQL code scanning alerts (go-gitea#36858)
  Refactor auth middleware (go-gitea#36848)
  Update Nix flake (go-gitea#36857)
  Update JS deps (go-gitea#36850)
  Load `mentionValues` asynchronously (go-gitea#36739)
  [skip ci] Updated translations via Crowdin
silverwind added a commit to silverwind/gitea that referenced this pull request Mar 8, 2026
* main: (26 commits)
  Clean up `refreshViewedFilesSummary` (go-gitea#36868)
  Remove `util.URLJoin` and replace all callers with direct path concatenation (go-gitea#36867)
  Optimize Docker build with dependency layer caching (go-gitea#36864)
  Fix URLJoin, markup render link reoslving, sign-in/up/linkaccount page common data (go-gitea#36861)
  Fix CodeQL code scanning alerts (go-gitea#36858)
  Refactor auth middleware (go-gitea#36848)
  Update Nix flake (go-gitea#36857)
  Update JS deps (go-gitea#36850)
  Load `mentionValues` asynchronously (go-gitea#36739)
  [skip ci] Updated translations via Crowdin
  Fix dbfs error handling (go-gitea#36844)
  Fix OAuth2 authorization code expiry and reuse handling (go-gitea#36797)
  Fix org permission API visibility checks for hidden members and private orgs (go-gitea#36798)
  Fix non-admins unable to automerge PRs from forks (go-gitea#36833)
  upgrade to github.com/cloudflare/circl 1.6.3, svgo 4.0.1, markdownlint-cli 0.48.0 (go-gitea#36837)
  Fix dump release asset bug (go-gitea#36799)
  build(deps): update material-icon-theme v5.32.0 (go-gitea#36832)
  Fix bug to check whether user can update pull request branch or rebase branch (go-gitea#36465)
  Fix forwarded proto handling for public URL detection (go-gitea#36810)
  Fix artifacts v4 backend upload problems (go-gitea#36805)
  ...

# Conflicts:
#	pnpm-lock.yaml
zjjhot added a commit to zjjhot/gitea that referenced this pull request Mar 10, 2026
* giteaofficial/main:
  Update minimum go version to 1.26.1, golangci-lint to 2.11.2, fix test style (go-gitea#36876)
  Add render cache for SVG icons (go-gitea#36863)
  Fix incorrect viewed files counter if reverted change was viewed (go-gitea#36819)
  [skip ci] Updated translations via Crowdin
  Clean up `refreshViewedFilesSummary` (go-gitea#36868)
  Remove `util.URLJoin` and replace all callers with direct path concatenation (go-gitea#36867)
  Optimize Docker build with dependency layer caching (go-gitea#36864)
  Fix URLJoin, markup render link reoslving, sign-in/up/linkaccount page common data (go-gitea#36861)
  Fix CodeQL code scanning alerts (go-gitea#36858)
  Refactor auth middleware (go-gitea#36848)
  Update Nix flake (go-gitea#36857)
  Update JS deps (go-gitea#36850)
  Load `mentionValues` asynchronously (go-gitea#36739)
  [skip ci] Updated translations via Crowdin
  Fix dbfs error handling (go-gitea#36844)
  Fix OAuth2 authorization code expiry and reuse handling (go-gitea#36797)
  Fix org permission API visibility checks for hidden members and private orgs (go-gitea#36798)
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