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

#150 Prevent invalid URL generation #151

Closed
wants to merge 2 commits into from
Closed
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
15 changes: 9 additions & 6 deletions src/main/java/com/github/packageurl/PackageURL.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.io.Serializable;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -437,23 +436,27 @@ private String canonicalize(boolean coordinatesOnly) {
* @return an encoded String
*/
private String percentEncode(final String input) {
return uriEncode(input, StandardCharsets.UTF_8);
return uriEncode(input);
}

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

StringBuilder builder = new StringBuilder();
for (byte b : source.getBytes(charset)) {
for (byte b : source.getBytes(StandardCharsets.UTF_8)) {
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());
String hexValue = Integer.toHexString(b).toUpperCase();
if (hexValue.length() == 1) {
builder.append('0');
}
builder.append(hexValue);
}
}
return builder.toString();
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/com/github/packageurl/PackageURLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.TreeMap;

import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -329,4 +332,18 @@ public void testNpmCaseSensitive() throws Exception {
Assert.assertEquals(base64Uppercase.getName(), "Base64");
Assert.assertEquals(base64Uppercase.getVersion(), "1.0.0");
}

@Test
public void testGeneratesValidUrlWithNewLineInVersionNumber() throws MalformedPackageURLException, UnsupportedEncodingException {
// Given
PackageURL url = new PackageURL("maven", "com.google.summit", "summit-ast", "2.2.0\n", null, null);

// When
String output = url.canonicalize();

// Then
String decoded = URLDecoder.decode(output, StandardCharsets.UTF_8.name());
Assert.assertEquals("pkg:maven/com.google.summit/[email protected]%0A", output);
Assert.assertEquals("pkg:maven/com.google.summit/[email protected]\n", decoded);
}
}