Skip to content

Commit a09d4a2

Browse files
authored
#1617: fix CVE generation #1624: tolerant versionrange parsing (#1625)
1 parent 4dc416f commit a09d4a2

File tree

48 files changed

+262
-538
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+262
-538
lines changed

cli/src/main/java/com/devonfw/tools/ide/json/VersionRangeDeserializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public VersionRange deserialize(JsonParser p, DeserializationContext context) th
1919

2020
JsonToken token = p.getCurrentToken();
2121
if (token == JsonToken.VALUE_STRING) {
22-
return VersionRange.of(p.getValueAsString());
22+
return VersionRange.of(p.getValueAsString(), true);
2323
} else {
2424
throw new IllegalArgumentException("Invalid JSON for VersionRange!");
2525
}

cli/src/main/java/com/devonfw/tools/ide/url/model/file/json/Cve.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ public Cve merge(Cve issue) {
6464
return new Cve(this.id, this.severity, newVersions);
6565
}
6666

67-
private static void mergeVersionRage(List<VersionRange> newVersions, VersionRange versionRange) {
67+
/**
68+
* @param newVersions the {@link List} of {@link VersionRange}s.
69+
* @param versionRange the new {@link VersionRange} to add.
70+
*/
71+
public static void mergeVersionRage(List<VersionRange> newVersions, VersionRange versionRange) {
6872

6973
if (newVersions.isEmpty()) {
7074
newVersions.add(versionRange);

cli/src/main/java/com/devonfw/tools/ide/version/VersionIdentifier.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
public final class VersionIdentifier implements VersionObject<VersionIdentifier>, GenericVersionRange {
1717

1818
/** {@link VersionIdentifier} "*" that will resolve to the latest stable version. */
19-
public static final VersionIdentifier LATEST = VersionIdentifier.of("*");
19+
public static final VersionIdentifier LATEST = new VersionIdentifier(VersionSegment.of("*"));
2020

2121
/** {@link VersionIdentifier} "*!" that will resolve to the latest snapshot. */
2222
public static final VersionIdentifier LATEST_UNSTABLE = VersionIdentifier.of("*!");
@@ -324,7 +324,7 @@ public static VersionIdentifier of(String version) {
324324

325325
if (version == null) {
326326
return null;
327-
} else if (version.equals("latest")) {
327+
} else if (version.equals("latest") || version.equals("*")) {
328328
return VersionIdentifier.LATEST;
329329
}
330330
VersionSegment startSegment = VersionSegment.of(version);

cli/src/main/java/com/devonfw/tools/ide/version/VersionRange.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,16 @@ public String toString() {
193193
*/
194194
public static VersionRange of(String value) {
195195

196+
return of(value, false);
197+
}
198+
199+
/**
200+
* @param value the {@link #toString() string representation} of a {@link VersionRange} to parse.
201+
* @param tolerance {@code true} to enable tolerant parsing so we can read garbage (e.g. form JSON) without failing.
202+
* @return the parsed {@link VersionRange}.
203+
*/
204+
public static VersionRange of(String value, boolean tolerance) {
205+
196206
Boolean isleftExclusive = null;
197207
Boolean isRightExclusive = null;
198208
if (value.startsWith(BoundaryType.START_EXCLUDING_PREFIX)) {
@@ -219,18 +229,25 @@ public static VersionRange of(String value) {
219229
String minString = value.substring(0, index);
220230
if (!minString.isBlank()) {
221231
min = VersionIdentifier.of(minString);
232+
if (min == VersionIdentifier.LATEST) {
233+
min = null;
234+
}
222235
}
223236
String maxString = value.substring(index + 1);
224237
if (!maxString.isBlank()) {
225238
max = VersionIdentifier.of(maxString);
239+
if (max == VersionIdentifier.LATEST) {
240+
max = null;
241+
}
226242
}
227243
}
228-
if (isleftExclusive == null) {
244+
if ((isleftExclusive == null) || (tolerance && (min == null))) {
229245
isleftExclusive = min == null;
230246
}
231-
if (isRightExclusive == null) {
247+
if ((isRightExclusive == null) || (tolerance && (max == null))) {
232248
isRightExclusive = max == null;
233249
}
250+
234251
if ((min == null) && (max == null) && isleftExclusive && isRightExclusive) {
235252
return UNBOUNDED;
236253
}

cli/src/test/java/com/devonfw/tools/ide/version/VersionRangeTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,16 @@ public void testIllegalSyntax() {
172172
checkIllegalRange("(1.1,1.0)");
173173
}
174174

175+
/** Test of {@link VersionRange#of(String, boolean)} with tolerance. */
176+
@Test
177+
public void testTolerance() {
178+
179+
assertThat(VersionRange.of("[*,*]", true)).isEqualTo(VersionRange.UNBOUNDED);
180+
assertThat(VersionRange.of("[,)", true)).isEqualTo(VersionRange.UNBOUNDED);
181+
assertThat(VersionRange.of("(,]", true)).isEqualTo(VersionRange.UNBOUNDED);
182+
assertThat(VersionRange.of("[,]", true)).isEqualTo(VersionRange.UNBOUNDED);
183+
}
184+
175185
private void checkIllegalRange(String range) {
176186

177187
try {

0 commit comments

Comments
 (0)