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

Fix 'get_code' method if issue is generated from dict #568

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

e0ne
Copy link

@e0ne e0ne commented Jan 23, 2020

If issue is generated from dict 'get_code' method doesn't return
code source. This patch fixes this by returning self.code property.

If issue is generated from dict 'get_code' method doesn't return
code source. This patch fixes this by returning self.code property.
@lukehinds
Copy link
Member

Is there a related issue to this to help us get some insight into what this fixes or how to test the change?

@e0ne
Copy link
Author

e0ne commented Feb 7, 2020

@lukehinds It was an issue with serialization-deserialization of bandit issues. We do some automation on top of bandit and to_dict/from_dict are regular operations. I can provide an example of how to reproduce the issue if needed.

@lukehinds
Copy link
Member

yes please @e0ne

@e0ne
Copy link
Author

e0ne commented Feb 7, 2020

here is a code to reproduce the bug:

from bandit import core

d = {'filename': 'horizon/forms/fields.py',
 'test_name': 'django_mark_safe',
 'test_id': 'B703',
 'issue_severity': 'MEDIUM',
 'issue_confidence': 'HIGH',
 'issue_text': 'Potential XSS on mark_safe function.',
 'line_number': 236,
 'line_range': [236],
 'code': "235         output.append('</select>')\n236         return mark_safe('\\n'.join(output))\n237 \n"}

i = core.issue_from_dict(d)
print(i.code)
print(i.as_dict())

Output:

235         output.append('</select>')
236         return mark_safe('\n'.join(output))
237

{'filename': 'horizon/forms/fields.py', 'test_name': 'django_mark_safe', 'test_id': 'B703', 'issue_severity': 'MEDIUM', 'issue_confidence': 'HIGH', 'issue_text': 'Potential XSS on mark_safe function.', 'line_number': 236, 'line_range': [236], 'code': ''}

As I understood, it's reproducible only when linecache can't load the file.

@e0ne
Copy link
Author

e0ne commented Feb 7, 2020

@lukehinds as you can see, as_dict method dumps the issue without code

@lukehinds lukehinds self-requested a review February 26, 2020 21:20
Copy link
Member

@ericwb ericwb left a comment

Choose a reason for hiding this comment

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

Please add a unit test

Copy link
Member

@ericwb ericwb left a comment

Choose a reason for hiding this comment

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

The get_code() and issue_from_dict() functions are really there for internal calls of Bandit itself and not designed to be external interfaces to use more generally. We do have some interfaces for 3rd parties to create formatters and plugins, but the example given would not be a generally be necessary I would think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants