Skip to content

Commit 9c0f322

Browse files
committed
Added v2 to v3 transformation that includes package changes
1 parent 4cfc957 commit 9c0f322

File tree

2 files changed

+171
-1
lines changed

2 files changed

+171
-1
lines changed

src/codemodder/codetf/v3/codetf.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55

66
from pydantic import BaseModel, model_validator
77

8+
from codemodder.logging import logger
9+
810
from ..common import Change, CodeTFWriter, Finding, FixQuality
911
from ..v2.codetf import AIMetadata as AIMetadatav2
12+
from ..v2.codetf import ChangeSet as v2ChangeSet
1013
from ..v2.codetf import CodeTF as CodeTFv2
1114
from ..v2.codetf import Result
1215
from ..v2.codetf import Run as Runv2
@@ -148,6 +151,48 @@ def from_v2_aimetadata(ai_metadata: AIMetadatav2) -> AIMetadata:
148151
)
149152

150153

154+
def from_v2_result_per_finding(result: Result) -> FixResult | None:
155+
"""
156+
This transformation assumes that the v2 result will only contain a single fixedFinding for all changesets.
157+
"""
158+
# Find the changeset with a fixedFinding
159+
try:
160+
changeset: v2ChangeSet = next(cs for cs in result.changeset if cs.fixedFindings)
161+
except StopIteration:
162+
logger.debug("No fixedFinding in the given Result")
163+
return None
164+
165+
assert changeset.fixedFindings
166+
finding = changeset.fixedFindings[0]
167+
168+
v3changesets = [
169+
ChangeSet(
170+
path=cs.path, diff=cs.diff, changes=[c.to_common() for c in cs.changes]
171+
)
172+
for cs in result.changeset
173+
]
174+
175+
generation_metadata = GenerationMetadata(
176+
strategy=Strategy.ai if changeset.ai else Strategy.deterministic,
177+
ai=from_v2_aimetadata(changeset.ai) if changeset.ai else None,
178+
provisional=False,
179+
)
180+
181+
fix_metadata = FixMetadata(
182+
id=result.codemod,
183+
summary=result.summary,
184+
description=result.description,
185+
generation=generation_metadata,
186+
)
187+
188+
return FixResult(
189+
finding=Finding(**finding.model_dump()),
190+
fixStatus=FixStatus(status=FixStatusType.fixed),
191+
changeSets=v3changesets,
192+
fixMetadata=fix_metadata,
193+
)
194+
195+
151196
def from_v2_result(result: Result) -> list[FixResult]:
152197
fix_results: list[FixResult] = []
153198
# generate fixed

tests/test_codetf.py

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@
2424
Strategy,
2525
)
2626
from codemodder.codetf.v3.codetf import Finding as FindingV3
27-
from codemodder.codetf.v3.codetf import FixStatusType, from_v2, from_v2_result
27+
from codemodder.codetf.v3.codetf import (
28+
FixStatusType,
29+
from_v2,
30+
from_v2_result,
31+
from_v2_result_per_finding,
32+
)
2833

2934

3035
@pytest.fixture(autouse=True)
@@ -259,6 +264,126 @@ def test_v2_result_to_v3():
259264
assert from_v2_result(result)
260265

261266

267+
def test_v2_result_to_v3_per_finding():
268+
result = Result(
269+
codemod="codeql:java/log-injection",
270+
summary="Introduced protections against Log Inject ion / Forging attacks",
271+
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',
272+
detectionTool=DetectionTool(name="CodeQL"),
273+
references=[
274+
Reference(
275+
url="https://owasp.org/www-community/attacks/Log_Inj ection",
276+
description="https://owasp.org/www-community/attacks/Log_Injection",
277+
),
278+
Reference(
279+
url="https://knowledge-base.secureflag.com/vulnerabilities/inadequate_input_validation/log_inject ion_vulnerability.html",
280+
description="https://knowledge-base.secureflag.com/vulnerabilities/inadequate_input_validation/log_injection_vulnerability.html",
281+
),
282+
Reference(
283+
url="https://cwe.mit re.org/data/definitions/117.html",
284+
description="https://cwe.mitre.org/data/definitions/117.html",
285+
),
286+
],
287+
properties={},
288+
failedFiles=[],
289+
changeset=[
290+
ChangeSet(
291+
path="app/src/main/java/org/apache/roller/planet/business/fetcher/RomeFeedFetcher.java",
292+
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@@ -123,7 +124,7 @@\n }\n \n if(log.isDebugEnabled()) {\n- log.debug("Subscription is: " + newSub.toString());\n+ log.debug("Subscription is: " + stripAll(newSub.toString()));\n }\n \n ',
293+
changes=[
294+
Change(
295+
lineNumber=126,
296+
description="Added a call to replace any newlines the value",
297+
diffSide=DiffSide.LEFT,
298+
properties={},
299+
packageActions=[
300+
PackageAction(
301+
action=Action.ADD,
302+
result=PackageResult.COMPLETED,
303+
package="pkg:maven/io.github.pixee/[email protected]",
304+
),
305+
PackageAction(
306+
action=Action.ADD,
307+
result=PackageResult.COMPLETED,
308+
package="pkg:maven/io.github.pixee/[email protected]",
309+
),
310+
],
311+
fixedFindings=[
312+
Finding(
313+
id="915a8320-3ee8-4b0e-849b-c1b380fb83e2",
314+
rule=Rule(
315+
id="log-injection",
316+
name="Log Injection",
317+
url="https://codeql.github.com/codeql-query-help/java/java-log-injection/",
318+
),
319+
)
320+
],
321+
)
322+
],
323+
ai=None,
324+
strategy=Strategy.deterministic,
325+
provisional=False,
326+
fixedFindings=[
327+
Finding(
328+
id="915a8320-3ee8-4b0e-849b-c1b380fb83e2",
329+
rule=Rule(
330+
id="log-injection",
331+
name="Log Injection",
332+
url="https://codeql.github.com/codeql-query-help/java/java-log-injection/",
333+
),
334+
)
335+
],
336+
fixQuality=None,
337+
),
338+
ChangeSet(
339+
path="app/pom.xml",
340+
diff="--- app/pom.xml\n+++ app/pom.xml\n@@ -591,9 +591,12 @@\n <version>5.3.0</version>\n <scope>test</scope>\n </dependency>\n+ <dependency>\n+ <groupId>io.github.pixee</groupId>\n+ <artifactId>java-security-toolkit</artifactId>\n+ </dependency>\n+ </dependencies>\n \n- </dependencies>\n-\n <build>\n \n <finalName>roller</finalName>",
341+
changes=[
342+
Change(
343+
lineNumber=594,
344+
description="This library holds security tools for protecting Java API calls.\n\nLicense: MIT ✅ | [Open source](https://github.com/pixee/java-security-toolkit) ✅ | [More facts](https://mvnrepository.com/artifact/io.github.pixee/java-security-toolkit/1.2.2)\n",
345+
diffSide=DiffSide.RIGHT,
346+
properties={"contextual_description": "true"},
347+
packageActions=[],
348+
fixedFindings=[],
349+
)
350+
],
351+
ai=None,
352+
strategy=Strategy.deterministic,
353+
provisional=False,
354+
fixedFindings=[],
355+
fixQuality=None,
356+
),
357+
ChangeSet(
358+
path="pom.xml",
359+
diff="--- pom.xml\n+++ pom.xml\n@@ -48,7 +48,8 @@\n <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>\n <roller.version>6.1.5</roller.version>\n <slf4j.version>1.7.36</slf4j.version>\n- </properties>\n+ <versions.java-security-toolkit>1.2.2</versions.java-security-toolkit>\n+ </properties>\n \n <modules>\n <module>app</module>\n@@ -110,7 +111,12 @@\n <version>5.11.4</version>\n <scope>test</scope>\n </dependency>\n- </dependencies>\n+ <dependency>\n+ <groupId>io.github.pixee</groupId>\n+ <artifactId>java-security-toolkit</artifactId>\n+ <version>${versions.java-security-toolkit}</version>\n+ </dependency>\n+ </dependencies>\n </dependencyManagement>\n \n </project>",
360+
changes=[
361+
Change(
362+
lineNumber=114,
363+
description="This library holds security tools for protecting Java API calls.\n\nLicense: MIT ✅ | [Open source](https://github.com/pixee/java-security-toolkit) ✅ | [More facts](https://mvnrepository.com/artifact/io.github.pixee/java-security-toolkit/1.2.2)\n",
364+
diffSide=DiffSide.RIGHT,
365+
properties={"contextual_description": "true"},
366+
packageActions=[],
367+
fixedFindings=[],
368+
)
369+
],
370+
ai=None,
371+
strategy=Strategy.deterministic,
372+
provisional=False,
373+
fixedFindings=[],
374+
fixQuality=None,
375+
),
376+
],
377+
unfixedFindings=[],
378+
)
379+
fix_result = from_v2_result_per_finding(result)
380+
assert fix_result
381+
assert len(fix_result.changeSets) == 3
382+
all_paths = {cs.path for cs in fix_result.changeSets}
383+
assert "app/pom.xml" in all_paths
384+
assert "pom.xml" in all_paths
385+
386+
262387
def test_v2_to_v3_conversion():
263388
with open("tests/samples/codetfv2_sample.codetf", "r") as f:
264389
codetfv2 = CodeTF.model_validate_json(f.read())

0 commit comments

Comments
 (0)