Skip to content

Commit 4275566

Browse files
fix: use line regarding file instead of chunk
1 parent c1df497 commit 4275566

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

engine/engine.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ type IEngine interface {
5454
const (
5555
customRegexRuleIdFormat = "custom-regex-%d"
5656
CxFileEndMarker = ";cx-file-end"
57+
totalLinesField = "totalLines"
58+
linesInChunkField = "linesInChunk"
5759
)
5860

5961
type EngineConfig struct {
@@ -109,7 +111,7 @@ func (e *Engine) DetectFragment(item plugins.ISourceItem, secretsChannel chan *s
109111
FilePath: item.GetSource(),
110112
}
111113

112-
return e.detectSecrets(item, fragment, secretsChannel, pluginName)
114+
return e.detectSecrets(context.Background(), item, fragment, secretsChannel, pluginName)
113115
}
114116

115117
// DetectFile reads the given file and detects secrets in it
@@ -137,7 +139,7 @@ func (e *Engine) DetectFile(ctx context.Context, item plugins.ISourceItem, secre
137139
}
138140
defer e.semaphore.ReleaseMemoryWeight(weight)
139141

140-
return e.detectChunks(item, secretsChannel)
142+
return e.detectChunks(ctx, item, secretsChannel)
141143
}
142144
// fileSize * 2 -> data file bytes and its conversion to string
143145
weight := fileSize * 2
@@ -156,11 +158,11 @@ func (e *Engine) DetectFile(ctx context.Context, item plugins.ISourceItem, secre
156158
FilePath: item.GetSource(),
157159
}
158160

159-
return e.detectSecrets(item, fragment, secretsChannel, "filesystem")
161+
return e.detectSecrets(ctx, item, fragment, secretsChannel, "filesystem")
160162
}
161163

162164
// detectChunks reads the given file in chunks and detects secrets in each chunk
163-
func (e *Engine) detectChunks(item plugins.ISourceItem, secretsChannel chan *secrets.Secret) error {
165+
func (e *Engine) detectChunks(ctx context.Context, item plugins.ISourceItem, secretsChannel chan *secrets.Secret) error {
164166
f, err := os.Open(item.GetSource())
165167
if err != nil {
166168
return fmt.Errorf("failed to open file %s: %w", item.GetSource(), err)
@@ -194,20 +196,22 @@ func (e *Engine) detectChunks(item plugins.ISourceItem, secretsChannel chan *sec
194196
Raw: chunkStr,
195197
FilePath: item.GetSource(),
196198
}
197-
if detectErr := e.detectSecrets(item, fragment, secretsChannel, "filesystem"); detectErr != nil {
199+
ctx = context.WithValue(ctx, totalLinesField, totalLines)
200+
ctx = context.WithValue(ctx, linesInChunkField, linesInChunk)
201+
if detectErr := e.detectSecrets(ctx, item, fragment, secretsChannel, "filesystem"); detectErr != nil {
198202
return fmt.Errorf("failed to detect secrets: %w", detectErr)
199203
}
200204
}
201205
}
202206

203207
// detectSecrets detects secrets and sends them to the secrets channel
204-
func (e *Engine) detectSecrets(item plugins.ISourceItem, fragment detect.Fragment, secrets chan *secrets.Secret,
208+
func (e *Engine) detectSecrets(ctx context.Context, item plugins.ISourceItem, fragment detect.Fragment, secrets chan *secrets.Secret,
205209
pluginName string) error {
206210
fragment.Raw += CxFileEndMarker + "\n"
207211

208212
values := e.detector.Detect(fragment)
209213
for _, value := range values {
210-
secret, buildErr := buildSecret(item, value, pluginName)
214+
secret, buildErr := buildSecret(ctx, item, value, pluginName)
211215
if buildErr != nil {
212216
return fmt.Errorf("failed to build secret: %w", buildErr)
213217
}
@@ -306,10 +310,10 @@ func GetRulesCommand(engineConfig *EngineConfig) *cobra.Command {
306310
}
307311

308312
// buildSecret creates a secret object from the given source item and finding
309-
func buildSecret(item plugins.ISourceItem, value report.Finding, pluginName string) (*secrets.Secret, error) {
313+
func buildSecret(ctx context.Context, item plugins.ISourceItem, value report.Finding, pluginName string) (*secrets.Secret, error) {
310314
gitInfo := item.GetGitInfo()
311315
itemId := getFindingId(item, value)
312-
startLine, endLine, err := getStartAndEndLines(pluginName, gitInfo, value)
316+
startLine, endLine, err := getStartAndEndLines(ctx, pluginName, gitInfo, value)
313317
if err != nil {
314318
return nil, fmt.Errorf("failed to get start and end lines for source %s: %w", item.GetSource(), err)
315319
}
@@ -342,13 +346,15 @@ func getFindingId(item plugins.ISourceItem, finding report.Finding) string {
342346
return fmt.Sprintf("%x", sha)
343347
}
344348

345-
func getStartAndEndLines(pluginName string, gitInfo *plugins.GitInfo, value report.Finding) (int, int, error) {
349+
func getStartAndEndLines(ctx context.Context, pluginName string, gitInfo *plugins.GitInfo, value report.Finding) (int, int, error) {
346350
var startLine, endLine int
347351
var err error
348352

349353
if pluginName == "filesystem" {
350-
startLine = value.StartLine + 1
351-
endLine = value.EndLine + 1
354+
totalLines := ctx.Value(totalLinesField).(int)
355+
linesInChunk := ctx.Value(linesInChunkField).(int)
356+
startLine = value.StartLine + (totalLines - linesInChunk) + 1
357+
endLine = value.EndLine + (totalLines - linesInChunk) + 1
352358
} else if pluginName == "git" {
353359
startLine, endLine, err = plugins.GetGitStartAndEndLine(gitInfo, value.StartLine, value.EndLine)
354360
if err != nil {

0 commit comments

Comments
 (0)