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

Add: Check for creation date <= last modification date #799

Merged
merged 3 commits into from
Mar 3, 2025
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
40 changes: 40 additions & 0 deletions tests/plugins/test_creation_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,43 @@ def test_wrong_length(self):
"False or incorrectly formatted creation_date.",
results[0].message,
)

def test_creation_date_greater_than_last_modification(self):
path = Path("some/file.nasl")
content = (
' script_tag(name:"creation_date", value:"2025-01-01 00:00:01 '
'+0200 (Wed, 01 Jan 2025)");\n'
' script_tag(name:"last_modification", value:"2025-01-01 00:00:00 '
'+0200 (Wed, 01 Jan 2025)");\n'
)
fake_context = self.create_file_plugin_context(
nasl_file=path, file_content=content
)
plugin = CheckCreationDate(fake_context)

results = list(plugin.run())

self.assertEqual(len(results), 1)

self.assertIsInstance(results[0], LinterError)
self.assertEqual(
"The creation_date must not be greater than the last modification date.",
results[0].message,
)

def test_creation_date_equal_last_modification(self):
path = Path("some/file.nasl")
content = (
' script_tag(name:"creation_date", value:"2025-01-01 00:00:00 '
'+0200 (Wed, 01 Jan 2025)");\n'
' script_tag(name:"last_modification", value:"2025-01-01 00:00:00 '
'+0200 (Wed, 01 Jan 2025)");\n'
)
fake_context = self.create_file_plugin_context(
nasl_file=path, file_content=content
)
plugin = CheckCreationDate(fake_context)

results = list(plugin.run())

self.assertEqual(len(results), 0)
84 changes: 49 additions & 35 deletions troubadix/plugins/creation_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,45 +52,59 @@ def check_content(
# Example: "2017-11-29 13:56:41 +0100 (Wed, 29 Nov 2017)"
match = tag_pattern.search(file_content)

if match:
try:
date_left = datetime.strptime(
match.group("value")[:25], "%Y-%m-%d %H:%M:%S %z"
)
# 2017-11-29 13:56:41 +0100 (error if no timezone)
date_right = datetime.strptime(
match.group("value")[27:43], "%a, %d %b %Y"
)
week_day_parsed = date_right.strftime("%a")
except ValueError:
yield LinterError(
"False or incorrectly formatted creation_date.",
file=nasl_file,
plugin=self.name,
)
return
if not match:
yield LinterError(
"False or incorrectly formatted creation_date.",
file=nasl_file,
plugin=self.name,
)
return

week_day_str = match.group("value")[27:30]
# Wed, 29 Nov 2017
if date_left.date() != date_right.date():
yield LinterError(
"The creation_date consists of two different dates.",
file=nasl_file,
plugin=self.name,
)
# Check correct weekday
elif week_day_str != week_day_parsed:
formatted_date = week_day_parsed
yield LinterError(
f"Wrong day of week. Please change it from '{week_day_str}"
f"' to '{formatted_date}'.",
file=nasl_file,
plugin=self.name,
)
else:
try:
date_left = datetime.strptime(
match.group("value")[:25], "%Y-%m-%d %H:%M:%S %z"
)
# 2017-11-29 13:56:41 +0100 (error if no timezone)
date_right = datetime.strptime(
match.group("value")[27:43], "%a, %d %b %Y"
)
week_day_parsed = date_right.strftime("%a")
except ValueError:
yield LinterError(
"False or incorrectly formatted creation_date.",
file=nasl_file,
plugin=self.name,
)
return

week_day_str = match.group("value")[27:30]
# Wed, 29 Nov 2017
if date_left.date() != date_right.date():
yield LinterError(
"The creation_date consists of two different dates.",
file=nasl_file,
plugin=self.name,
)
# Check correct weekday
elif week_day_str != week_day_parsed:
formatted_date = week_day_parsed
yield LinterError(
f"Wrong day of week. Please change it from '{week_day_str}"
f"' to '{formatted_date}'.",
file=nasl_file,
plugin=self.name,
)

last_modification_pattern = get_script_tag_pattern(
ScriptTag.LAST_MODIFICATION
)
if match := last_modification_pattern.search(file_content):
last_modification = datetime.strptime(
match.group("value")[:25], "%Y-%m-%d %H:%M:%S %z"
)
if date_left > last_modification:
yield LinterError(
"The creation_date must not be greater than the last modification date.",
file=nasl_file,
plugin=self.name,
)
Loading