Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

URI encode/decode in conformance with RFC 3986 #98

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 55 additions & 20 deletions src/main/java/com/github/packageurl/PackageURL.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@
package com.github.packageurl;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -55,7 +53,6 @@
public final class PackageURL implements Serializable {

private static final long serialVersionUID = 3243226021636427586L;
private static final String UTF8 = StandardCharsets.UTF_8.name();
private static final Pattern PATH_SPLITTER = Pattern.compile("/");

/**
Expand Down Expand Up @@ -432,16 +429,38 @@ private String canonicalize(boolean coordinatesOnly) {
* @return an encoded String
*/
private String percentEncode(final String input) {
try {
return URLEncoder.encode(input, UTF8)
.replace("+", "%20")
// "*" is a reserved character in RFC 3986.
.replace("*", "%2A")
// "~" is an unreserved character in RFC 3986.
.replace("%7E", "~");
} catch (UnsupportedEncodingException e) {
return input; // this should never occur
return uriEncode(input, StandardCharsets.UTF_8);
}

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

StringBuilder builder = new StringBuilder();
for (byte b : source.getBytes(charset)) {
if (isUnreserved(b)) {
builder.append((char) b);
}
else {
// Substitution: A '%' followed by the hexadecimal representation of the ASCII value of the replaced character
builder.append('%');
builder.append(Integer.toHexString(b).toUpperCase());
}
}
return builder.toString();
}

private static boolean isUnreserved(int c) {
return (isAlpha(c) || isDigit(c) || '-' == c || '.' == c || '_' == c || '~' == c);
}

private static boolean isAlpha(int c) {
return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
}

private static boolean isDigit(int c) {
return (c >= '0' && c <= '9');
}

/**
Expand All @@ -455,17 +474,33 @@ private String percentDecode(final String input) {
if (input == null) {
return null;
}
try {
final String decoded = URLDecoder.decode(input, UTF8);
if (!decoded.equals(input)) {
return decoded;
}
} catch (UnsupportedEncodingException e) {
return input; // this should never occur
final String decoded = uriDecode(input);
if (!decoded.equals(input)) {
return decoded;
}
return input;
}

public static String uriDecode(String source) {
if (source == null) {
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));
}
}
return builder.toString();
}

/**
* Given a specified PackageURL, this method will parse the purl and populate this classes
* instance fields so that the corresponding getters may be called to retrieve the individual
Expand Down
12 changes: 12 additions & 0 deletions src/test/resources/test-suite-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,18 @@
"subpath": null,
"is_invalid": false
},
{
"description": "valid debian purl containing a plus in the name and version",
"purl": "pkg:deb/debian/[email protected]+6",
"canonical_purl": "pkg:deb/debian/g%2B%[email protected]%2B6",
"type": "deb",
"namespace": "debian",
"name": "g++-10",
"version": "10.2.1+6",
"qualifiers": null,
"subpath": null,
"is_invalid": false
},
{
"description": "checks for invalid qualifier keys",
"purl": "pkg:npm/[email protected]?in%20production=true",
Expand Down
Loading