mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-03-25 11:33:02 -04:00
Forgejo's OCI container registry did not enable basic authentication for the top-level endpoint `/v2`. Furthermore, it did not include the `WWW-Authenticate` header when returning the status code 401 as mandated by [RFC 7235](https://datatracker.ietf.org/doc/html/rfc7235#section-3.1), "Hypertext Transfer Protocol (HTTP/1.1): Authentication", section 3.1. Those deficiencies made it impossible for Apple's [container](https://github.com/apple/container) to log into Forgejo OCI container registry. This has been rectified. The problem did not occur with most other tools because they do not include credentials when sending the initial request to `/v2`. Forgejo's reply then included `WWW-Authenticate` as expected. Enabling basic authentication for `/v2` has the side effect that Apple's container uses username and password for all successive requests and not the bearer token. If that is a problem, it's up to Apple to change container's behaviour. If invalid credentials are passed to `container registry login`, then container enters an infinite loop. The same happens with quay.io, but not ghcr.io (returns 403) or docker.io (returns 401 but _without_ `WWW-Authenticate`). As this is invalid behaviour on container's side, it's up to Apple to change container. Docker and Podman handle it correctly. Login and pushing have been tested manually with Docker 29.1.3, Podman 5.7.1, and Apple's container 0.9.0. Resolves https://codeberg.org/forgejo/forgejo/issues/11297. ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests for Go changes (can be removed for JavaScript changes) - I added test coverage for Go changes... - [x] in their respective `*_test.go` for unit tests. - [x] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I ran... - [ ] `make pr-go` before pushing ### Tests for JavaScript changes (can be removed for Go changes) - I added test coverage for JavaScript changes... - [ ] in `web_src/js/*.test.js` if it can be unit tested. - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)). ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [x] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change. - [ ] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change. *The decision if the pull request will be shown in the release notes is up to the mergers / release team.* The content of the `release-notes/<pull request number>.md` file will serve as the basis for the release notes. If the file does not exist, the title of the pull request will be used instead. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11393 Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org> Co-authored-by: Andreas Ahlenstorf <andreas@ahlenstorf.ch> Co-committed-by: Andreas Ahlenstorf <andreas@ahlenstorf.ch>
103 lines
3.4 KiB
Go
103 lines
3.4 KiB
Go
// Copyright 2014 The Gogs Authors. All rights reserved.
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package auth
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"regexp"
|
|
"strings"
|
|
|
|
user_model "forgejo.org/models/user"
|
|
"forgejo.org/modules/auth/webauthn"
|
|
"forgejo.org/modules/log"
|
|
"forgejo.org/modules/optional"
|
|
"forgejo.org/modules/session"
|
|
"forgejo.org/modules/setting"
|
|
"forgejo.org/modules/web/middleware"
|
|
user_service "forgejo.org/services/user"
|
|
)
|
|
|
|
// Init should be called exactly once when the application starts to allow plugins
|
|
// to allocate necessary resources
|
|
func Init() {
|
|
webauthn.Init()
|
|
}
|
|
|
|
// isAttachmentDownload check if request is a file download (GET) with URL to an attachment
|
|
func isAttachmentDownload(req *http.Request) bool {
|
|
return strings.HasPrefix(req.URL.Path, "/attachments/") && req.Method == "GET"
|
|
}
|
|
|
|
// isContainerPath checks if the request targets the container endpoint
|
|
func isContainerPath(req *http.Request) bool {
|
|
// Go's URL omits trailing slashes from `Path`. That means that `/v2/`, the top-level endpoint, appears as `/v2`.
|
|
// strings.HasPrefix(req.URL.Path, "/v2") would be inappropriate because it would match paths like `/v2-abcd`, too.
|
|
return req.URL.Path == "/v2" || strings.HasPrefix(req.URL.Path, "/v2/")
|
|
}
|
|
|
|
var (
|
|
gitRawOrAttachPathRe = regexp.MustCompile(`^/[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+/(?:(?:git-(?:(?:upload)|(?:receive))-pack$)|(?:info/refs$)|(?:HEAD$)|(?:objects/)|(?:raw/)|(?:releases/download/)|(?:attachments/))`)
|
|
lfsPathRe = regexp.MustCompile(`^/[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+/info/lfs/`)
|
|
archivePathRe = regexp.MustCompile(`^/[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+/archive/`)
|
|
)
|
|
|
|
func isGitRawOrAttachPath(req *http.Request) bool {
|
|
return gitRawOrAttachPathRe.MatchString(req.URL.Path)
|
|
}
|
|
|
|
func isGitRawOrAttachOrLFSPath(req *http.Request) bool {
|
|
if isGitRawOrAttachPath(req) {
|
|
return true
|
|
}
|
|
if setting.LFS.StartServer {
|
|
return lfsPathRe.MatchString(req.URL.Path)
|
|
}
|
|
return false
|
|
}
|
|
|
|
func isArchivePath(req *http.Request) bool {
|
|
return archivePathRe.MatchString(req.URL.Path)
|
|
}
|
|
|
|
// handleSignIn clears existing session variables and stores new ones for the specified user object
|
|
func handleSignIn(resp http.ResponseWriter, req *http.Request, sess SessionStore, user *user_model.User) {
|
|
// We need to regenerate the session...
|
|
newSess, err := session.RegenerateSession(resp, req)
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("Error regenerating session: %v", err))
|
|
} else {
|
|
sess = newSess
|
|
}
|
|
|
|
_ = sess.Delete("openid_verified_uri")
|
|
_ = sess.Delete("openid_signin_remember")
|
|
_ = sess.Delete("openid_determined_email")
|
|
_ = sess.Delete("openid_determined_username")
|
|
_ = sess.Delete("twofaUid")
|
|
_ = sess.Delete("twofaRemember")
|
|
_ = sess.Delete("twofaOpenID")
|
|
_ = sess.Delete("webauthnAssertion")
|
|
_ = sess.Delete("linkAccount")
|
|
err = sess.Set("uid", user.ID)
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("Error setting session: %v", err))
|
|
}
|
|
|
|
// Language setting of the user overwrites the one previously set
|
|
// If the user does not have a locale set, we save the current one.
|
|
if len(user.Language) == 0 {
|
|
lc := middleware.Locale(resp, req)
|
|
opts := &user_service.UpdateOptions{
|
|
Language: optional.Some(lc.Language()),
|
|
}
|
|
if err := user_service.UpdateUser(req.Context(), user, opts); err != nil {
|
|
log.Error(fmt.Sprintf("Error updating user language [user: %d, locale: %s]", user.ID, user.Language))
|
|
return
|
|
}
|
|
}
|
|
|
|
middleware.SetLocaleCookie(resp, user.Language, 0)
|
|
}
|