Skip to content

Commit 0f4a495

Browse files
tylerwincecat-codeericwb
authored
Cannot seek stdin on pipe (#496)
* add namespaces for parent attributes * pylint formatting changes * added _Seeker for running seek on sys.stdin * Update node_visitor.py * Update general_hardcoded_password.py * Update general_hardcoded_password.py * pep8 fixes * added list handling for hard fname swaps * updated manager * maintaining list order * Update manager.py * Update manager.py * Update manager.py * Update issue.py * Update node_visitor.py * Update manager.py * Update issue.py * Update context.py * Update issue.py * Update manager.py * Update node_visitor.py * Update tester.py * Update issue.py * Update manager.py * Update context.py * Update node_visitor.py * Update manager.py Co-authored-by: wxu <[email protected]> Co-authored-by: Eric Brown <[email protected]>
1 parent 1691b93 commit 0f4a495

File tree

5 files changed

+37
-8
lines changed

5 files changed

+37
-8
lines changed

bandit/core/context.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,7 @@ def is_module_imported_like(self, module):
315315
@property
316316
def filename(self):
317317
return self._context.get("filename")
318+
319+
@property
320+
def file_data(self):
321+
return self._context.get("file_data")

bandit/core/issue.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def __init__(
9393
self.text = text
9494
self.ident = ident
9595
self.fname = ""
96+
self.fdata = None
9697
self.test = ""
9798
self.test_id = test_id
9899
self.lineno = lineno
@@ -171,9 +172,17 @@ def get_code(self, max_lines=3, tabbed=False):
171172
lmin = max(1, self.lineno - max_lines // 2)
172173
lmax = lmin + len(self.linerange) + max_lines - 1
173174

175+
if self.fname == "<stdin>":
176+
self.fdata.seek(0)
177+
for line_num in range(1, lmin):
178+
self.fdata.readline()
179+
174180
tmplt = "%i\t%s" if tabbed else "%i %s"
175181
for line in range(lmin, lmax):
176-
text = linecache.getline(self.fname, line)
182+
if self.fname == "<stdin>":
183+
text = self.fdata.readline()
184+
else:
185+
text = linecache.getline(self.fname, line)
177186

178187
if isinstance(text, bytes):
179188
text = text.decode("utf-8")

bandit/core/manager.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# SPDX-License-Identifier: Apache-2.0
55
import collections
66
import fnmatch
7+
import io
78
import json
89
import logging
910
import os
@@ -277,8 +278,12 @@ def run_tests(self):
277278
self._show_progress("%s.. " % count, flush=True)
278279
try:
279280
if fname == "-":
280-
sys.stdin = os.fdopen(sys.stdin.fileno(), "rb", 0)
281-
self._parse_file("<stdin>", sys.stdin, new_files_list)
281+
open_fd = os.fdopen(sys.stdin.fileno(), "rb", 0)
282+
fdata = io.BytesIO(open_fd.read())
283+
new_files_list = [
284+
"<stdin>" if x == "-" else x for x in new_files_list
285+
]
286+
self._parse_file("<stdin>", fdata, new_files_list)
282287
else:
283288
with open(fname, "rb") as fdata:
284289
self._parse_file(fname, fdata, new_files_list)
@@ -333,7 +338,7 @@ def _parse_file(self, fname, fdata, new_files_list):
333338

334339
except tokenize.TokenError:
335340
pass
336-
score = self._execute_ast_visitor(fname, data, nosec_lines)
341+
score = self._execute_ast_visitor(fname, fdata, data, nosec_lines)
337342
self.scores.append(score)
338343
self.metrics.count_issues(
339344
[
@@ -360,7 +365,7 @@ def _parse_file(self, fname, fdata, new_files_list):
360365
LOG.debug(" Exception string: %s", e)
361366
LOG.debug(" Exception traceback: %s", traceback.format_exc())
362367

363-
def _execute_ast_visitor(self, fname, data, nosec_lines):
368+
def _execute_ast_visitor(self, fname, fdata, data, nosec_lines):
364369
"""Execute AST parse on each file
365370
366371
:param fname: The name of the file being parsed
@@ -370,7 +375,13 @@ def _execute_ast_visitor(self, fname, data, nosec_lines):
370375
"""
371376
score = []
372377
res = b_node_visitor.BanditNodeVisitor(
373-
fname, self.b_ma, self.b_ts, self.debug, nosec_lines, self.metrics
378+
fname,
379+
fdata,
380+
self.b_ma,
381+
self.b_ts,
382+
self.debug,
383+
nosec_lines,
384+
self.metrics,
374385
)
375386

376387
score = res.process(data)

bandit/core/node_visitor.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515

1616

1717
class BanditNodeVisitor:
18-
def __init__(self, fname, metaast, testset, debug, nosec_lines, metrics):
18+
def __init__(
19+
self, fname, fdata, metaast, testset, debug, nosec_lines, metrics
20+
):
1921
self.debug = debug
2022
self.nosec_lines = nosec_lines
2123
self.seen = 0
@@ -25,6 +27,7 @@ def __init__(self, fname, metaast, testset, debug, nosec_lines, metrics):
2527
}
2628
self.depth = 0
2729
self.fname = fname
30+
self.fdata = fdata
2831
self.metaast = metaast
2932
self.testset = testset
3033
self.imports = set()
@@ -37,7 +40,7 @@ def __init__(self, fname, metaast, testset, debug, nosec_lines, metrics):
3740
try:
3841
self.namespace = b_utils.get_module_qualname_from_path(fname)
3942
except b_utils.InvalidModulePath:
40-
LOG.info(
43+
LOG.warning(
4144
"Unable to find qualified name for module: %s", self.fname
4245
)
4346
self.namespace = ""
@@ -214,6 +217,7 @@ def pre_visit(self, node):
214217
self.context["node"] = node
215218
self.context["linerange"] = b_utils.linerange_fix(node)
216219
self.context["filename"] = self.fname
220+
self.context["file_data"] = self.fdata
217221

218222
self.seen += 1
219223
LOG.debug(

bandit/core/tester.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def run_tests(self, raw_context, checktype):
6161
result.fname = temp_context["filename"].decode("utf-8")
6262
else:
6363
result.fname = temp_context["filename"]
64+
result.fdata = temp_context["file_data"]
6465

6566
if result.lineno is None:
6667
result.lineno = temp_context["lineno"]

0 commit comments

Comments
 (0)