Skip to content

Commit 3f836b1

Browse files
authored
bug: fix CORS issue (#1447)
1 parent b9c1f7f commit 3f836b1

File tree

5 files changed

+26
-8
lines changed

5 files changed

+26
-8
lines changed

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ jobs:
195195
MYSQL_USER: test
196196
MYSQL_PASSWORD: test
197197
MYSQL_DATABASE: syncstorage
198+
resource_class: large
198199
steps:
199200
- setup_remote_docker:
200201
docker_layer_caching: true

syncserver-settings/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ impl Default for Settings {
176176
statsd_host: Some("localhost".to_owned()),
177177
statsd_port: 8125,
178178
human_logs: false,
179-
cors_allowed_origin: None,
179+
cors_allowed_origin: Some("*".to_owned()),
180180
cors_allowed_methods: Some(
181181
["DELETE", "GET", "POST", "PUT"]
182182
.into_iter()
@@ -187,7 +187,7 @@ impl Default for Settings {
187187
[
188188
"Authorization",
189189
"Content-Type",
190-
"UserAgent",
190+
"User-Agent",
191191
X_LAST_MODIFIED,
192192
X_WEAVE_TIMESTAMP,
193193
X_WEAVE_NEXT_OFFSET,
@@ -202,7 +202,7 @@ impl Default for Settings {
202202
.map(String::from)
203203
.collect(),
204204
),
205-
cors_max_age: None,
205+
cors_max_age: Some(1728000),
206206
syncstorage: SyncstorageSettings::default(),
207207
tokenserver: TokenserverSettings::default(),
208208
}

syncserver/src/server/mod.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,10 +376,6 @@ fn build_cors(settings: &Settings) -> Cors {
376376
// for finer grained specification.
377377
let mut cors = Cors::default();
378378

379-
if let Some(allowed_origin) = &settings.cors_allowed_origin {
380-
cors = cors.allowed_origin(allowed_origin);
381-
}
382-
383379
if let Some(allowed_methods) = &settings.cors_allowed_methods {
384380
let mut methods = vec![];
385381
for method_string in allowed_methods {
@@ -396,6 +392,16 @@ fn build_cors(settings: &Settings) -> Cors {
396392
cors = cors.max_age(*max_age);
397393
}
398394

395+
// explicitly set the CORS allow origin, since Default does not
396+
// appear to set the `allow-origins: *` header.
397+
if let Some(ref origin) = settings.cors_allowed_origin {
398+
if origin == "*" {
399+
cors = cors.allow_any_origin();
400+
} else {
401+
cors = cors.allowed_origin(origin);
402+
}
403+
}
404+
399405
cors
400406
}
401407

tools/integration_tests/run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def start_server():
4949

5050
os.environ.setdefault("SYNC_MASTER_SECRET", "secret0")
5151
os.environ.setdefault("SYNC_CORS_MAX_AGE", "555")
52-
os.environ.setdefault("SYNC_CORS_ALLOWED_ORIGIN", "localhost")
52+
os.environ.setdefault("SYNC_CORS_ALLOWED_ORIGIN", "*")
5353
mock_fxa_server_url = os.environ["MOCK_FXA_SERVER_URL"]
5454
url = "%s/v2" % mock_fxa_server_url
5555
os.environ["SYNC_TOKENSERVER__FXA_BROWSERID_SERVER_URL"] = url

tools/integration_tests/test_storage.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2266,6 +2266,17 @@ def test_cors_settings_are_set(self):
22662266
res.headers["access-control-allow-origin"], "localhost"
22672267
)
22682268

2269+
def test_cors_allows_any_origin(self):
2270+
self.app.options(
2271+
self.root + "/__heartbeat__",
2272+
headers={
2273+
"Access-Control-Request-Method": "GET",
2274+
"Origin": "http://test-website.com",
2275+
"Access-Control-Request-Headers": "Content-Type"
2276+
},
2277+
status=200
2278+
)
2279+
22692280
# PATCH is not a default allowed method, so request should return 405
22702281
def test_patch_is_not_allowed(self):
22712282
collection = self.root + "/storage/xxx_col1"

0 commit comments

Comments
 (0)