2021-09-16 19:04:54 +05:30
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2021-09-16 19:04:54 +05:30
|
|
|
|
|
|
|
package private
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
2021-12-10 13:44:24 +05:30
|
|
|
asymkey_model "code.gitea.io/gitea/models/asymkey"
|
2021-09-16 19:04:54 +05:30
|
|
|
"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()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// This is safe as force pushes are already forbidden
|
2022-10-23 20:14:45 +05:30
|
|
|
err = git.NewCommand(repo.Ctx, "rev-list").AddDynamicArguments(oldCommitID + "..." + newCommitID).
|
2022-04-01 08:25:30 +05:30
|
|
|
Run(&git.RunOpts{
|
|
|
|
Env: env,
|
|
|
|
Dir: repo.Path,
|
|
|
|
Stdout: stdoutWriter,
|
2022-02-11 18:17:22 +05:30
|
|
|
PipelineFunc: func(ctx context.Context, cancel context.CancelFunc) error {
|
2021-09-16 19:04:54 +05:30
|
|
|
_ = stdoutWriter.Close()
|
|
|
|
err := readAndVerifyCommitsFromShaReader(stdoutReader, repo, env)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("%v", err)
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
_ = stdoutReader.Close()
|
|
|
|
return err
|
2022-02-11 18:17:22 +05:30
|
|
|
},
|
|
|
|
})
|
2021-09-16 19:04:54 +05:30
|
|
|
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 {
|
|
|
|
log.Error("%v", err)
|
|
|
|
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()
|
|
|
|
}()
|
|
|
|
hash := git.MustIDFromString(sha)
|
|
|
|
|
2022-10-23 20:14:45 +05:30
|
|
|
return git.NewCommand(repo.Ctx, "cat-file", "commit").AddDynamicArguments(sha).
|
2022-04-01 08:25:30 +05:30
|
|
|
Run(&git.RunOpts{
|
|
|
|
Env: env,
|
|
|
|
Dir: repo.Path,
|
|
|
|
Stdout: stdoutWriter,
|
2022-02-11 18:17:22 +05:30
|
|
|
PipelineFunc: func(ctx context.Context, cancel context.CancelFunc) error {
|
2021-09-16 19:04:54 +05:30
|
|
|
_ = stdoutWriter.Close()
|
|
|
|
commit, err := git.CommitFromReader(repo, hash, stdoutReader)
|
|
|
|
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 19:07:34 +05:30
|
|
|
verification := asymkey_model.ParseCommitWithSignature(ctx, commit)
|
2021-09-16 19:04:54 +05:30
|
|
|
if !verification.Verified {
|
|
|
|
cancel()
|
|
|
|
return &errUnverifiedCommit{
|
|
|
|
commit.ID.String(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2022-02-11 18:17:22 +05:30
|
|
|
},
|
|
|
|
})
|
2021-09-16 19:04:54 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|