Skip to content

Commit d3bae78

Browse files
committed
chore(guard): centralize cors config
1 parent 7b579e8 commit d3bae78

File tree

3 files changed

+44
-17
lines changed

3 files changed

+44
-17
lines changed

engine/packages/guard-core/src/proxy_service.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,28 @@ impl ProxyService {
581581
HeaderValue::from_str(&cors.expose_headers)?,
582582
);
583583

584+
if let Some(allow_methods) = &cors.allow_methods {
585+
headers.insert(
586+
"access-control-allow-methods",
587+
HeaderValue::from_str(allow_methods)?,
588+
);
589+
}
590+
591+
if let Some(allow_headers) = &cors.allow_headers {
592+
headers.insert(
593+
"access-control-allow-headers",
594+
HeaderValue::from_str(allow_headers)?,
595+
);
596+
}
597+
598+
if let Some(max_age) = &cors.max_age {
599+
headers.insert(
600+
"access-control-max-age",
601+
HeaderValue::from_str(&max_age.to_string())?,
602+
);
603+
}
604+
605+
// Add Vary header to prevent cache poisoning when echoing origin
584606
if cors.allow_origin != "*" {
585607
headers.insert("vary", HeaderValue::from_static("Origin"));
586608
}

engine/packages/guard-core/src/request_context.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,11 @@ pub struct CorsConfig {
150150
pub allow_origin: String,
151151
pub allow_credentials: bool,
152152
pub expose_headers: String,
153+
154+
// Only set for OPTIONS requests
155+
// TODO: Vec of Method
156+
pub allow_methods: Option<String>,
157+
pub allow_headers: Option<String>,
158+
// Seconds
159+
pub max_age: Option<u32>,
153160
}

engine/packages/pegboard-gateway/src/lib.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -132,31 +132,29 @@ impl PegboardGateway {
132132
.and_then(|v| v.to_str().ok())
133133
.unwrap_or("*");
134134

135-
let mut response = Response::builder()
135+
req_ctx.set_cors(CorsConfig {
136+
allow_origin: origin.clone(),
137+
allow_credentials: true,
138+
expose_headers: "*".to_string(),
139+
allow_methods: Some("GET, POST, PUT, DELETE, OPTIONS, PATCH".to_string()),
140+
allow_headers: Some(requested_headers.to_string()),
141+
max_age: Some(86400),
142+
});
143+
144+
return Ok(Response::builder()
136145
.status(StatusCode::NO_CONTENT)
137-
.header("access-control-allow-origin", &origin)
138-
.header("access-control-allow-credentials", "true")
139-
.header(
140-
"access-control-allow-methods",
141-
"GET, POST, PUT, DELETE, OPTIONS, PATCH",
142-
)
143-
.header("access-control-allow-headers", requested_headers)
144-
.header("access-control-expose-headers", "*")
145-
.header("access-control-max-age", "86400");
146-
147-
// Add Vary header to prevent cache poisoning when echoing origin
148-
if origin != "*" {
149-
response = response.header("vary", "Origin");
150-
}
151-
152-
return Ok(response.body(ResponseBody::Full(Full::new(Bytes::new())))?);
146+
.body(ResponseBody::Full(Full::new(Bytes::new())))?);
153147
}
154148

155149
// Set CORS headers through guard
156150
req_ctx.set_cors(CorsConfig {
157151
allow_origin: origin.clone(),
158152
allow_credentials: true,
159153
expose_headers: "*".to_string(),
154+
// Not an options req, not required
155+
allow_methods: None,
156+
allow_headers: None,
157+
max_age: None,
160158
});
161159

162160
let body_bytes = req

0 commit comments

Comments
 (0)