Skip to content

Commit

Permalink
Fix URL encoding and decoding
Browse files Browse the repository at this point in the history
The methods `uriEncode` and `uriDecode` did not properly handle
percent-encoding. In particular, `uriEncode` didn't properly output two
uppercase hex digits and `urlDecode` did not properly handle non-ASCII
characters.

Aditionally, if no percent-encoding was performed, these methods will
now return the original string.

Fixes package-url#150
Closes package-url#153
Fixes package-url#154
  • Loading branch information
dwalluck committed Feb 20, 2025
1 parent ee6dda9 commit ee295df
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 29 deletions.
65 changes: 36 additions & 29 deletions src/main/java/com/github/packageurl/PackageURL.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/
package com.github.packageurl;

import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -441,22 +442,25 @@ private String percentEncode(final String input) {
}

private static String uriEncode(String source, Charset charset) {
if (source == null || source.length() == 0) {
if (source == null || source.isEmpty()) {
return source;
}

StringBuilder builder = new StringBuilder();
for (byte b : source.getBytes(charset)) {
boolean changed = false;
StringBuilder builder = new StringBuilder(source.length());
byte[] bytes = source.getBytes(charset);

for (byte b : bytes) {
if (isUnreserved(b)) {
builder.append((char) b);
}
else {
// Substitution: A '%' followed by the hexadecimal representation of the ASCII value of the replaced character
} else {
builder.append('%');
builder.append(Integer.toHexString(b).toUpperCase());
builder.append(Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, 16)));
builder.append(Character.toUpperCase(Character.forDigit(b & 0xF, 16)));
changed = true;
}
}
return builder.toString();
return changed ? builder.toString() : source;
}

private static boolean isUnreserved(int c) {
Expand All @@ -479,34 +483,37 @@ private static boolean isDigit(int c) {
* @return a decoded String
*/
private String percentDecode(final String input) {
if (input == null) {
return null;
}
final String decoded = uriDecode(input);
if (!decoded.equals(input)) {
return decoded;
}
return input;
return uriDecode(input);
}

public static String uriDecode(String source) {
if (source == null) {
if (source == null || source.isEmpty()) {
return source;
}
int length = source.length();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < length; i++) {
if (source.charAt(i) == '%') {
String str = source.substring(i + 1, i + 3);
char c = (char) Integer.parseInt(str, 16);
builder.append(c);
i += 2;
}
else {
builder.append(source.charAt(i));

boolean changed = false;
byte[] bytes = source.getBytes(StandardCharsets.UTF_8);
ByteArrayOutputStream buffer = new ByteArrayOutputStream(bytes.length);

for (int i = 0; i < bytes.length; i++) {
int b = bytes[i];

if (b == '%') {
if (i + 2 >= bytes.length) {
return source;
}

int b1 = Character.digit(bytes[++i], 16);
int b2 = Character.digit(bytes[++i], 16);
buffer.write((char) ((b1 << 4) + b2));
changed = true;
} else {
buffer.write(b);
}
}
return builder.toString();

byte[] b = buffer.toByteArray();
return changed ? new String(b, StandardCharsets.UTF_8) : source;
}

/**
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/com/github/packageurl/PackageURLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ public static void setup() throws IOException {
json = new JSONArray(jsonTxt);
}

@Test
public void testEncoding1() throws MalformedPackageURLException {
PackageURL purl = new PackageURL("maven", "com.google.summit", "summit-ast", "2.2.0\n", null, null);
Assert.assertEquals("pkg:maven/com.google.summit/[email protected]%0A", purl.toString());
}

@Test
public void testEncoding2() throws MalformedPackageURLException {
PackageURL purl = new PackageURL("pkg:nuget/%D0%9Cicros%D0%BEft.%D0%95ntit%D1%83Fram%D0%B5work%D0%A1%D0%BEr%D0%B5");
Assert.assertEquals("Мicrosоft.ЕntitуFramеworkСоrе", purl.getName());
Assert.assertEquals("pkg:nuget/%D0%9Cicros%D0%BEft.%D0%95ntit%D1%83Fram%D0%B5work%D0%A1%D0%BEr%D0%B5", purl.toString());
}

@Test
public void testConstructorParsing() throws Exception {
exception = ExpectedException.none();
Expand Down

0 comments on commit ee295df

Please sign in to comment.