Skip to content

Commit

Permalink
Take file extension from uploaded file (#593)
Browse files Browse the repository at this point in the history
This adds support for scanning yml, json, etc.
Slight security issue here taking file type from caller, but that issue already exists
and I haven't fully evaluated the risk there...
  • Loading branch information
jlk committed Mar 9, 2021
1 parent a3bcac7 commit 9546bcc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pkg/http-server/file-scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"io/ioutil"
"net/http"
"os"
"path"
"strconv"
"strings"

Expand Down Expand Up @@ -63,12 +64,17 @@ func (g *APIHandler) scanFile(w http.ResponseWriter, r *http.Request) {
}
defer file.Close()

// fileExtension will include the period. (eg ".yaml")
fileExtension := path.Ext(handler.Filename)

zap.S().Debugf("uploaded file: %+v", handler.Filename)
zap.S().Debugf("uploaded file extension: %+v", fileExtension)
zap.S().Debugf("file size: %+v", handler.Size)
zap.S().Debugf("MIME header: %+v", handler.Header)

// Create a temporary file within temp directory
tempFile, err := ioutil.TempFile("", "terrascan-*.tf")
tempFileTemplate := fmt.Sprintf("terrascan-*%s", fileExtension)
tempFile, err := ioutil.TempFile("", tempFileTemplate)
if err != nil {
errMsg := fmt.Sprintf("failed to create temp file. error: '%v'", err)
zap.S().Error(errMsg)
Expand Down
16 changes: 16 additions & 0 deletions pkg/http-server/file-scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,22 @@ func TestUpload(t *testing.T) {
invalidShowPassed: true,
wantStatus: http.StatusBadRequest,
},
{
name: "scan valid kubernetes yaml",
path: "../iac-providers/kubernetes/v1/testdata/yaml-extension2/test_pod.yml",
param: testParamName,
iacType: "k8s",
cloudType: testCloudType,
wantStatus: http.StatusOK,
},
{
name: "scan valid tfplan json",
path: "../iac-providers/tfplan/v1/testdata/valid-tfplan.json",
param: testParamName,
iacType: "tfplan",
cloudType: testCloudType,
wantStatus: http.StatusOK,
},
}

for _, tt := range table {
Expand Down

0 comments on commit 9546bcc

Please sign in to comment.