Skip to content

Commit

Permalink
Merge pull request #462 from RachelTucker/3_2_14_BPBROWSER-93
Browse files Browse the repository at this point in the history
BPBROWSER 93- Percent encoding query parameters
  • Loading branch information
Denver authored Mar 29, 2017
2 parents 129ee46 + 5017333 commit 3d24509
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,27 @@ public void getObjectsWithPagination() throws IOException, URISyntaxException {

}

@Test
public void getBucketWithAmpersandFolderName() throws IOException, URISyntaxException {
final String bucketName = "test_ampersand_folder";
final String folderName = "test&folder/";
try {
HELPERS.ensureBucketExists(bucketName, envDataPolicyId);
loadBookTestDataWithPrefix(client, bucketName, folderName);

final HeadObjectResponse response = client.headObject(new HeadObjectRequest(
bucketName, folderName + "beowulf.txt"));
assertThat(response.getStatus(),
is(HeadObjectResponse.Status.EXISTS));

final GetBucketResponse getBucket = client.getBucket(new GetBucketRequest(bucketName));
assertThat(getBucket.getListBucketResult(), is(notNullValue()));
assertThat(getBucket.getListBucketResult().getObjects().size(), is(4));
} finally {
deleteAllContents(client, bucketName);
}
}

private static boolean s3ObjectExists(final List<S3Object> objects, final String fileName) {
for (final S3Object obj : objects) {
if (obj.getName().equals(fileName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected final <T> void updateQueryParam(final String name, final T param) {
this.queryParams.remove(name);
}
else {
this.queryParams.put(name, SafeStringManipulation.safeUrlEscape(param));
this.queryParams.put(name, SafeStringManipulation.safeQueryParamEscape(param));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,34 @@ public final class SafeStringManipulation {
"!$'()*,&=" + // removed ; (so it will be escaped) and added / (so it will not)
"@:/"; // Their urlFragmentEscaper uses URL_PATH_OTHER_SAFE_CHARS_LACKING_PLUS + "+/?"+

/**
* Specified as query safe characters in spec https://tools.ietf.org/html/rfc3986#section-3.4
* with the following exceptions:
* Encoding: "&", ":", "+", "=" as they have special meaning
* Not Encoding: "/"
*/
private static final String DS3_QUERY_PARAM_SAFE_CHARS = "-._~!$'()*,;@/";

private static final Escaper DS3_URL_FRAGMENT_ESCAPER =
new PercentEscaper(DS3_URL_PATH_FRAGMENT_SAFE_CHARS, false);

private static final Escaper DS3_QUERY_PARAM_ESCAPER =
new PercentEscaper(DS3_QUERY_PARAM_SAFE_CHARS, false);

private SafeStringManipulation() {
//pass
}

/**
* Percent encodes user-provided query parameter values.
*/
public static <T> String safeQueryParamEscape(final T obj) {
if (obj == null) {
return null;
}
return DS3_QUERY_PARAM_ESCAPER.escape(safeToString(obj));
}

public static <T> String safeUrlEscape(final T obj) {
if (obj == null) {
return null;
Expand All @@ -58,6 +79,7 @@ public static <T> String safeToString(final T obj) {
}
return obj.toString();
}

public static Escaper getDs3Escaper() {
// escaped characters in DS3 path and query parameter value segments
return DS3_URL_FRAGMENT_ESCAPER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,13 @@ public void safeUrlEscape_Test() {
assertThat(SafeStringManipulation.safeUrlEscape("one2three+four"), is("one2three%2Bfour"));
assertThat(SafeStringManipulation.safeUrlEscape("one two three"), is("one%20two%20three"));
}

@Test
public void safeQueryParamEscape_Test() {
assertThat(SafeStringManipulation.safeQueryParamEscape(null), is(nullValue()));
assertThat(SafeStringManipulation.safeQueryParamEscape(""), is(""));
assertThat(SafeStringManipulation.safeQueryParamEscape("one2three+four"), is("one2three%2Bfour"));
assertThat(SafeStringManipulation.safeQueryParamEscape("one two three"), is("one%20two%20three"));
assertThat(SafeStringManipulation.safeQueryParamEscape("one&two&three"), is("one%26two%26three"));
}
}

0 comments on commit 3d24509

Please sign in to comment.