-
Notifications
You must be signed in to change notification settings - Fork 23
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 square bracket checking to account for arbitrary nested parentheses #81
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,30 @@ | ||
import string | ||
import six | ||
|
||
|
||
def leading_characters(s, length): | ||
def brackets_check(pattern): | ||
""" | ||
Returns non-whitespace leading characters | ||
Check whether the pattern is missing square brackets, in a way which does | ||
not require the usual parsing. This is a light hack to provide an improved | ||
error message in this particular case. | ||
|
||
:param str s: The string to process | ||
:param int length: The number of characters to return | ||
:return: The non-whitespace leading characters | ||
:rtype: str or None | ||
:param pattern: A STIX pattern string | ||
:return: True if the pattern had its brackets; False if not | ||
""" | ||
if s is None: | ||
return None | ||
if isinstance(pattern, six.string_types): | ||
# the non-whitespace chars | ||
non_ws_chars = (c for c in pattern if not c.isspace()) | ||
|
||
stripped = [] | ||
for char in s: | ||
if char not in string.whitespace: | ||
stripped.append(char) | ||
# There can be an arbitrary number of open parens first... skip over those | ||
c = next(non_ws_chars, None) | ||
while c == "(": | ||
c = next(non_ws_chars, None) | ||
|
||
upper_bound = min(length, len(stripped)) | ||
return ''.join(stripped[:upper_bound]) | ||
if c == "[": | ||
result = True | ||
else: | ||
result = False | ||
|
||
else: | ||
result = False | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,33 @@ | ||
""" | ||
Test cases for stix2patterns/helpers.py. | ||
""" | ||
import pytest | ||
|
||
from stix2patterns.helpers import leading_characters | ||
from stix2patterns.helpers import brackets_check | ||
|
||
|
||
def test_leading_characters(): | ||
@pytest.mark.parametrize( | ||
"value", [ | ||
'[file:size = 1280]', | ||
' [file:size = 1280]', | ||
'( [file:size = 1280])', | ||
'( ( [file:size = 1280]) )', | ||
'(( ( ( [file:size = 1280])) ))', | ||
'[', | ||
], | ||
) | ||
def test_brackets_check(value): | ||
assert brackets_check(value) | ||
|
||
assert leading_characters('[file:size = 1280]', 2) == '[f' | ||
assert leading_characters(' [file:size = 1280]', 2) == '[f' | ||
assert leading_characters('( [file:size = 1280])', 2) == '([' | ||
assert leading_characters('[', 2) == '[' | ||
assert leading_characters(None, 2) is None | ||
|
||
@pytest.mark.parametrize( | ||
"value", [ | ||
None, | ||
"file:size = 1280", | ||
"(file:size = 1280)", | ||
" ( file:size = 1280 ) ", | ||
" (( (( file:size = 1280 ) )) ) ", | ||
] | ||
) | ||
def test_brackets_check_fail(value): | ||
assert not brackets_check(None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import re | ||
|
||
import stix2patterns.inspector | ||
|
||
HASHES_REGEX = { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import re | ||
|
||
import stix2patterns.inspector | ||
|
||
HASHES_REGEX = { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be simpler to treat open parens and whitespace chars equally? Can we just check
while c == "(" or c.isspace():
here instead of creatingnon_ws_chars
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah... that would work too. If anything more complex were happening, it might make more sense to separate the whitespace skipping from the other processing, but it does seem kinda silly here since it's so simple. I'll change it.