From 8041f8859ff2da09a643e3050d8c5ca721b118aa Mon Sep 17 00:00:00 2001
From: JesseLovelace <43148100+JesseLovelace@users.noreply.github.com>
Date: Tue, 20 Jul 2021 11:03:46 -0700
Subject: [PATCH 1/2] feat: fix post policy escape bug, update conformance
tests
---
.../google/cloud/storage/PostPolicyV4.java | 12 +++++++--
.../cloud/storage/V4PostPolicyTest.java | 25 ++++++++-----------
pom.xml | 2 +-
3 files changed, 22 insertions(+), 17 deletions(-)
diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/PostPolicyV4.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/PostPolicyV4.java
index 96afca06e1..25302a4c1b 100644
--- a/google-cloud-storage/src/main/java/com/google/cloud/storage/PostPolicyV4.java
+++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/PostPolicyV4.java
@@ -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");
diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/V4PostPolicyTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/V4PostPolicyTest.java
index 161fb36404..fc7f59f921 100644
--- a/google-cloud-storage/src/test/java/com/google/cloud/storage/V4PostPolicyTest.java
+++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/V4PostPolicyTest.java
@@ -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;
@@ -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(
@@ -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");
@@ -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"))));
diff --git a/pom.xml b/pom.xml
index 1fdcedce9a..e12c5be7f8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -93,7 +93,7 @@
com.google.cloud
google-cloud-conformance-tests
- 0.0.11
+ 0.1.1
test
From 5c981e2981e12f9ad41be10f2a66566823e9b4eb Mon Sep 17 00:00:00 2001
From: JesseLovelace <43148100+JesseLovelace@users.noreply.github.com>
Date: Fri, 20 Aug 2021 11:37:52 -0700
Subject: [PATCH 2/2] fix typo
---
.../src/main/java/com/google/cloud/storage/PostPolicyV4.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/PostPolicyV4.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/PostPolicyV4.java
index 25302a4c1b..4abcb4fd33 100644
--- a/google-cloud-storage/src/main/java/com/google/cloud/storage/PostPolicyV4.java
+++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/PostPolicyV4.java
@@ -451,7 +451,7 @@ public String toJson() {
} else {
switch (c) {
case '\\':
- // The JsonObject/JsonArray operations above handle quote escapes, so leave any "/""
+ // The JsonObject/JsonArray operations above handle quote escapes, so leave any "\""
// found alone
if (jsonArray[i + 1] == '"') {
escapedJson.append("\\");