Skip to content

Commit 5631c5e

Browse files
committed
feat: Include CORS access controls for the web-ui
1 parent 26ed45b commit 5631c5e

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ rusqlite = { version = "0.39", features = ["bundled"] }
4444
dirs = "6.0.0"
4545
minijinja = "2.20.0"
4646
base64 = "0.22.1"
47+
actix-cors = "0.7.1"

src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#![allow(rustdoc::bare_urls)]
22
#![doc = include_str!("../README.md")]
33

4+
use actix_cors::Cors;
5+
use actix_web::http::header;
46
use actix_web::{web, App, HttpServer};
57
use std::sync::Arc;
68
use tokio::sync::RwLock;
@@ -79,7 +81,20 @@ pub async fn start() -> std::io::Result<()> {
7981
);
8082

8183
HttpServer::new(move || {
84+
let mut cors = Cors::default()
85+
.allowed_methods(vec!["GET", "POST", "PUT", "DELETE"])
86+
.allowed_headers(vec![
87+
header::CONTENT_TYPE,
88+
header::AUTHORIZATION,
89+
header::HeaderName::from_static("apikey"),
90+
])
91+
.max_age(3600);
92+
for origin in &config.allowed_origins {
93+
cors = cors.allowed_origin(origin.as_str());
94+
}
95+
8296
App::new()
97+
.wrap(cors)
8398
.app_data(web::Data::new(state.clone()))
8499
.app_data(web::Data::new(pending.clone()))
85100
.app_data(web::Data::new(config.clone()))

src/settings.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ pub struct Config {
5353
pub port: u16,
5454
pub username: String,
5555
pub password: String,
56+
pub allowed_origins: Vec<String>,
5657

5758
pub apikey: String,
5859
pub workers: usize,
@@ -105,6 +106,15 @@ impl Config {
105106
.unwrap();
106107
let username = squire::get_env_var("username", None);
107108
let password = squire::get_env_var("password", None);
109+
let default_allowed = format!(
110+
"http://{host}:{port},http://0.0.0.0:{port},http://localhost:{port}",
111+
host = host,
112+
port = port
113+
);
114+
let allowed_origins = squire::get_env_var("allowed_origins", Some(&default_allowed))
115+
.split(',')
116+
.map(String::from)
117+
.collect();
108118

109119
let apikey = squire::get_env_var("apikey", None);
110120
if apikey.is_empty() {
@@ -193,6 +203,7 @@ impl Config {
193203
port,
194204
username,
195205
password,
206+
allowed_origins,
196207
apikey,
197208
workers,
198209
qbit_url,

0 commit comments

Comments
 (0)