Skip to content

Commit 40290cc

Browse files
authored
fix private key line number (#231)
1 parent a3b7929 commit 40290cc

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

detect_secrets/plugins/private_key.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,24 @@ def analyze_line(
9797
self._analyzed_files.add(filename)
9898
file_content = self.read_file(filename)
9999
if file_content:
100-
output.update(
101-
super().analyze_line(
102-
filename=filename, line=file_content, line_number=1,
103-
context=context, raw_context=raw_context, **kwargs,
104-
),
100+
found_secrets = super().analyze_line(
101+
filename=filename, line=file_content, line_number=1,
102+
context=context, raw_context=raw_context, **kwargs,
105103
)
104+
updated_secrets = set()
105+
for sec in found_secrets:
106+
secret_val = sec.secret_value or ''
107+
line_number = self.find_line_number(file_content, secret_val)
108+
updated_secrets.add(
109+
PotentialSecret(
110+
type=self.secret_type,
111+
filename=sec.filename,
112+
secret=secret_val,
113+
line_number=line_number,
114+
is_verified=sec.is_verified,
115+
),
116+
)
117+
output.update(updated_secrets)
106118
return output
107119

108120
def analyze_string(self, string: str) -> Generator[str, None, None]:
@@ -129,3 +141,15 @@ def get_file_size(self, file_path: str) -> int:
129141
return os.path.getsize(file_path)
130142
except Exception:
131143
return -1
144+
145+
def find_line_number(
146+
self, file_content: str, substring: str, default_line_number: int = 1,
147+
) -> int:
148+
if len(substring) == 0:
149+
return default_line_number
150+
lines = file_content.splitlines()
151+
152+
for line_number, line in enumerate(lines, start=1):
153+
if substring in line:
154+
return line_number
155+
return default_line_number

0 commit comments

Comments
 (0)