fix(webui): mark cookies Secure behind a TLS proxy; run gosec in the gate (#1919)
Cookies took Secure from r.TLS, which is nil behind a proxy that terminates TLS, so sign-in cookies went out without it. Every dashboard cookie now goes through one builder that reads the scheme the way the origin check does. The worktree reset and the agent worker's chown walk use os.Root, so a symlink swapped in mid-walk can't point them outside the worktree. gosec runs in golangci-lint, tests included. G104, G115, G204 and G304 are excluded in config; each remaining finding is fixed or suppressed at the site with its reason.
This commit is contained in:
46 files changed
+199
-115
No files matched your search
@@ -2,7 +2,7 @@ package worktree
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/cgi"
|
||||
"net/http/cgi" //nolint:gosec // G504: serves git http-backend; the Httpoxy CVE predates Go 1.6.3.
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"os/exec"
|
||||
@@ -87,7 +87,7 @@ func TestForgeHTTPAuthentication(t *testing.T) {
|
||||
defer target.Close()
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if tc.foreign {
|
||||
http.Redirect(w, r, target.URL+r.URL.RequestURI(), http.StatusTemporaryRedirect)
|
||||
http.Redirect(w, r, target.URL+r.URL.RequestURI(), http.StatusTemporaryRedirect) //nolint:gosec // G710: the foreign redirect is what the test checks.
|
||||
return
|
||||
}
|
||||
user, password, ok := r.BasicAuth()
|
||||
@@ -101,7 +101,7 @@ func TestForgeHTTPAuthentication(t *testing.T) {
|
||||
return
|
||||
}
|
||||
if tc.redirect && !strings.HasPrefix(r.URL.Path, "/redirect/") {
|
||||
http.Redirect(w, r, "/redirect"+r.URL.RequestURI(), http.StatusTemporaryRedirect)
|
||||
http.Redirect(w, r, "/redirect"+r.URL.RequestURI(), http.StatusTemporaryRedirect) //nolint:gosec // G710: the foreign redirect is what the test checks.
|
||||
return
|
||||
}
|
||||
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/redirect")
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -550,18 +551,20 @@ func cleanUntracked(dir string, tree *object.Tree) error {
|
||||
if err := collectTrackedPaths(tree, "", files, dirs, opaqueDirs); err != nil {
|
||||
return err
|
||||
}
|
||||
return filepath.WalkDir(dir, func(path string, entry os.DirEntry, walkErr error) error {
|
||||
// The agent writes this tree, so every operation goes through a root that
|
||||
// refuses to follow a symlink out of it, even one swapped in mid-walk.
|
||||
root, err := os.OpenRoot(dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = root.Close() }()
|
||||
return fs.WalkDir(root.FS(), ".", func(rel string, entry fs.DirEntry, walkErr error) error {
|
||||
if walkErr != nil {
|
||||
return walkErr
|
||||
}
|
||||
if path == dir {
|
||||
if rel == "." {
|
||||
return nil
|
||||
}
|
||||
rel, err := filepath.Rel(dir, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rel = filepath.ToSlash(rel)
|
||||
if rel == ".git" {
|
||||
// Leave a .git file or directory alone.
|
||||
if entry.IsDir() {
|
||||
@@ -580,7 +583,7 @@ func cleanUntracked(dir string, tree *object.Tree) error {
|
||||
// directory sitting on a path the tree declares as a FILE -- is
|
||||
// output, not tracked content. It has to go before the reset can
|
||||
// write the file this path is supposed to hold.
|
||||
if err := os.RemoveAll(path); err != nil {
|
||||
if err := root.RemoveAll(rel); err != nil {
|
||||
return err
|
||||
}
|
||||
return filepath.SkipDir
|
||||
@@ -588,7 +591,7 @@ func cleanUntracked(dir string, tree *object.Tree) error {
|
||||
if _, ok := files[rel]; ok {
|
||||
return nil
|
||||
}
|
||||
return os.Remove(path)
|
||||
return root.Remove(rel)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -634,7 +637,7 @@ func (m *Manager) setIdentity(r *git.Repository) error {
|
||||
|
||||
// writeSentinel marks the worktree as fully prepared.
|
||||
func writeSentinel(dir string) error {
|
||||
if err := os.WriteFile(filepath.Join(dir, preparedSentinel), nil, 0o644); err != nil {
|
||||
if err := os.WriteFile(filepath.Join(dir, preparedSentinel), nil, 0o644); err != nil { //nolint:gosec // G306: the task container reads the sentinel as another user.
|
||||
return fmt.Errorf("write prepared sentinel: %w", err)
|
||||
}
|
||||
return nil
|
||||
@@ -1046,7 +1049,7 @@ func writeSnapshotFile(destDir string, file *object.File) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("read tracked file %s: %w", file.Name, err)
|
||||
}
|
||||
if err := os.WriteFile(target, []byte(contents), 0o644); err != nil {
|
||||
if err := os.WriteFile(target, []byte(contents), 0o644); err != nil { //nolint:gosec // G306: the task container works on these files as another user.
|
||||
return fmt.Errorf("write snapshot file %s: %w", file.Name, err)
|
||||
}
|
||||
return nil
|
||||
|
||||
Reference in new issue
Block a user