Skip to content

Commit 2fba57b

Browse files
a-orenclaude
andcommitted
fix(golang): reject // indirect //exhortignore as ignore marker (TC-4345)
The greedy regex in IgnoredLine matched the last // in a line, causing "// indirect //exhortignore" to be incorrectly treated as ignored. Parse the first // comment instead and only accept standalone ignore markers or the semicolon-separated format (// indirect; exhortignore), matching the JavaScript client behavior. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent cea1a4b commit 2fba57b

2 files changed

Lines changed: 33 additions & 26 deletions

File tree

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

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -560,32 +560,32 @@ private String extractPackageName(String line) {
560560
public boolean IgnoredLine(String line) {
561561
boolean result = false;
562562
if (IgnorePatternDetector.containsIgnorePattern(line)) {
563-
// if exhortignore or trustify-da-ignore is alone in a comment or is in a comment together
564-
// with indirect or as a
565-
// comment inside a
566-
// comment ( e.g // indirect //exhort)
567-
// then this line is to be checked if it's a comment after a package name.
568-
if (Pattern.matches(".+//\\s*(exhortignore|trustify-da-ignore)", line)
569-
|| Pattern.matches(
570-
".+//\\s*indirect\\s*;\\s*(//)?\\s*(exhortignore|trustify-da-ignore)", line)) {
571-
String trimmedRow = line.trim();
572-
// filter out lines where exhortignore or trustify-da-ignore has no meaning
573-
if (!trimmedRow.startsWith("module ")
574-
&& !trimmedRow.startsWith("go ")
575-
&& !trimmedRow.startsWith("require (")
576-
&& !trimmedRow.startsWith("require(")
577-
&& !trimmedRow.startsWith("exclude ")
578-
&& !trimmedRow.startsWith("replace ")
579-
&& !trimmedRow.startsWith("retract ")
580-
&& !trimmedRow.startsWith("use ")
581-
&& !trimmedRow.contains(
582-
"=>")) { // only for lines that after trimming starts with "require " or starting
583-
// with
584-
// package name followd by one space, and then a semver version.
585-
if (trimmedRow.startsWith("require ")
586-
|| Pattern.matches(
587-
"^[a-z.0-9/-]+\\s{1,2}[vV][0-9]\\.[0-9](\\.[0-9]){0,2}.*", trimmedRow)) {
588-
result = true;
563+
String trimmedRow = line.trim();
564+
int firstComment = trimmedRow.indexOf("//");
565+
if (firstComment >= 0) {
566+
String comment = trimmedRow.substring(firstComment + 2).trim();
567+
// Match standalone ignore pattern (e.g. "// exhortignore") or
568+
// semicolon-separated with indirect (e.g. "// indirect; exhortignore",
569+
// "// indirect; // exhortignore").
570+
// Does NOT match "// indirect //exhortignore" (no semicolon) or
571+
// "// indirect exhortignore" (space-separated) — these are incorrect formats.
572+
if (comment.matches("(exhortignore|trustify-da-ignore)")
573+
|| comment.matches("indirect\\s*;\\s*(//)?\\s*(exhortignore|trustify-da-ignore)")) {
574+
// filter out lines where exhortignore or trustify-da-ignore has no meaning
575+
if (!trimmedRow.startsWith("module ")
576+
&& !trimmedRow.startsWith("go ")
577+
&& !trimmedRow.startsWith("require (")
578+
&& !trimmedRow.startsWith("require(")
579+
&& !trimmedRow.startsWith("exclude ")
580+
&& !trimmedRow.startsWith("replace ")
581+
&& !trimmedRow.startsWith("retract ")
582+
&& !trimmedRow.startsWith("use ")
583+
&& !trimmedRow.contains("=>")) {
584+
if (trimmedRow.startsWith("require ")
585+
|| Pattern.matches(
586+
"^[a-z.0-9/-]+\\s{1,2}[vV][0-9]\\.[0-9](\\.[0-9]){0,2}.*", trimmedRow)) {
587+
result = true;
588+
}
589589
}
590590
}
591591
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,13 @@ void test_IgnoredLine_rejects_old_space_separated_indirect_format() {
239239
.isFalse();
240240
assertThat(provider.IgnoredLine(" github.com/foo/bar v1.0.0 // indirect exhortignore"))
241241
.isFalse();
242+
// Double-slash without semicolon should NOT be recognized (TC-4345)
243+
assertThat(
244+
provider.IgnoredLine(
245+
" github.com/foo/bar v1.0.0 // indirect //trustify-da-ignore"))
246+
.isFalse();
247+
assertThat(provider.IgnoredLine(" github.com/foo/bar v1.0.0 // indirect //exhortignore"))
248+
.isFalse();
242249
// New semicolon format should still be recognized
243250
assertThat(
244251
provider.IgnoredLine(

0 commit comments

Comments
 (0)