Skip to content

Commit 7eb6067

Browse files
committed
better default csp
1 parent e118165 commit 7eb6067

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
- add `text` and `post_html` properties to the [html](https://sql.ophir.dev/documentation.sql?component=html#component) component. This allows to include sanitized user-generated content in the middle of custom HTML.
77
- allow loading javascript ESM modules in the shell component
88
- allow customizing the [Content-Security-Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy) in the configuration.
9-
- the new default content-security-policy is more secure and easier to use. You can now include inline javascript in your custom components with `<script nonce="{{@csp_nonce}}">...</script>`.
9+
- the new default *content security policy* is both more secure and easier to use. You can now include inline javascript in your custom components with `<script nonce="{{@csp_nonce}}">...</script>`.
1010
- update to [sqlparser v0.49.0](https://github.com/sqlparser-rs/sqlparser-rs/blob/main/CHANGELOG.md#0490-2024-07-23)
1111
- support [`WITH ORDINALITY`](https://www.postgresql.org/docs/current/queries-table-expressions.html#QUERIES-TABLEFUNCTIONS) in postgres `FROM` clauses
1212
- update to [handlebars-rs v6](https://github.com/sunng87/handlebars-rust/blob/master/CHANGELOG.md#600---2024-07-20)

src/webserver/http.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -534,21 +534,7 @@ pub fn create_app(
534534
// when receiving a request outside of the prefix, redirect to the prefix
535535
.default_service(fn_service(default_prefix_redirect))
536536
.wrap(Logger::default())
537-
.wrap(
538-
middleware::DefaultHeaders::new()
539-
.add((
540-
"Server",
541-
format!("{} v{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")),
542-
))
543-
.add((
544-
"Content-Security-Policy",
545-
app_state
546-
.config
547-
.content_security_policy
548-
.as_deref()
549-
.unwrap_or("script-src 'self' https://cdn.jsdelivr.net"),
550-
)),
551-
)
537+
.wrap(default_headers(&app_state))
552538
.wrap(middleware::Condition::new(
553539
app_state.config.compress_responses,
554540
middleware::Compress::default(),
@@ -560,6 +546,15 @@ pub fn create_app(
560546
.app_data(app_state)
561547
}
562548

549+
fn default_headers(app_state: &web::Data<AppState>) -> middleware::DefaultHeaders {
550+
let server_header = format!("{} v{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
551+
let mut headers = middleware::DefaultHeaders::new().add(("Server", server_header));
552+
if let Some(csp) = &app_state.config.content_security_policy {
553+
headers = headers.add(("Content-Security-Policy", csp.as_str()));
554+
}
555+
headers
556+
}
557+
563558
pub async fn run_server(config: &AppConfig, state: AppState) -> anyhow::Result<()> {
564559
let listen_on = config.listen_on();
565560
let state = web::Data::new(state);

0 commit comments

Comments
 (0)