mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
2978 files. The fork is 2026-07-15 (rebrand: gitea.dev -> github.com/hanzoai/git), so 2026 is the year, and "Copyright 2026 Hanzo AI, Inc." was already the form used by the handful of files that had ours. Shape is now: // Copyright 2026 Hanzo AI, Inc. All rights reserved. // Copyright 2015 The Gogs Authors. All rights reserved. // Copyright 2019 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT Ours on top, theirs retained verbatim. Retained because MIT requires it, and separately because they are true — those authors did write that code. Not renamed to "The Git Authors": Git is a different project under a different license, and putting its name here would attribute this code to people who have nothing to do with it. Adding our line is the fix; overwriting theirs would have been both a licence violation and a false statement. gofmt still reports import-order drift in some of these files. It predates this change (verified against a stash) and is left alone rather than burying a one-line-per-file legal change under 3000 files of unrelated reordering. Build clean. Co-authored-by: Hanzo Dev <[email protected]>
109 lines
3.7 KiB
Go
109 lines
3.7 KiB
Go
// Copyright 2020 The Macaron Authors
|
|
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"github.com/hanzoai/git/modules/session"
|
|
"github.com/hanzoai/git/modules/setting"
|
|
"github.com/hanzoai/git/modules/util"
|
|
)
|
|
|
|
const (
|
|
CookieWebBannerDismissed = "hanzo_git_disbnr"
|
|
CookieTheme = "hanzo_git_theme"
|
|
cookieRedirectTo = "redirect_to"
|
|
)
|
|
|
|
func GetRedirectToCookie(req *http.Request) string {
|
|
return GetSiteCookie(req, cookieRedirectTo)
|
|
}
|
|
|
|
// SetRedirectToCookie convenience function to set the RedirectTo cookie consistently
|
|
func SetRedirectToCookie(resp http.ResponseWriter, value string) {
|
|
SetSiteCookie(resp, cookieRedirectTo, value, 0)
|
|
}
|
|
|
|
// DeleteRedirectToCookie convenience function to delete most cookies consistently
|
|
func DeleteRedirectToCookie(resp http.ResponseWriter) {
|
|
SetSiteCookie(resp, cookieRedirectTo, "", -1)
|
|
}
|
|
|
|
func RedirectLinkUserLogin(req *http.Request) string {
|
|
if req.Header.Get("X-Fetch-Action") != "" {
|
|
// when building the redirect link for a fetch request, the current link might be a partial page,
|
|
// so we only redirect to the login page without redirect_to parameter
|
|
return setting.AppSubURL + "/user/login"
|
|
}
|
|
return setting.AppSubURL + "/user/login?redirect_to=" + url.QueryEscape(setting.AppSubURL+req.URL.RequestURI())
|
|
}
|
|
|
|
// GetSiteCookie returns given cookie value from request header.
|
|
func GetSiteCookie(req *http.Request, name string) string {
|
|
cookie, err := req.Cookie(name)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
val, _ := url.QueryUnescape(cookie.Value)
|
|
return val
|
|
}
|
|
|
|
// SetSiteCookie returns given cookie value from request header.
|
|
func SetSiteCookie(resp http.ResponseWriter, name, value string, maxAge int) {
|
|
// Previous versions would use a cookie path with a trailing /.
|
|
// These are more specific than cookies without a trailing /, so
|
|
// we need to delete these if they exist.
|
|
deleteLegacySiteCookie(resp, name)
|
|
|
|
// HINT: INSTALL-PAGE-COOKIE-INIT: the cookie system is not properly initialized on the Install page, so there is no CookiePath
|
|
cookie := &http.Cookie{
|
|
Name: name,
|
|
Value: url.QueryEscape(value),
|
|
MaxAge: maxAge,
|
|
Path: util.IfZero(setting.SessionConfig.CookiePath, "/"),
|
|
Domain: setting.SessionConfig.Domain,
|
|
Secure: setting.SessionConfig.Secure,
|
|
HttpOnly: true,
|
|
SameSite: setting.SessionConfig.SameSite,
|
|
}
|
|
resp.Header().Add("Set-Cookie", cookie.String())
|
|
}
|
|
|
|
// deleteLegacySiteCookie deletes the cookie with the given name at the cookie
|
|
// path with a trailing /, which would unintentionally override the cookie.
|
|
func deleteLegacySiteCookie(resp http.ResponseWriter, name string) {
|
|
if setting.SessionConfig.CookiePath == "" || strings.HasSuffix(setting.SessionConfig.CookiePath, "/") {
|
|
// If the cookie path ends with /, no legacy cookies will take
|
|
// precedence, so do nothing. The exception is that cookies with no
|
|
// path could override other cookies, but it's complicated and we don't
|
|
// currently handle that.
|
|
return
|
|
}
|
|
|
|
cookie := &http.Cookie{
|
|
Name: name,
|
|
Value: "",
|
|
MaxAge: -1,
|
|
Path: setting.SessionConfig.CookiePath + "/",
|
|
Domain: setting.SessionConfig.Domain,
|
|
Secure: setting.SessionConfig.Secure,
|
|
HttpOnly: true,
|
|
SameSite: setting.SessionConfig.SameSite,
|
|
}
|
|
resp.Header().Add("Set-Cookie", cookie.String())
|
|
}
|
|
|
|
func init() {
|
|
session.BeforeRegenerateSession = append(session.BeforeRegenerateSession, func(resp http.ResponseWriter, _ *http.Request) {
|
|
// Ensure that a cookie with a trailing slash does not take precedence over
|
|
// the cookie written by the middleware.
|
|
deleteLegacySiteCookie(resp, setting.SessionConfig.CookieName)
|
|
})
|
|
}
|