2021-09-16 08:34:54 -05:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-09-16 08:34:54 -05:00
|
|
|
|
|
|
|
package private
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
2021-12-10 03:14:24 -05:00
|
|
|
asymkey_model "code.gitea.io/gitea/models/asymkey"
|
2021-09-16 08:34:54 -05:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This file contains commit verification functions for refs passed across in hooks
|
|
|
|
|
|
|
|
func verifyCommits(oldCommitID, newCommitID string, repo *git.Repository, env []string) error {
|
|
|
|
stdoutReader, stdoutWriter, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to create os.Pipe for %s", repo.Path)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
_ = stdoutReader.Close()
|
|
|
|
_ = stdoutWriter.Close()
|
|
|
|
}()
|
|
|
|
|
2023-08-29 21:27:53 -05:00
|
|
|
var command *git.Command
|
2023-12-13 16:02:00 -05:00
|
|
|
objectFormat, _ := repo.GetObjectFormat()
|
2023-12-17 06:56:08 -05:00
|
|
|
if oldCommitID == objectFormat.EmptyObjectID().String() {
|
2023-08-29 21:27:53 -05:00
|
|
|
// When creating a new branch, the oldCommitID is empty, by using "newCommitID --not --all":
|
|
|
|
// List commits that are reachable by following the newCommitID, exclude "all" existing heads/tags commits
|
|
|
|
// So, it only lists the new commits received, doesn't list the commits already present in the receiving repository
|
|
|
|
command = git.NewCommand(repo.Ctx, "rev-list").AddDynamicArguments(newCommitID).AddArguments("--not", "--all")
|
|
|
|
} else {
|
|
|
|
command = git.NewCommand(repo.Ctx, "rev-list").AddDynamicArguments(oldCommitID + "..." + newCommitID)
|
|
|
|
}
|
2021-09-16 08:34:54 -05:00
|
|
|
// This is safe as force pushes are already forbidden
|
2023-08-29 21:27:53 -05:00
|
|
|
err = command.Run(&git.RunOpts{
|
|
|
|
Env: env,
|
|
|
|
Dir: repo.Path,
|
|
|
|
Stdout: stdoutWriter,
|
|
|
|
PipelineFunc: func(ctx context.Context, cancel context.CancelFunc) error {
|
|
|
|
_ = stdoutWriter.Close()
|
|
|
|
err := readAndVerifyCommitsFromShaReader(stdoutReader, repo, env)
|
|
|
|
if err != nil {
|
2024-04-07 06:17:06 -05:00
|
|
|
log.Error("readAndVerifyCommitsFromShaReader failed: %v", err)
|
2023-08-29 21:27:53 -05:00
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
_ = stdoutReader.Close()
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
})
|
2021-09-16 08:34:54 -05:00
|
|
|
if err != nil && !isErrUnverifiedCommit(err) {
|
|
|
|
log.Error("Unable to check commits from %s to %s in %s: %v", oldCommitID, newCommitID, repo.Path, err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func readAndVerifyCommitsFromShaReader(input io.ReadCloser, repo *git.Repository, env []string) error {
|
|
|
|
scanner := bufio.NewScanner(input)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
err := readAndVerifyCommit(line, repo, env)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return scanner.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
func readAndVerifyCommit(sha string, repo *git.Repository, env []string) error {
|
|
|
|
stdoutReader, stdoutWriter, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to create pipe for %s: %v", repo.Path, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
_ = stdoutReader.Close()
|
|
|
|
_ = stdoutWriter.Close()
|
|
|
|
}()
|
2023-12-19 02:20:47 -05:00
|
|
|
|
|
|
|
commitID := git.MustIDFromString(sha)
|
2021-09-16 08:34:54 -05:00
|
|
|
|
2022-10-23 09:44:45 -05:00
|
|
|
return git.NewCommand(repo.Ctx, "cat-file", "commit").AddDynamicArguments(sha).
|
2022-03-31 21:55:30 -05:00
|
|
|
Run(&git.RunOpts{
|
|
|
|
Env: env,
|
|
|
|
Dir: repo.Path,
|
|
|
|
Stdout: stdoutWriter,
|
2022-02-11 07:47:22 -05:00
|
|
|
PipelineFunc: func(ctx context.Context, cancel context.CancelFunc) error {
|
2021-09-16 08:34:54 -05:00
|
|
|
_ = stdoutWriter.Close()
|
2023-12-13 16:02:00 -05:00
|
|
|
commit, err := git.CommitFromReader(repo, commitID, stdoutReader)
|
2021-09-16 08:34:54 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 08:37:34 -05:00
|
|
|
verification := asymkey_model.ParseCommitWithSignature(ctx, commit)
|
2021-09-16 08:34:54 -05:00
|
|
|
if !verification.Verified {
|
|
|
|
cancel()
|
|
|
|
return &errUnverifiedCommit{
|
|
|
|
commit.ID.String(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2022-02-11 07:47:22 -05:00
|
|
|
},
|
|
|
|
})
|
2021-09-16 08:34:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type errUnverifiedCommit struct {
|
|
|
|
sha string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *errUnverifiedCommit) Error() string {
|
|
|
|
return fmt.Sprintf("Unverified commit: %s", e.sha)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isErrUnverifiedCommit(err error) bool {
|
|
|
|
_, ok := err.(*errUnverifiedCommit)
|
|
|
|
return ok
|
|
|
|
}
|