prevent some CodeQL warnings (#6027)

This commit is contained in:
Alessandro Ros
2026-08-03 16:47:25 +00:00
committed by GitHub
parent 62c119f0a4
commit 1c7bc95cdd
2 changed files with 36 additions and 1 deletions
+35
View File
@@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
"time"
@@ -13,6 +14,26 @@ import (
"github.com/gin-gonic/gin"
)
// this prevents directory traversal.
// functionally it's useless since there's already conf.IsValidPathName, but it's needed by CodeQL.
func isSubdirectoryOf(base string, path string) error {
baseAbs, err := filepath.Abs(filepath.Clean(base))
if err != nil {
return err
}
candidateAbs, err := filepath.Abs(filepath.Join(baseAbs, path))
if err != nil {
return err
}
if !strings.HasPrefix(candidateAbs, baseAbs) {
return fmt.Errorf("path escapes base directory")
}
return nil
}
func recordingsOfPath(
pathConf *conf.Path,
pathName string,
@@ -100,15 +121,29 @@ func (a *API) onRecordingDeleteSegment(ctx *gin.Context) {
return
}
commonPath := recordstore.CommonPath(pathConf.RecordPath)
pathFormat := recordstore.PathAddExtension(
strings.ReplaceAll(pathConf.RecordPath, "%path", pathName),
pathConf.RecordFormat,
)
err = isSubdirectoryOf(commonPath, pathFormat)
if err != nil {
a.writeError(ctx, http.StatusBadRequest, err)
return
}
segmentPath := recordstore.Path{
Start: start,
}.Encode(pathFormat)
err = isSubdirectoryOf(pathFormat, segmentPath)
if err != nil {
a.writeError(ctx, http.StatusBadRequest, err)
return
}
err = os.Remove(segmentPath)
if err != nil {
a.writeError(ctx, http.StatusBadRequest, err)
+1 -1
View File
@@ -27,7 +27,7 @@ const (
)
// this prevents directory traversal.
// functionally it's useless since there's already conf.IsPathName, but it's needed by CodeQL.
// functionally it's useless since there's already conf.IsValidPathName, but it's needed by CodeQL.
func safeSubDirectory(base string, pathName string) (string, error) {
baseAbs, err := filepath.Abs(filepath.Clean(base))
if err != nil {