Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot seek stdin on pipe #496

Merged
merged 40 commits into from Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
272fd74
add namespaces for parent attributes
tylerwince May 13, 2019
7a5e827
pylint formatting changes
tylerwince May 13, 2019
583149a
added _Seeker for running seek on sys.stdin
tylerwince May 15, 2019
cf4f11a
Merge branch 'master' into 495-cannot-seek-stdin-on-pipe
tylerwince May 15, 2019
acd80a7
Update node_visitor.py
tylerwince May 15, 2019
edcecbe
Update general_hardcoded_password.py
tylerwince May 15, 2019
ca5cfe3
Update general_hardcoded_password.py
tylerwince May 15, 2019
6a33c6e
pep8 fixes
tylerwince May 15, 2019
91543c2
added list handling for hard fname swaps
cat-code May 15, 2019
969e816
updated manager
tylerwince May 15, 2019
6535101
maintaining list order
cat-code May 15, 2019
10c6378
Merge pull request #1 from JuanHuaXu/495-cannot-seek-stdin-on-pipe
tylerwince May 15, 2019
9f99d63
updated pep8 errors
tylerwince May 16, 2019
c9bb2b5
Merge branch 'master' into 495-cannot-seek-stdin-on-pipe
tylerwince May 27, 2019
8305291
Merge branch 'master' into 495-cannot-seek-stdin-on-pipe
tylerwince Jun 25, 2019
132e1c2
Merge branch 'master' into 495-cannot-seek-stdin-on-pipe
tylerwince Aug 2, 2019
c88dcac
Merge branch 'master' into 495-cannot-seek-stdin-on-pipe
ericwb Aug 18, 2019
ce523ba
Merge branch 'master' into 495-cannot-seek-stdin-on-pipe
tylerwince Oct 8, 2019
7b62387
Merge branch 'master' into 495-cannot-seek-stdin-on-pipe
ericwb Jan 12, 2020
c4dda4d
Merge branch 'master' into 495-cannot-seek-stdin-on-pipe
ericwb Jan 21, 2020
235acee
Merge branch 'master' into 495-cannot-seek-stdin-on-pipe
ericwb Mar 8, 2020
8038c11
Merge branch 'main' into 495-cannot-seek-stdin-on-pipe
ericwb Feb 14, 2022
fe741b4
Update manager.py
ericwb Feb 14, 2022
b035898
Update manager.py
ericwb Feb 14, 2022
f0e8316
Update manager.py
ericwb Feb 16, 2022
f0106e4
Update issue.py
ericwb Feb 16, 2022
e27cb8a
Update node_visitor.py
ericwb Feb 16, 2022
410a7cf
Update manager.py
ericwb Feb 16, 2022
0b43d58
Update issue.py
ericwb Feb 16, 2022
3f0139e
Update context.py
ericwb Feb 17, 2022
128fb4e
Update issue.py
ericwb Feb 17, 2022
217fe52
Update manager.py
ericwb Feb 17, 2022
1a9b389
Update node_visitor.py
ericwb Feb 17, 2022
4ab2d4a
Update tester.py
ericwb Feb 17, 2022
c42e501
Update issue.py
ericwb Feb 17, 2022
e583627
Update manager.py
ericwb Feb 17, 2022
86ffba3
Update context.py
ericwb Feb 17, 2022
a5e44ba
Update node_visitor.py
ericwb Feb 17, 2022
2e9b2d4
Update manager.py
ericwb Feb 17, 2022
6562a4d
Merge branch 'main' into 495-cannot-seek-stdin-on-pipe
tylerwince Feb 17, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions bandit/core/context.py
Expand Up @@ -315,3 +315,7 @@ def is_module_imported_like(self, module):
@property
def filename(self):
return self._context.get("filename")

@property
def file_data(self):
return self._context.get("file_data")
11 changes: 10 additions & 1 deletion bandit/core/issue.py
Expand Up @@ -93,6 +93,7 @@ def __init__(
self.text = text
self.ident = ident
self.fname = ""
self.fdata = None
self.test = ""
self.test_id = test_id
self.lineno = lineno
Expand Down Expand Up @@ -171,9 +172,17 @@ def get_code(self, max_lines=3, tabbed=False):
lmin = max(1, self.lineno - max_lines // 2)
lmax = lmin + len(self.linerange) + max_lines - 1

if self.fname == "<stdin>":
self.fdata.seek(0)
for line_num in range(1, lmin):
self.fdata.readline()

tmplt = "%i\t%s" if tabbed else "%i %s"
for line in range(lmin, lmax):
text = linecache.getline(self.fname, line)
if self.fname == "<stdin>":
text = self.fdata.readline()
else:
text = linecache.getline(self.fname, line)

if isinstance(text, bytes):
text = text.decode("utf-8")
Expand Down
21 changes: 16 additions & 5 deletions bandit/core/manager.py
Expand Up @@ -4,6 +4,7 @@
# SPDX-License-Identifier: Apache-2.0
import collections
import fnmatch
import io
import json
import logging
import os
Expand Down Expand Up @@ -269,8 +270,12 @@ def run_tests(self):
self._show_progress("%s.. " % count, flush=True)
try:
if fname == "-":
sys.stdin = os.fdopen(sys.stdin.fileno(), "rb", 0)
self._parse_file("<stdin>", sys.stdin, new_files_list)
open_fd = os.fdopen(sys.stdin.fileno(), "rb", 0)
fdata = io.BytesIO(open_fd.read())
new_files_list = [
"<stdin>" if x == "-" else x for x in new_files_list
]
self._parse_file("<stdin>", fdata, new_files_list)
else:
with open(fname, "rb") as fdata:
self._parse_file(fname, fdata, new_files_list)
Expand Down Expand Up @@ -325,7 +330,7 @@ def _parse_file(self, fname, fdata, new_files_list):

except tokenize.TokenError:
pass
score = self._execute_ast_visitor(fname, data, nosec_lines)
score = self._execute_ast_visitor(fname, fdata, data, nosec_lines)
self.scores.append(score)
self.metrics.count_issues(
[
Expand All @@ -352,7 +357,7 @@ def _parse_file(self, fname, fdata, new_files_list):
LOG.debug(" Exception string: %s", e)
LOG.debug(" Exception traceback: %s", traceback.format_exc())

def _execute_ast_visitor(self, fname, data, nosec_lines):
def _execute_ast_visitor(self, fname, fdata, data, nosec_lines):
"""Execute AST parse on each file

:param fname: The name of the file being parsed
Expand All @@ -362,7 +367,13 @@ def _execute_ast_visitor(self, fname, data, nosec_lines):
"""
score = []
res = b_node_visitor.BanditNodeVisitor(
fname, self.b_ma, self.b_ts, self.debug, nosec_lines, self.metrics
fname,
fdata,
self.b_ma,
self.b_ts,
self.debug,
nosec_lines,
self.metrics,
)

score = res.process(data)
Expand Down
8 changes: 6 additions & 2 deletions bandit/core/node_visitor.py
Expand Up @@ -15,7 +15,9 @@


class BanditNodeVisitor:
def __init__(self, fname, metaast, testset, debug, nosec_lines, metrics):
def __init__(
self, fname, fdata, metaast, testset, debug, nosec_lines, metrics
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bump for visibility, please see the number of issues that are being linked to this PR. This change to BanditNodeVisitor, which introduces new positional argument fdata in the middle of the signature, causes a backwards-incompatible break with flake8-bandit, which uses this object.

Ideally, this new argument should have been added to the end of the signature as an optional kwarg. Barring that, as small as this change is, this is now more than a patch update: I think this should have been released as 1.8.0 instead of 1.7.3.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bandit doesn't guarantee stability of its internals. Tools reaching in and trying to use them will break much like tools that reach directly into Flake8 for things they shouldn't

):
self.debug = debug
self.nosec_lines = nosec_lines
self.seen = 0
Expand All @@ -25,6 +27,7 @@ def __init__(self, fname, metaast, testset, debug, nosec_lines, metrics):
}
self.depth = 0
self.fname = fname
self.fdata = fdata
self.metaast = metaast
self.testset = testset
self.imports = set()
Expand All @@ -37,7 +40,7 @@ def __init__(self, fname, metaast, testset, debug, nosec_lines, metrics):
try:
self.namespace = b_utils.get_module_qualname_from_path(fname)
except b_utils.InvalidModulePath:
LOG.info(
LOG.warning(
"Unable to find qualified name for module: %s", self.fname
)
self.namespace = ""
Expand Down Expand Up @@ -214,6 +217,7 @@ def pre_visit(self, node):
self.context["node"] = node
self.context["linerange"] = b_utils.linerange_fix(node)
self.context["filename"] = self.fname
self.context["file_data"] = self.fdata

self.seen += 1
LOG.debug(
Expand Down
1 change: 1 addition & 0 deletions bandit/core/tester.py
Expand Up @@ -61,6 +61,7 @@ def run_tests(self, raw_context, checktype):
result.fname = temp_context["filename"].decode("utf-8")
else:
result.fname = temp_context["filename"]
result.fdata = temp_context["file_data"]

if result.lineno is None:
result.lineno = temp_context["lineno"]
Expand Down