Skip to content

Commit

Permalink
[Issue #220] - dcRUSTy - Implement utility function wrapper for iouti…
Browse files Browse the repository at this point in the history
…l.ReadFile which skips following symlink
  • Loading branch information
dcRUSTy committed Aug 12, 2020
1 parent a917db6 commit 56cf563
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
4 changes: 2 additions & 2 deletions directory_hook.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"io/ioutil"
"talisman/gitrepo"
"talisman/utility"

log "github.com/Sirupsen/logrus"

Expand Down Expand Up @@ -35,5 +35,5 @@ func (p *DirectoryHook) GetFilesFromDirectory(globPattern string) []gitrepo.Addi

func ReadFile(filepath string) ([]byte, error) {
log.Debugf("reading file %s", filepath)
return ioutil.ReadFile(filepath)
return utility.SafeReadFile(filepath)
}
4 changes: 2 additions & 2 deletions gitrepo/gitrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package gitrepo
import (
"fmt"
log "github.com/Sirupsen/logrus"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"strings"
"talisman/utility"
)

//FilePath represents the absolute path of an added file
Expand Down Expand Up @@ -161,7 +161,7 @@ func NewScannerAddition(filePath string, commits []string, content []byte) Addit
func (repo GitRepo) ReadRepoFile(fileName string) ([]byte, error) {
path := filepath.Join(repo.root, fileName)
log.Debugf("reading file %s", path)
return ioutil.ReadFile(path)
return utility.SafeReadFile(path)
}

//ReadRepoFileOrNothing returns the contents of the supplied relative filename by locating it in the git repo.
Expand Down
3 changes: 1 addition & 2 deletions utility/sha_256_hasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package utility
import (
"crypto/sha256"
"encoding/hex"
"io/ioutil"
)

type SHA256Hasher interface {
Expand All @@ -20,7 +19,7 @@ func (DefaultSHA256Hasher) CollectiveSHA256Hash(paths []string) string {
concatBytes := hashByte(&sbyte)
nameByte := []byte(path)
nameHash := hashByte(&nameByte)
fileBytes, _ := ioutil.ReadFile(path)
fileBytes, _ := SafeReadFile(path)
fileHash := hashByte(&fileBytes)
finHash = concatBytes + fileHash + nameHash
}
Expand Down
15 changes: 15 additions & 0 deletions utility/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,18 @@ func Dir(src string, dst string) error {
}
return nil
}

func IsFileSymlink(path string) bool {
fileMetadata, err := os.Lstat(path)
if err != nil {
return false
}
return fileMetadata.Mode()&os.ModeSymlink != 0
}

func SafeReadFile(path string) ([]byte, error) {
if IsFileSymlink(path) {
return []byte{}, nil
}
return ioutil.ReadFile(path)
}

0 comments on commit 56cf563

Please sign in to comment.