Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: enforce URL encoding in authentication redirects and tests#47

Merged
appleboy merged 2 commits intomainfrom
auth
Feb 22, 2026
Merged

fix: enforce URL encoding in authentication redirects and tests#47
appleboy merged 2 commits intomainfrom
auth

Conversation

@appleboy
Copy link
Member

  • Ensure redirect URLs in authentication middleware are properly URL-encoded
  • Add unit tests to verify URL encoding of redirect parameters during login, timeout, and fingerprint mismatch scenarios

- Ensure redirect URLs in authentication middleware are properly URL-encoded
- Add unit tests to verify URL encoding of redirect parameters during login, timeout, and fingerprint mismatch scenarios

Signed-off-by: appleboy <[email protected]>
Copilot AI review requested due to automatic review settings February 22, 2026 13:39
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 ensures authentication-related redirects (login, idle timeout, fingerprint mismatch) correctly URL-encode the “redirect” return URL, and adds unit tests to validate that complex querystrings survive round-tripping through the login redirect.

Changes:

  • URL-encode redirect return URLs using url.QueryEscape in auth middleware redirects.
  • Add unit tests covering redirect encoding for unauthenticated access, session idle timeout, and fingerprint mismatch cases.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
internal/middleware/auth.go Apply URL encoding to redirect query parameter in /login redirects.
internal/middleware/auth_test.go Add tests asserting redirected redirect parameter preserves complex querystrings across scenarios.
Comments suppressed due to low confidence (1)

internal/middleware/auth.go:52

  • RequireAuth dereferences userService unconditionally when a session is present (userService.GetUserByID(...)). Since this middleware can be called with a nil service (as in the added tests), an authenticated request would panic. Consider guarding the audit-load block with if userService != nil { ... } (or making the parameter non-optional via an interface/constructor) so the middleware is safe even when userService is nil.
		if userID == nil {
			// Redirect to login with return URL
			redirectURL := c.Request.URL.String()
			c.Redirect(http.StatusFound, "/login?redirect="+url.QueryEscape(redirectURL))
			c.Abort()
			return
		}

		c.Set("user_id", userID)

		// Load user object for audit logging and other purposes
		user, err := userService.GetUserByID(userID.(string))
		if err == nil {
			c.Set("user", user)

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

- Add createTestUserService helper to set up a test UserService with an in-memory store, preventing nil pointer issues in tests
- Update TestRequireAuth_RedirectURLEncoded to use a real test UserService, ensuring middleware stability if code paths change
- Add additional imports necessary for the new helper function

Signed-off-by: appleboy <[email protected]>
@appleboy appleboy merged commit 47ff688 into main Feb 22, 2026
15 checks passed
@appleboy appleboy deleted the auth branch February 22, 2026 13:50
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.

2 participants