Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat: fix post policy escape bug, update conformance tests (#924)
* feat: fix post policy escape bug, update conformance tests

* fix typo
  • Loading branch information
JesseLovelace committed Aug 20, 2021
1 parent 69b996b commit d8329c3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
Expand Up @@ -443,13 +443,21 @@ public String toJson() {
StringBuilder escapedJson = new StringBuilder();

// Certain characters in a policy must be escaped
for (char c : json.toCharArray()) {
char[] jsonArray = json.toCharArray();
for (int i = 0; i < jsonArray.length; i++) {
char c = jsonArray[i];
if (c >= 128) { // is a unicode character
escapedJson.append(String.format("\\u%04x", (int) c));
} else {
switch (c) {
case '\\':
escapedJson.append("\\\\");
// The JsonObject/JsonArray operations above handle quote escapes, so leave any "\""
// found alone
if (jsonArray[i + 1] == '"') {
escapedJson.append("\\");
} else {
escapedJson.append("\\\\");
}
break;
case '\b':
escapedJson.append("\\b");
Expand Down
Expand Up @@ -28,7 +28,6 @@
import com.google.cloud.conformance.storage.v1.UrlStyle;
import com.google.cloud.storage.testing.RemoteStorageHelper;
import com.google.common.base.Charsets;
import com.google.common.base.Strings;
import com.google.common.io.BaseEncoding;
import com.google.protobuf.Timestamp;
import com.google.protobuf.util.JsonFormat;
Expand Down Expand Up @@ -117,17 +116,6 @@ public void test() {

PolicyConditions conditions = policyInput.getConditions();

if (!Strings.isNullOrEmpty(fields.get("success_action_redirect"))) {
builder.addSuccessActionRedirectUrlCondition(
PostPolicyV4.ConditionV4Type.MATCHES, fields.get("success_action_redirect"));
}

if (!Strings.isNullOrEmpty(fields.get("success_action_status"))) {
builder.addSuccessActionStatusCondition(
PostPolicyV4.ConditionV4Type.MATCHES,
Integer.parseInt(fields.get("success_action_status")));
}

if (conditions != null) {
if (!conditions.getStartsWithList().isEmpty()) {
builder.addCustomCondition(
Expand Down Expand Up @@ -166,17 +154,25 @@ public void test() {
style);

String expectedPolicy = testData.getPolicyOutput().getExpectedDecodedPolicy();

StringBuilder escapedPolicy = new StringBuilder();

// Java automatically unescapes the unicode escapes in the conformance tests, so we need to
// manually re-escape them
for (char c : expectedPolicy.toCharArray()) {
char[] expectedPolicyArray = expectedPolicy.toCharArray();
for (int i = 0; i < expectedPolicyArray.length; i++) {
char c = expectedPolicyArray[i];
if (c >= 128) {
escapedPolicy.append(String.format("\\u%04x", (int) c));
} else {
switch (c) {
case '\\':
escapedPolicy.append("\\\\");
// quotes aren't unescaped, so leave any "\"" found alone
if (expectedPolicyArray[i + 1] == '"') {
escapedPolicy.append("\\");
} else {
escapedPolicy.append("\\\\");
}
break;
case '\b':
escapedPolicy.append("\\b");
Expand All @@ -202,6 +198,7 @@ public void test() {
}
}
assertEquals(testData.getPolicyOutput().getFieldsMap(), policy.getFields());

assertEquals(
escapedPolicy.toString(),
new String(BaseEncoding.base64().decode(policy.getFields().get("policy"))));
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -108,7 +108,7 @@
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-conformance-tests</artifactId>
<version>0.0.11</version>
<version>0.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down

0 comments on commit d8329c3

Please sign in to comment.