Skip to content

Commit

Permalink
excel style for info
Browse files Browse the repository at this point in the history
  • Loading branch information
babenek committed Jan 16, 2025
1 parent 51d8a02 commit 41927bb
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 20 deletions.
1 change: 0 additions & 1 deletion credsweeper/deep_scanner/deep_scanner.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import datetime
import logging
from pathlib import Path
from typing import List, Optional, Any, Tuple, Union

from credsweeper.common.constants import RECURSIVE_SCAN_LIMITATION
Expand Down
4 changes: 3 additions & 1 deletion credsweeper/deep_scanner/xlsx_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from credsweeper.deep_scanner.abstract_scanner import AbstractScanner
from credsweeper.file_handler.data_content_provider import DataContentProvider
from credsweeper.file_handler.string_content_provider import StringContentProvider
from credsweeper.utils import Util

logger = logging.getLogger(__name__)

Expand All @@ -32,10 +33,11 @@ def data_scan(
df = sheet_data.replace(to_replace="_x000D_\n", value='\n', regex=True).fillna('').astype(str)
for row_pos, row in enumerate(df.values):
for col_pos, cell in enumerate(row):
cell_info = f"{sheet_info}:{Util.get_excel_column_name(col_pos)}{row_pos + 1}"
cell_provider = StringContentProvider(lines=cell.splitlines(),
file_path=data_provider.file_path,
file_type=data_provider.file_type,
info=f"{sheet_info}:R{row_pos + 1}C{col_pos + 1}")
info=cell_info)
cell_candidates = self.scanner.scan(cell_provider)
candidates.extend(cell_candidates)
row_line = '\t'.join(row)
Expand Down
1 change: 0 additions & 1 deletion credsweeper/file_handler/data_content_provider.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import base64
import json
import logging
import string
Expand Down
11 changes: 11 additions & 0 deletions credsweeper/utils/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,3 +749,14 @@ def subtext(text: str, pos: int, hunk_size: int) -> str:
if 0 > left_pos:
left_pos = 0
return text[left_pos:right_pos].rstrip()

@staticmethod
def get_excel_column_name(column_index: int) -> str:
"""Converts index based column position into Excel style column name"""
name = ''
if isinstance(column_index, int):
while 0 <= column_index:
column_index, remain = divmod(column_index, 26)
name = f"{chr(ord('A') + remain)}{name}"
column_index -= 1
return name
2 changes: 1 addition & 1 deletion tests/data/depth_3.json
Original file line number Diff line number Diff line change
Expand Up @@ -11158,7 +11158,7 @@
"line": "A2 ID:master,PW:dipPr10Gg!",
"line_num": 1,
"path": "./tests/samples/sample.ods",
"info": "FILE|SheetAny:R2C1",
"info": "FILE|SheetAny:A2",
"value": "dipPr10Gg!",
"value_start": 16,
"value_end": 26,
Expand Down
32 changes: 16 additions & 16 deletions tests/data/doc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17764,7 +17764,7 @@
"line": "F16 224.52.124.93 root/A0dM1Nka",
"line_num": 1,
"path": "./tests/samples/sample.ods",
"info": "FILE|SheetAny:R16C6",
"info": "FILE|SheetAny:F16",
"value": "A0dM1Nka",
"value_start": 23,
"value_end": 31,
Expand All @@ -17789,7 +17789,7 @@
"line": "AKIAGIREOGIAXLSX4BT5",
"line_num": 1,
"path": "./tests/samples/sample.ods",
"info": "FILE|Sheet1x1:R1C1",
"info": "FILE|Sheet1x1:A1",
"value": "AKIAGIREOGIAXLSX4BT5",
"value_start": 0,
"value_end": 20,
Expand Down Expand Up @@ -17839,7 +17839,7 @@
"line": "B3 192.168.0.1 master/NBd@126t!",
"line_num": 1,
"path": "./tests/samples/sample.ods",
"info": "FILE|SheetAny:R3C2",
"info": "FILE|SheetAny:B3",
"value": "NBd@126t!",
"value_start": 22,
"value_end": 31,
Expand All @@ -17864,7 +17864,7 @@
"line": "A2 ID:master,PW:dipPr10Gg!",
"line_num": 1,
"path": "./tests/samples/sample.ods",
"info": "FILE|SheetAny:R2C1",
"info": "FILE|SheetAny:A2",
"value": "dipPr10Gg!",
"value_start": 16,
"value_end": 26,
Expand All @@ -17889,7 +17889,7 @@
"line": "A2 ID:master,PW:dipPr10Gg!",
"line_num": 1,
"path": "./tests/samples/sample.ods",
"info": "FILE|SheetAny:R2C1",
"info": "FILE|SheetAny:A2",
"value": "dipPr10Gg!",
"value_start": 16,
"value_end": 26,
Expand All @@ -17906,7 +17906,7 @@
"line": "A2 ID:master,PW:dipPr10Gg!",
"line_num": 1,
"path": "./tests/samples/sample.ods",
"info": "FILE|SheetAny:R2C1",
"info": "FILE|SheetAny:A2",
"value": "master,PW:dipPr10Gg!",
"value_start": 6,
"value_end": 26,
Expand All @@ -17931,7 +17931,7 @@
"line": "A2 ID:master,PW:dipPr10Gg!",
"line_num": 1,
"path": "./tests/samples/sample.ods",
"info": "FILE|SheetAny:R2C1",
"info": "FILE|SheetAny:A2",
"value": "dipPr10Gg!",
"value_start": 16,
"value_end": 26,
Expand All @@ -17956,7 +17956,7 @@
"line": "A1 password is w3Ry5tR0nG",
"line_num": 1,
"path": "./tests/samples/sample.ods",
"info": "FILE|SheetAny:R1C1",
"info": "FILE|SheetAny:A1",
"value": "w3Ry5tR0nG",
"value_start": 15,
"value_end": 25,
Expand Down Expand Up @@ -18081,7 +18081,7 @@
"line": "F16 224.52.124.93 root/A0dM1Nka",
"line_num": 1,
"path": "./tests/samples/sample.xlsx",
"info": "FILE|SheetAny:R16C6",
"info": "FILE|SheetAny:F16",
"value": "A0dM1Nka",
"value_start": 23,
"value_end": 31,
Expand All @@ -18106,7 +18106,7 @@
"line": "AKIAGIREOGIAXLSX4BT5",
"line_num": 1,
"path": "./tests/samples/sample.xlsx",
"info": "FILE|Sheet1x1:R1C1",
"info": "FILE|Sheet1x1:A1",
"value": "AKIAGIREOGIAXLSX4BT5",
"value_start": 0,
"value_end": 20,
Expand Down Expand Up @@ -18156,7 +18156,7 @@
"line": "B3 192.168.0.1 master/NBd@126t!",
"line_num": 1,
"path": "./tests/samples/sample.xlsx",
"info": "FILE|SheetAny:R3C2",
"info": "FILE|SheetAny:B3",
"value": "NBd@126t!",
"value_start": 22,
"value_end": 31,
Expand All @@ -18181,7 +18181,7 @@
"line": "A2 ID:master,PW:dipPr10Gg!",
"line_num": 1,
"path": "./tests/samples/sample.xlsx",
"info": "FILE|SheetAny:R2C1",
"info": "FILE|SheetAny:A2",
"value": "dipPr10Gg!",
"value_start": 16,
"value_end": 26,
Expand All @@ -18206,7 +18206,7 @@
"line": "A2 ID:master,PW:dipPr10Gg!",
"line_num": 1,
"path": "./tests/samples/sample.xlsx",
"info": "FILE|SheetAny:R2C1",
"info": "FILE|SheetAny:A2",
"value": "dipPr10Gg!",
"value_start": 16,
"value_end": 26,
Expand All @@ -18223,7 +18223,7 @@
"line": "A2 ID:master,PW:dipPr10Gg!",
"line_num": 1,
"path": "./tests/samples/sample.xlsx",
"info": "FILE|SheetAny:R2C1",
"info": "FILE|SheetAny:A2",
"value": "master,PW:dipPr10Gg!",
"value_start": 6,
"value_end": 26,
Expand All @@ -18248,7 +18248,7 @@
"line": "A2 ID:master,PW:dipPr10Gg!",
"line_num": 1,
"path": "./tests/samples/sample.xlsx",
"info": "FILE|SheetAny:R2C1",
"info": "FILE|SheetAny:A2",
"value": "dipPr10Gg!",
"value_start": 16,
"value_end": 26,
Expand All @@ -18273,7 +18273,7 @@
"line": "A1 password is w3Ry5tR0nG",
"line_num": 1,
"path": "./tests/samples/sample.xlsx",
"info": "FILE|SheetAny:R1C1",
"info": "FILE|SheetAny:A1",
"value": "w3Ry5tR0nG",
"value_start": 15,
"value_end": 25,
Expand Down
14 changes: 14 additions & 0 deletions tests/utils/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import tempfile
import unittest
from pathlib import Path
from xmlrpc.client import MAXINT

from lxml.etree import XMLSyntaxError

Expand Down Expand Up @@ -650,3 +651,16 @@ def test_xml_p(self):
Util.is_xml(
bytearray(b'\n<xml> far far away ') + bytearray(b'\n' * MAX_LINE_LENGTH) +
bytearray(b' long long ago </xml>')))

def test_get_excel_column_name_n(self):
self.assertFalse(Util.get_excel_column_name(None))
self.assertFalse(Util.get_excel_column_name(-1))
self.assertFalse(Util.get_excel_column_name(3.14))

def test_get_excel_column_name_p(self):
self.assertEqual("A", Util.get_excel_column_name(0))
self.assertEqual("AQ", Util.get_excel_column_name(42))
self.assertEqual("CS", Util.get_excel_column_name(96))
self.assertEqual("AAA", Util.get_excel_column_name(702))
self.assertEqual("XFD", Util.get_excel_column_name(16383))
self.assertEqual("FXSHRXX", Util.get_excel_column_name(MAXINT))

0 comments on commit 41927bb

Please sign in to comment.