Skip to content

Commit 9fbc396

Browse files
committed
fixup! fix(secret): skip non-seekable files during scanning
1 parent 92cc9f1 commit 9fbc396

File tree

3 files changed

+13
-21
lines changed

3 files changed

+13
-21
lines changed

ggshield/core/scan/file.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from ggshield.utils.files import ListFilesMode, is_path_binary, list_files, url_for_path
55

6-
from .scannable import NonSeekableFileError, Scannable
6+
from .scannable import Scannable
77

88

99
class File(Scannable):
@@ -30,16 +30,13 @@ def is_longer_than(self, max_utf8_encoded_size: int) -> bool:
3030
# We already have the encoded size, easy
3131
return self._utf8_encoded_size > max_utf8_encoded_size
3232

33-
try:
34-
with self.path.open("rb") as fp:
35-
(
36-
result,
37-
self._content,
38-
self._utf8_encoded_size,
39-
) = Scannable._is_file_longer_than(fp, max_utf8_encoded_size)
40-
return result
41-
except NonSeekableFileError:
42-
raise
33+
with self.path.open("rb") as fp:
34+
(
35+
result,
36+
self._content,
37+
self._utf8_encoded_size,
38+
) = Scannable._is_file_longer_than(fp, max_utf8_encoded_size)
39+
return result
4340

4441
def _read_content(self) -> None:
4542
if self._content is None:

ggshield/core/scan/scannable.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,12 @@ def _is_file_longer_than(
149149
Raises DecodeError if the file cannot be decoded.
150150
"""
151151
# Get the byte size
152-
assert fp.seekable()
152+
# Note: IOBase.seekable() returns True on some non-seekable files like /proc/self/mounts
153153
try:
154154
byte_size = fp.seek(0, SEEK_END)
155-
except OSError:
156-
raise NonSeekableFileError("File does not support seeking operations")
155+
fp.seek(0, SEEK_SET)
156+
except OSError as exc:
157+
raise NonSeekableFileError() from exc
157158

158159
if byte_size > max_utf8_encoded_size * UTF8_TO_WORSE_OTHER_ENCODING_RATIO:
159160
# Even if the file used the worst encoding (UTF-32), encoding the content of
@@ -162,10 +163,6 @@ def _is_file_longer_than(
162163
return True, None, None
163164

164165
# Determine the encoding
165-
try:
166-
fp.seek(0, SEEK_SET)
167-
except OSError:
168-
raise NonSeekableFileError("File does not support seeking operations")
169166
charset_matches = charset_normalizer.from_fp(fp)
170167
charset_match = charset_matches.best()
171168
if charset_match is None:

tests/unit/core/scan/test_scannable.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,5 @@ def test_file_non_seekable(mock_open, tmp_path):
5252
test_file.write_text("test content")
5353
file_obj = File(test_file)
5454

55-
with pytest.raises(
56-
NonSeekableFileError, match="File does not support seeking operations"
57-
):
55+
with pytest.raises(NonSeekableFileError):
5856
file_obj.is_longer_than(1000)

0 commit comments

Comments
 (0)