1
1
from __future__ import print_function
2
2
from __future__ import unicode_literals
3
3
4
- import codecs
5
- import itertools
6
4
import json
7
5
import os
8
6
import subprocess
15
13
from ..plugins .high_entropy_strings import HighEntropyStringsPlugin
16
14
from .baseline import merge_results
17
15
from .bidirectional_iterator import BidirectionalIterator
16
+ from .code_snippet import CodeSnippetHighlighter
18
17
from .color import AnsiColor
19
18
from .color import colorize
20
19
from .common import write_baseline_to_file
@@ -454,92 +453,59 @@ def _get_secret_with_context(
454
453
455
454
:raises: SecretNotFoundOnSpecifiedLineError
456
455
"""
457
- secret_line_idx = secret ['line_number' ] - 1
458
- end_line = secret_line_idx + lines_of_context + 1
459
-
460
- if secret_line_idx <= lines_of_context :
461
- start_line = 0
462
- index_of_secret_in_output = secret_line_idx
463
- else :
464
- start_line = secret_line_idx - lines_of_context
465
- index_of_secret_in_output = lines_of_context
466
-
467
- with codecs .open (filename , encoding = 'utf-8' ) as file :
468
- output = list (
469
- itertools .islice (
470
- file .read ().splitlines (),
471
- start_line ,
472
- end_line ,
473
- ),
474
- )
456
+ snippet = CodeSnippetHighlighter ().get_code_snippet (
457
+ filename ,
458
+ secret ['line_number' ],
459
+ lines_of_context = lines_of_context ,
460
+ )
475
461
476
462
try :
477
- output [index_of_secret_in_output ] = _highlight_secret (
478
- output [index_of_secret_in_output ],
479
- secret ['line_number' ],
463
+ raw_secret_value = get_raw_secret_value (
464
+ snippet .target_line ,
480
465
secret ,
481
- filename ,
482
466
plugin_settings ,
467
+ filename ,
483
468
)
469
+
470
+ snippet .highlight_line (raw_secret_value )
484
471
except SecretNotFoundOnSpecifiedLineError :
485
472
if not force :
486
473
raise
487
474
488
- output [index_of_secret_in_output ] = '{}' .format (
489
- colorize (
490
- output [index_of_secret_in_output ],
491
- AnsiColor .BOLD ,
492
- ),
475
+ snippet .target_line = colorize (
476
+ snippet .target_line ,
477
+ AnsiColor .BOLD ,
493
478
)
494
479
495
- # Adding line numbers
496
- return '\n ' .join (
497
- map (
498
- lambda x : '{}:{}' .format (
499
- colorize (
500
- str (int (x [0 ]) + start_line + 1 ),
501
- AnsiColor .LIGHT_GREEN ,
502
- ),
503
- x [1 ],
504
- ),
505
- enumerate (output ),
506
- ),
507
- )
480
+ return str (snippet .add_line_numbers ())
508
481
509
482
510
- def _highlight_secret (
483
+ def get_raw_secret_value (
511
484
secret_line ,
512
- secret_lineno ,
513
485
secret ,
514
- filename ,
515
486
plugin_settings ,
487
+ filename ,
516
488
):
517
489
"""
518
490
:type secret_line: str
519
491
:param secret_line: the line on which the secret is found
520
492
521
- :type secret_lineno: int
522
- :param secret_lineno: secret_line's line number in the source file
523
-
524
493
:type secret: dict
525
494
:param secret: see caller's docstring
526
495
527
- :type filename: str
528
- :param filename: this is needed, because PotentialSecret uses this
529
- as a means of comparing whether two secrets are equal.
530
-
531
496
:type plugin_settings: list
532
497
:param plugin_settings: see caller's docstring
533
498
534
- :rtype: str
535
- :returns: secret_line, but with the actual secret highlighted.
499
+ :type filename: str
500
+ :param filename: this is needed, because PotentialSecret uses this
501
+ as a means of comparing whether two secrets are equal.
536
502
"""
537
503
plugin = initialize .from_secret_type (
538
504
secret ['type' ],
539
505
plugin_settings ,
540
506
)
541
507
542
- for raw_secret in _raw_secret_generator (
508
+ for raw_secret in raw_secret_generator (
543
509
plugin ,
544
510
secret_line ,
545
511
filetype = determine_file_type (filename ),
@@ -553,26 +519,18 @@ def _highlight_secret(
553
519
# There could be more than two secrets on the same line.
554
520
# We only want to highlight the right one.
555
521
if secret_obj .secret_hash == secret ['hashed_secret' ]:
556
- break
522
+ return raw_secret
557
523
else :
558
- raise SecretNotFoundOnSpecifiedLineError (secret_lineno )
559
-
560
- index_of_secret = secret_line .lower ().index (raw_secret .lower ())
561
- end_of_secret = index_of_secret + len (raw_secret )
562
- return '{}{}{}' .format (
563
- secret_line [:index_of_secret ],
564
- colorize (
565
- # copy the secret out of the line because .lower() from secret
566
- # generator may be different from the original value:
567
- secret_line [index_of_secret :end_of_secret ],
568
- AnsiColor .RED_BACKGROUND ,
569
- ),
570
- secret_line [index_of_secret + len (raw_secret ):],
571
- )
524
+ raise SecretNotFoundOnSpecifiedLineError (secret ['line_number' ])
572
525
573
526
574
- def _raw_secret_generator (plugin , secret_line , filetype ):
575
- """Generates raw secrets by re-scanning the line, with the specified plugin"""
527
+ def raw_secret_generator (plugin , secret_line , filetype ):
528
+ """Generates raw secrets by re-scanning the line, with the specified plugin
529
+
530
+ :type plugin: BasePlugin
531
+ :type secret_line: str
532
+ :type filetype: FileType
533
+ """
576
534
for raw_secret in plugin .secret_generator (secret_line , filetype = filetype ):
577
535
yield raw_secret
578
536
0 commit comments