Skip to content

Commit 4543297

Browse files
authored
Fixed CodeTF v2 result to v3 transformation (#1068)
* CodetfV2 will now use more common types * Added test
1 parent 1f2788c commit 4543297

File tree

2 files changed

+83
-21
lines changed

2 files changed

+83
-21
lines changed

src/codemodder/codetf/v2/codetf.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,32 @@
1515

1616
from codemodder import __version__
1717

18-
from ..common import (
19-
CaseInsensitiveEnum,
20-
)
18+
from ..common import Action as CommonAction
2119
from ..common import Change as CommonChange
2220
from ..common import (
2321
CodeTFWriter,
22+
)
23+
from ..common import DiffSide as CommonDiffSide
24+
from ..common import (
2425
FixQuality,
26+
)
27+
from ..common import PackageAction as CommonPackageAction
28+
from ..common import PackageResult as CommonPackageResult
29+
from ..common import (
2530
Rule,
2631
)
2732

2833
if TYPE_CHECKING:
2934
from codemodder.context import CodemodExecutionContext
3035

3136

32-
class Action(CaseInsensitiveEnum):
33-
ADD = "add"
34-
REMOVE = "remove"
35-
36-
37-
class PackageResult(CaseInsensitiveEnum):
38-
COMPLETED = "completed"
39-
FAILED = "failed"
40-
SKIPPED = "skipped"
41-
37+
Action = CommonAction
4238

43-
class DiffSide(CaseInsensitiveEnum):
44-
LEFT = "left"
45-
RIGHT = "right"
39+
PackageResult = CommonPackageResult
4640

41+
DiffSide = CommonDiffSide
4742

48-
class PackageAction(BaseModel):
49-
action: Action
50-
result: PackageResult
51-
package: str
43+
PackageAction = CommonPackageAction
5244

5345

5446
class Change(BaseModel):

tests/test_codetf.py

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,15 @@
1616
Result,
1717
Rule,
1818
)
19+
from codemodder.codetf.v2.codetf import (
20+
Action,
21+
DetectionTool,
22+
PackageAction,
23+
PackageResult,
24+
Strategy,
25+
)
1926
from codemodder.codetf.v3.codetf import Finding as FindingV3
20-
from codemodder.codetf.v3.codetf import FixStatusType, from_v2
27+
from codemodder.codetf.v3.codetf import FixStatusType, from_v2, from_v2_result
2128

2229

2330
@pytest.fixture(autouse=True)
@@ -189,6 +196,69 @@ def test_v3_finding_id_not_optional():
189196
FindingV3(id=None, rule=Rule(id="foo", name="whatever")) # type: ignore[arg-type]
190197

191198

199+
def test_v2_result_to_v3():
200+
result = Result(
201+
codemod="codeql:java/log-injection",
202+
summary="Introduced protections against Log Inject ion / Forging attacks",
203+
description='This change ensures that log messages can\'t contain newline characters, leaving you vulnerable to Log Forging / Log Injection.\n\nIf malicious users can get newline characters into a log message, they can inject and forge new log entries that look like they came from the server, and trick log analysis tools, administrators, and more . This leads to vulnerabilities like Log Injection, Log Forging, and more attacks from there.\n\nOur change simply strips out newline characters from log messages, ensuring that they can \'t be used to forge new log entries.\n```diff\n+ import io.github.pixee.security.Newlines;\n ...\n String orderId = getUserOrderId();\n- log.info("User order ID: " + orderId);\n+ log. info("User order ID: " + Newlines.stripNewlines(orderId));\n```\n',
204+
detectionTool=DetectionTool(name="CodeQL"),
205+
references=[
206+
Reference(
207+
url="https://owasp.org/www-community/attacks/Log_Inj ection",
208+
description="https://owasp.org/www-community/attacks/Log_Injection",
209+
),
210+
Reference(
211+
url="https://knowledge-base.secureflag.com/vulnerabilities/inadequate_input_validation/log_inject ion_vulnerability.html",
212+
description="https://knowledge-base.secureflag.com/vulnerabilities/inadequate_input_validation/log_injection_vulnerability.html",
213+
),
214+
Reference(
215+
url="https://cwe.mit re.org/data/definitions/117.html",
216+
description="https://cwe.mitre.org/data/definitions/117.html",
217+
),
218+
],
219+
properties={},
220+
failedFiles=[],
221+
changeset=[
222+
ChangeSet(
223+
path="app/src/main/java/org/apache /roller/planet/business/fetcher/RomeFeedFetcher.java",
224+
diff='--- RomeFeedFetcher.java\n+++ RomeFeedFetcher.java\n@@ -26,6 +26,7 @@\n import com.rometools.rome.io.FeedException;\n import com.rometools.rome.io.SyndFeedInput;\n import com.rometools.rome.io.XmlReader;\n+import static io.github.pixee.security.Newlines.stripAll;\n \n import java.io.IOException;\n import java. net.URI;\n@@ -87,7 +88,7 @@\n }\n \n // fetch the feed\n- log.debug("Fetching feed: "+feedURL);\n+ log.debug("Fetching feed: "+stripAll(feedURL));\n SyndFeed feed;\n try {\n feed = fetchFeed(feedURL);',
225+
changes=[
226+
Change(
227+
lineNumber=90,
228+
description="Added a call to replace any newlines the value",
229+
diffSide=DiffSide.LEFT,
230+
properties={},
231+
packageActions=[
232+
PackageAction(
233+
action=Action.ADD,
234+
result=PackageResult.FAILED,
235+
package="pkg:maven/io.github.pixee/java-security [email protected]",
236+
)
237+
],
238+
fixedFindings=[
239+
Finding(
240+
id="e5ceaca8-4a05-4f8d-ac74-6a822ac69d8f",
241+
rule=Rule(
242+
id="log-injection",
243+
name="Log Injection",
244+
url="https://codeql.github.com/codeql-query-help/ java/java-log-injection/",
245+
),
246+
)
247+
],
248+
)
249+
],
250+
ai=None,
251+
strategy=Strategy.deterministic,
252+
provisional=False,
253+
fixedFindings=None,
254+
fixQuality=None,
255+
)
256+
],
257+
unfixedFindings=[],
258+
)
259+
assert from_v2_result(result)
260+
261+
192262
def test_v2_to_v3_conversion():
193263
with open("tests/samples/codetfv2_sample.codetf", "r") as f:
194264
codetfv2 = CodeTF.model_validate_json(f.read())

0 commit comments

Comments
 (0)