Skip to content

Commit f0c471e

Browse files
a-orenclaude
andcommitted
fix: strip PEP 508 environment markers in splitToNameVersion (TC-4041)
Prevents environment markers (e.g. "; python_version >= '3.6'") from leaking into version comparison, which caused false version mismatches. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 02462fe commit f0c471e

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

src/main/java/io/github/guacsec/trustifyda/providers/PythonPipProvider.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ private static String extractDepFull(String requirementLine) {
210210
}
211211

212212
private String[] splitToNameVersion(String nameVersion) {
213+
// Strip PEP 508 environment markers (everything after ";")
214+
int markerIndex = nameVersion.indexOf(';');
215+
if (markerIndex != -1) {
216+
nameVersion = nameVersion.substring(0, markerIndex).trim();
217+
}
213218
String[] result;
214219
if (nameVersion.matches(
215220
"[a-zA-Z0-9-_()]+={2}[0-9]{1,4}[.][0-9]{1,4}(([.][0-9]{1,4})|([.][a-zA-Z0-9]+)|([a-zA-Z0-9]+)|([.][a-zA-Z0-9]+[.][a-z-A-Z0-9]+))?")) {

src/test/java/io/github/guacsec/trustifyda/providers/Python_Provider_Test.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,39 @@ void Test_The_ProvideComponent_Path_Should_Throw_Exception() {
267267
.isThrownBy(() -> new PythonPipProvider(Path.of(".")).provideComponent());
268268
}
269269

270+
@Test
271+
void getIgnoredDependencies_strips_environment_markers() throws IOException {
272+
Path tempFile = Files.createTempFile("requirements", ".txt");
273+
try {
274+
Files.writeString(
275+
tempFile,
276+
String.join(
277+
System.lineSeparator(),
278+
"requests==2.25.1 ; python_version >= \"3.6\" #trustify-da-ignore",
279+
"idna==2.10 ; python_version >= \"3.6\" # trustify-da-ignore",
280+
"six==1.16.0 ; python_version < \"3.0\" or python_version >= \"3.3\""
281+
+ " #trustify-da-ignore",
282+
"chardet==4.0.0 ; python_version >= \"3.6\" and sys_platform == \"linux\""
283+
+ " #trustify-da-ignore",
284+
"flask==2.0.3"));
285+
var provider = new PythonPipProvider(tempFile);
286+
var ignored = provider.getIgnoredDependencies(Files.readString(tempFile));
287+
var ignoredMap =
288+
ignored.stream()
289+
.collect(
290+
java.util.stream.Collectors.toMap(
291+
com.github.packageurl.PackageURL::getName,
292+
com.github.packageurl.PackageURL::getVersion));
293+
assertThat(ignoredMap).containsEntry("requests", "2.25.1");
294+
assertThat(ignoredMap).containsEntry("idna", "2.10");
295+
assertThat(ignoredMap).containsEntry("six", "1.16.0");
296+
assertThat(ignoredMap).containsEntry("chardet", "4.0.0");
297+
assertThat(ignoredMap).doesNotContainKey("flask");
298+
} finally {
299+
Files.deleteIfExists(tempFile);
300+
}
301+
}
302+
270303
private String dropIgnored(String s) {
271304
return s.replaceAll("\\s+", "").replaceAll("\"timestamp\":\"[a-zA-Z0-9\\-\\:]+\"", "");
272305
}

0 commit comments

Comments
 (0)