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" "fmt"
"net/http" "net/http"
"os" "os"
"path/filepath"
"strings" "strings"
"time" "time"
@@ -13,6 +14,26 @@ import (
"github.com/gin-gonic/gin" "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( func recordingsOfPath(
pathConf *conf.Path, pathConf *conf.Path,
pathName string, pathName string,
@@ -100,15 +121,29 @@ func (a *API) onRecordingDeleteSegment(ctx *gin.Context) {
return return
} }
commonPath := recordstore.CommonPath(pathConf.RecordPath)
pathFormat := recordstore.PathAddExtension( pathFormat := recordstore.PathAddExtension(
strings.ReplaceAll(pathConf.RecordPath, "%path", pathName), strings.ReplaceAll(pathConf.RecordPath, "%path", pathName),
pathConf.RecordFormat, pathConf.RecordFormat,
) )
err = isSubdirectoryOf(commonPath, pathFormat)
if err != nil {
a.writeError(ctx, http.StatusBadRequest, err)
return
}
segmentPath := recordstore.Path{ segmentPath := recordstore.Path{
Start: start, Start: start,
}.Encode(pathFormat) }.Encode(pathFormat)
err = isSubdirectoryOf(pathFormat, segmentPath)
if err != nil {
a.writeError(ctx, http.StatusBadRequest, err)
return
}
err = os.Remove(segmentPath) err = os.Remove(segmentPath)
if err != nil { if err != nil {
a.writeError(ctx, http.StatusBadRequest, err) a.writeError(ctx, http.StatusBadRequest, err)
+1 -1
View File
@@ -27,7 +27,7 @@ const (
) )
// this prevents directory traversal. // 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) { func safeSubDirectory(base string, pathName string) (string, error) {
baseAbs, err := filepath.Abs(filepath.Clean(base)) baseAbs, err := filepath.Abs(filepath.Clean(base))
if err != nil { if err != nil {