github.com/pkumar631/talisman@v0.3.2/detector/filecontent_detector.go (about)

     1  package detector
     2  
     3  import (
     4  	"fmt"
     5  	log "github.com/Sirupsen/logrus"
     6  	"github.com/thoughtworks/talisman/git_repo"
     7  	"strings"
     8  )
     9  
    10  type FileContentDetector struct {
    11  	base64Detector *Base64Detector
    12  	hexDetector *HexDetector
    13  }
    14  
    15  func NewFileContentDetector() *FileContentDetector {
    16  	fc := FileContentDetector{}
    17  	fc.base64Detector = NewBase64Detector()
    18  	fc.hexDetector = NewHexDetector()
    19  	return &fc
    20  }
    21  
    22  func (fc *FileContentDetector) AggressiveMode() *FileContentDetector {
    23  	fc.base64Detector.aggressiveDetector = &Base64AggressiveDetector{}
    24  	return fc
    25  }
    26  
    27  
    28  func (fc *FileContentDetector) Test(additions []git_repo.Addition, ignores Ignores, result *DetectionResults) {
    29  	for _, addition := range additions {
    30  		if ignores.Deny(addition) {
    31  			log.WithFields(log.Fields{
    32  				"filePath": addition.Path,
    33  			}).Info("Ignoring addition as it was specified to be ignored.")
    34  			result.Ignore(addition.Path, fmt.Sprintf("%s was ignored by .talismanignore", addition.Path))
    35  			continue
    36  		}
    37  		base64Results := fc.detectFile(addition.Data)
    38  		fillDetectionResults(base64Results, addition, result)
    39  	}
    40  }
    41  
    42  func fillDetectionResults(base64Results []string, addition git_repo.Addition, result *DetectionResults) {
    43  	for _, base64Res := range base64Results {
    44  		if base64Res != "" {
    45  			log.WithFields(log.Fields{
    46  				"filePath": addition.Path,
    47  			}).Info("Failing file as it contains a base64 encoded text.")
    48  			result.Fail(addition.Path, fmt.Sprintf("Expected file to not to contain base64 or hex encoded texts such as: %s", base64Res))
    49  		}
    50  	}
    51  }
    52  
    53  func (fc *FileContentDetector) detectFile(data []byte) []string {
    54  	content := string(data)
    55  	return fc.checkEachLine(content)
    56  }
    57  
    58  func (fc *FileContentDetector) checkEachLine(content string) []string {
    59  	lines := strings.Split(content, "\n")
    60  	res := []string{}
    61  	for _, line := range lines {
    62  		lineResult := fc.checkEachWord(line)
    63  		if len(lineResult) > 0 {
    64  			res = append(res, lineResult...)
    65  		}
    66  	}
    67  	return res
    68  }
    69  
    70  func (fc *FileContentDetector) checkEachWord(line string) []string {
    71  	words := strings.Fields(line)
    72  	res := []string{}
    73  	for _, word := range words {
    74  		wordResult := fc.base64Detector.checkBase64Encoding(word)
    75  		if wordResult != "" {
    76  			res = append(res, wordResult)
    77  		}
    78  		wordResult = fc.hexDetector.checkHexEncoding(word)
    79  		if wordResult != "" {
    80  			res = append(res, wordResult)
    81  		}
    82  	}
    83  	return res
    84  }