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

Change: Various script_xref() related extensions / modifications. #642

Merged
merged 6 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion tests/plugins/test_script_calls_empty_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def test_missing_values(self):
content = (
' script_category("");\n'
' script_xref(name: "URL",value:"");\n'
' script_xref(name:"URL", value:"");\n'
' script_tag(name:"", value:"");\n'
)
fake_context = self.create_file_plugin_context(
Expand All @@ -65,5 +66,5 @@ def test_missing_values(self):

results = list(plugin.run())

self.assertEqual(len(results), 2)
self.assertEqual(len(results), 3)
self.assertIsInstance(results[0], LinterError)
89 changes: 89 additions & 0 deletions tests/plugins/test_script_tag_whitespaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def test_ok(self):
' script_tag(name:"insight", value:"bar foo");\n'
' script_tag(name:"impact", value:"- foo\n - bar");\n'
' script_tag(name:"affected", value:"foo\n bar");\n'
' script_xref(name:"foo1", value:"foo\nbar");\n'
' script_xref(name:"foo2", value:"bar foo");\n'
' script_xref(name:"foo3", value:"- foo\n - bar");\n'
' script_xref(name:"foo4", value:"foo\n bar");\n'
)
fake_context = self.create_file_plugin_context(
nasl_file=self.path, file_content=content
Expand Down Expand Up @@ -85,6 +89,23 @@ def test_script_name_leading_whitespace(self):
results[0].message,
)

def test_script_xref_leading_whitespace(self):
content = ' script_xref(name:"foo", value:" bar");\n'
fake_context = self.create_file_plugin_context(
nasl_file=self.path, file_content=content
)
plugin = CheckScriptTagWhitespaces(fake_context)

results = list(plugin.run())

self.assertEqual(len(results), 1)
self.assertIsInstance(results[0], LinterError)
self.assertEqual(
'script_xref(name:"foo", value:" bar");: value contains a'
" leading or trailing whitespace character",
results[0].message,
)

def test_script_tag_trailing_whitespace(self):
content = ' script_tag(name:"insight", value:"bar ");\n'
fake_context = self.create_file_plugin_context(
Expand All @@ -109,6 +130,23 @@ def test_script_name_trailing_whitespace(self):
self.assertEqual(len(results), 1)
self.assertIsInstance(results[0], LinterError)

def test_script_xref_trailing_whitespace(self):
content = ' script_xref(name:"foo", value:"bar ");\n'
fake_context = self.create_file_plugin_context(
nasl_file=self.path, file_content=content
)
plugin = CheckScriptTagWhitespaces(fake_context)

results = list(plugin.run())

self.assertEqual(len(results), 1)
self.assertIsInstance(results[0], LinterError)
self.assertEqual(
'script_xref(name:"foo", value:"bar ");: value contains a'
" leading or trailing whitespace character",
results[0].message,
)

# nb: The script_name() tag is not allowed to contain newlines (checked in a
# dedicated plugin) so no specific test cases have been added here.
def test_script_tag_trailing_newline(self):
Expand Down Expand Up @@ -158,3 +196,54 @@ def test_script_tag_trailing_newline_with_newline_and_spaces(self):

self.assertEqual(len(results), 1)
self.assertIsInstance(results[0], LinterError)

# nb: The value of script_xref(name:"URL" are also checked separately in
# script_xref_url() and that one would also report trailing / leading
# newlines and similar for that specific case
def test_script_xref_trailing_newline(self):
content = ' script_xref(name:"foo", value:"bar\n");\n'
fake_context = self.create_file_plugin_context(
nasl_file=self.path, file_content=content
)
plugin = CheckScriptTagWhitespaces(fake_context)

results = list(plugin.run())

self.assertEqual(len(results), 1)
self.assertIsInstance(results[0], LinterError)

def test_script_xref_trailing_newline_with_space(self):
content = ' script_xref(name:"foo", value:"foo bar\n");\n'
fake_context = self.create_file_plugin_context(
nasl_file=self.path, file_content=content
)
plugin = CheckScriptTagWhitespaces(fake_context)

results = list(plugin.run())

self.assertEqual(len(results), 1)
self.assertIsInstance(results[0], LinterError)

def test_script_xref_trailing_newline_with_newline(self):
content = ' script_xref(name:"foo", value:"foo\nbar\n");\n'
fake_context = self.create_file_plugin_context(
nasl_file=self.path, file_content=content
)
plugin = CheckScriptTagWhitespaces(fake_context)

results = list(plugin.run())

self.assertEqual(len(results), 1)
self.assertIsInstance(results[0], LinterError)

def test_script_xref_trailing_newline_with_newline_and_spaces(self):
content = ' script_xref(name:"foo", value:"foo\n bar\n");\n'
fake_context = self.create_file_plugin_context(
nasl_file=self.path, file_content=content
)
plugin = CheckScriptTagWhitespaces(fake_context)

results = list(plugin.run())

self.assertEqual(len(results), 1)
self.assertIsInstance(results[0], LinterError)
26 changes: 25 additions & 1 deletion tests/plugins/test_script_xref_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_wrong_name(self):
self.assertIsInstance(results[0], LinterError)
self.assertEqual(
'script_xref(nammmme: "foo", value:"bar");: does not conform to'
' script_xref(name:"<name>", value:<value>);',
' script_xref(name:"<name>", value:"<value>");',
results[0].message,
)

Expand All @@ -85,3 +85,27 @@ def test_wrong_missing_parameters(self):

self.assertEqual(len(results), 1)
self.assertIsInstance(results[0], LinterError)

def test_wrong_missing_name_parameter(self):
content = ' script_xref("foo", value:"bar");\n'
fake_context = self.create_file_plugin_context(
nasl_file=self.path, file_content=content
)
plugin = CheckScriptXrefForm(fake_context)

results = list(plugin.run())

self.assertEqual(len(results), 1)
self.assertIsInstance(results[0], LinterError)

def test_wrong_missing_value_parameter(self):
content = ' script_xref(name:"foo", "bar");\n'
fake_context = self.create_file_plugin_context(
nasl_file=self.path, file_content=content
)
plugin = CheckScriptXrefForm(fake_context)

results = list(plugin.run())

self.assertEqual(len(results), 1)
self.assertIsInstance(results[0], LinterError)
23 changes: 7 additions & 16 deletions troubadix/helper/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,33 +142,30 @@ def get_script_tag_pattern(script_tag: ScriptTag) -> re.Pattern:


_XREF_TAG_PATTERN = (
r'script_xref\(\s*name\s*:\s*["\'](?P<type>{type})["\']\s*,'
r'\s*value\s*:\s*["\']?(?P<value>{value})["\']?\s*\)\s*;'
r'script_xref\(\s*name\s*:\s*(?P<quote>[\'"])(?P<name>{name})(?P=quote)\s*,'
r'\s*value\s*:\s*(?P<quote2>[\'"])(?P<value>{value})(?P=quote2)\s*\)\s*;'
)


def get_xref_pattern(
name: str,
*,
value: str = r".+",
flags: re.RegexFlag = 0,
name: str, *, value: str = r".+?", flags: re.RegexFlag = 0
) -> re.Pattern:
"""
The returned pattern catches all
`script_xref(name="{type}", value="{value}");`
`script_xref(name="{name}", value="{value}");`

Arguments:
name script xref type e.g. URL
name script xref name e.g. URL
value script tag value (default: at least one char)
flags regex flags for compile (default: 0)

The returned `Match`s by this pattern will have group strings
.group('type') and .group('value')
.group('name') and .group('value')
Returns
`re.Pattern` object
"""
return re.compile(
_XREF_TAG_PATTERN.format(type=name, value=value),
_XREF_TAG_PATTERN.format(name=name, value=value),
flags=flags,
)

Expand All @@ -178,12 +175,6 @@ def get_xref_pattern(
r"(?P=quote99)?\s*\)\s*;"
)

_XREF_TAG_PATTERN = (
r'script_xref\(\s*name\s*:\s*(?P<quote>[\'"])(?P<type>{type})'
r'(?P=quote)\s*,\s*value\s*:\s*(?P<quote2>[\'"])?(?P<value>{'
r"value})(?P=quote2)?\s*\)\s*;"
)


class SpecialScriptTag(Enum):
ADD_PREFERENCE = "add_preference"
Expand Down
7 changes: 6 additions & 1 deletion troubadix/plugins/script_tag_whitespaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from troubadix.helper.patterns import (
_get_special_script_tag_pattern,
_get_tag_pattern,
get_xref_pattern,
)
from troubadix.plugin import FileContentPlugin, LinterError, LinterResult

Expand Down Expand Up @@ -51,7 +52,11 @@ def check_content(
name=SpecialScriptTag.NAME.value
).finditer(file_content)

matches = chain(tag_matches, name_matches)
xref_matches = get_xref_pattern(name=r".+?", flags=re.S).finditer(
file_content
)

matches = chain(tag_matches, name_matches, xref_matches)

for match in matches:
if re.match(r"^\s+.*", match.group("value")) or re.match(
Expand Down
2 changes: 1 addition & 1 deletion troubadix/plugins/script_xref_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def check_content(
):
yield LinterError(
f"{match.group(0)}: does not conform to"
' script_xref(name:"<name>", value:<value>);',
' script_xref(name:"<name>", value:"<value>");',
file=nasl_file,
plugin=self.name,
)
Loading