Skip to content

Commit e253d03

Browse files
tsegismonttakeseem
andauthored
feature #2800 CORS: support moz-extension (#2804) (#2810)
Co-authored-by: takeseem <takeseem@users.noreply.github.com>
1 parent 8c71f20 commit e253d03

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

vertx-web/src/main/java/io/vertx/ext/web/impl/Origin.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ private Origin(String protocol, String host, String port, String resource) {
8383
defaultPort = DEFAULT_HTTPS_PORT;
8484
break;
8585
case "chrome-extension":
86+
case "moz-extension":
8687
this.protocol = protocol;
8788
defaultPort = "-1";
8889
break;
@@ -96,6 +97,10 @@ private Origin(String protocol, String host, String port, String resource) {
9697
if (!isValidChromeExtensionId(host, 0)) {
9798
throw new IllegalStateException("Illegal Chrome Extension id: " + host);
9899
}
100+
} else if ("moz-extension".equals(protocol)) {
101+
if (!isValidMozExtensionId(host, 0)) {
102+
throw new IllegalStateException("Illegal Moz Extension id: " + host);
103+
}
99104
} else {
100105
// hosts are either domain names, dot separated or ipv6 like
101106
// https://tools.ietf.org/html/rfc1123
@@ -230,6 +235,8 @@ public static boolean isValid(String text) {
230235
break;
231236
case "chrome-extension":
232237
return isValidChromeExtensionId(text, sep0 + 3);
238+
case "moz-extension":
239+
return isValidMozExtensionId(text, sep0 + 3);
233240
default:
234241
return false;
235242
}
@@ -282,6 +289,19 @@ private static boolean isValidChromeExtensionId(String text, int offset) {
282289
return valid;
283290
}
284291

292+
private static boolean isValidMozExtensionId(String text, int offset) {
293+
boolean valid = text.length() - offset == 36;
294+
for (int i = offset, pos = 0; valid && i < text.length(); i++, pos++) {
295+
char c = text.charAt(i);
296+
if (pos == 8 || pos == 13 || pos == 18 || pos == 23) {
297+
valid = c == '-';
298+
} else {
299+
valid = (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z');
300+
}
301+
}
302+
return valid;
303+
}
304+
285305
private static boolean check(String host, String port) {
286306
if (host == null) {
287307
return false;

vertx-web/src/test/java/io/vertx/ext/web/tests/handler/CORSHandlerTest.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@
2525
import io.vertx.ext.web.tests.WebTestBase;
2626
import org.junit.Test;
2727

28-
import java.util.Arrays;
29-
import java.util.HashSet;
30-
import java.util.LinkedHashSet;
31-
import java.util.Set;
28+
import java.util.*;
3229
import java.util.function.Consumer;
3330

3431
import static org.mockito.ArgumentMatchers.any;
@@ -747,4 +744,12 @@ public void testAcceptChromeExtensionOrigin() throws Exception {
747744
router.route().handler(context -> context.response().end());
748745
testRequest(HttpMethod.GET, "/", req -> req.headers().add("origin", "chrome-extension://gmbgaklkmjakoegficnlkhebmhkjfich"), resp -> checkHeaders(resp, "*", null, null, null), 200, "OK", null);
749746
}
747+
748+
@Test
749+
public void testAcceptMozExtensionOrigin() throws Exception {
750+
router.route().handler(CorsHandler.create().addOriginWithRegex("moz-extension://.*"));
751+
router.route().handler(context -> context.response().end());
752+
String origin = "moz-extension://" + UUID.randomUUID();
753+
testRequest(HttpMethod.GET, "/", req -> req.headers().add("origin", origin), resp -> checkHeaders(resp, origin, null, null, null), 200, "OK", null);
754+
}
750755
}

vertx-web/src/test/java/io/vertx/ext/web/tests/impl/OriginTests.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.junit.runners.Parameterized.Parameters;
99

1010
import java.util.Arrays;
11+
import java.util.UUID;
1112

1213
import static org.junit.Assert.*;
1314

@@ -25,7 +26,8 @@ public static Iterable<Object[]> data() {
2526
{"http://[::1]"},
2627
{"http://my-site.com"},
2728
{"http://my-site.123.com"},
28-
{"chrome-extension://gmbgaklkmjakoegficnlkhebmhkjfich"}
29+
{"chrome-extension://gmbgaklkmjakoegficnlkhebmhkjfich"},
30+
{"moz-extension://" + UUID.randomUUID()}
2931
});
3032
}
3133

@@ -55,6 +57,8 @@ public static Iterable<Object[]> data() {
5557
{"http://1[::1]:8080"},
5658
{"chrome-extension://gmbg0klkmjakoegficnlkhebmhkjfich"},
5759
{"chrome-extension://gmbgaklkmjako"},
60+
{"moz-extension://" + UUID.randomUUID().toString().replace('-', 'z')},
61+
{"moz-extension://" + UUID.randomUUID().toString().substring(1)},
5862
});
5963
}
6064

0 commit comments

Comments
 (0)