mirror of
https://github.com/goharbor/harbor
synced 2025-04-07 21:30:15 +00:00
Validate job ID when getting job log
Add validation to job ID in the API to get job log in job service, to prevent file path traversal attack. Signed-off-by: Daniel Jiang <jiangd@vmware.com>
This commit is contained in:
parent
880521518f
commit
65cf02a1d7
|
@ -7,14 +7,15 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"github.com/vmware/harbor/src/jobservice/opm"
|
"strings"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
|
||||||
"github.com/vmware/harbor/src/jobservice/core"
|
"github.com/vmware/harbor/src/jobservice/core"
|
||||||
"github.com/vmware/harbor/src/jobservice/errs"
|
"github.com/vmware/harbor/src/jobservice/errs"
|
||||||
"github.com/vmware/harbor/src/jobservice/models"
|
"github.com/vmware/harbor/src/jobservice/models"
|
||||||
|
"github.com/vmware/harbor/src/jobservice/opm"
|
||||||
)
|
)
|
||||||
|
|
||||||
//Handler defines approaches to handle the http requests.
|
//Handler defines approaches to handle the http requests.
|
||||||
|
@ -206,6 +207,11 @@ func (dh *DefaultHandler) HandleJobLogReq(w http.ResponseWriter, req *http.Reque
|
||||||
vars := mux.Vars(req)
|
vars := mux.Vars(req)
|
||||||
jobID := vars["job_id"]
|
jobID := vars["job_id"]
|
||||||
|
|
||||||
|
if strings.Contains(jobID, "..") || strings.ContainsRune(jobID, os.PathSeparator) {
|
||||||
|
dh.handleError(w, http.StatusBadRequest, fmt.Errorf("Invalid Job ID: %s", jobID))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
logData, err := dh.controller.GetJobLogData(jobID)
|
logData, err := dh.controller.GetJobLogData(jobID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
code := http.StatusInternalServerError
|
code := http.StatusInternalServerError
|
||||||
|
|
|
@ -227,6 +227,22 @@ func TestCheckStatus(t *testing.T) {
|
||||||
ctx.WG.Wait()
|
ctx.WG.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetJobLogInvalidID(t *testing.T) {
|
||||||
|
exportUISecret(fakeSecret)
|
||||||
|
|
||||||
|
server, port, ctx := createServer()
|
||||||
|
server.Start()
|
||||||
|
<-time.After(200 * time.Millisecond)
|
||||||
|
|
||||||
|
_, err := getReq(fmt.Sprintf("http://localhost:%d/api/v1/jobs/%%2F..%%2Fpasswd/log", port))
|
||||||
|
if err == nil || strings.Contains(err.Error(), "400") {
|
||||||
|
t.Fatalf("Expected 400 error but got: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
server.Stop()
|
||||||
|
ctx.WG.Wait()
|
||||||
|
}
|
||||||
|
|
||||||
func TestGetJobLog(t *testing.T) {
|
func TestGetJobLog(t *testing.T) {
|
||||||
exportUISecret(fakeSecret)
|
exportUISecret(fakeSecret)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user