|
| 1 | +use std::{ |
| 2 | + future::{Future, Ready, ready}, |
| 3 | + pin::Pin, |
| 4 | +}; |
| 5 | + |
| 6 | +use actix_web::{ |
| 7 | + Error, |
| 8 | + dev::{Service, ServiceRequest, ServiceResponse, Transform, forward_ready}, |
| 9 | + web, |
| 10 | +}; |
| 11 | + |
| 12 | +use actix_web::http::header::CONTENT_SECURITY_POLICY; |
| 13 | + |
| 14 | +pub struct CSP; |
| 15 | + |
| 16 | +// `S` - type of the next service |
| 17 | +// `B` - type of response's body |
| 18 | +impl<S, B> Transform<S, ServiceRequest> for CSP |
| 19 | +where |
| 20 | + S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>, |
| 21 | + S::Future: 'static, |
| 22 | + B: 'static, |
| 23 | +{ |
| 24 | + type Response = ServiceResponse<B>; |
| 25 | + type Error = Error; |
| 26 | + type InitError = (); |
| 27 | + type Transform = CSPMiddleware<S>; |
| 28 | + type Future = Ready<Result<Self::Transform, Self::InitError>>; |
| 29 | + |
| 30 | + fn new_transform(&self, service: S) -> Self::Future { |
| 31 | + ready(Ok(CSPMiddleware { service })) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +pub struct CSPMiddleware<S> { |
| 36 | + /// The next service to call |
| 37 | + service: S, |
| 38 | +} |
| 39 | + |
| 40 | +// This future doesn't have the requirement of being `Send`. |
| 41 | +// See: futures_util::future::LocalBoxFuture |
| 42 | +type LocalBoxFuture<T> = Pin<Box<dyn Future<Output = T> + 'static>>; |
| 43 | + |
| 44 | +// `S`: type of the wrapped service |
| 45 | +// `B`: type of the body - try to be generic over the body where possible |
| 46 | +impl<S, B> Service<ServiceRequest> for CSPMiddleware<S> |
| 47 | +where |
| 48 | + S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>, |
| 49 | + S::Future: 'static, |
| 50 | + B: 'static, |
| 51 | +{ |
| 52 | + type Response = ServiceResponse<B>; |
| 53 | + type Error = Error; |
| 54 | + type Future = LocalBoxFuture<Result<Self::Response, Self::Error>>; |
| 55 | + |
| 56 | + // This service is ready when its next service is ready |
| 57 | + forward_ready!(service); |
| 58 | + |
| 59 | + fn call(&self, req: ServiceRequest) -> Self::Future { |
| 60 | + let csp_string = if req.path().starts_with("/netzgrafik-frontend") { |
| 61 | + // FIXME: add unsafe-inline for scripts if we don't keep the next commit |
| 62 | + // or, ideally, fix NGE to not require it. |
| 63 | + concat!( |
| 64 | + "default-src 'self'; form-action 'self'; frame-ancestors 'self'; ", |
| 65 | + "connect-src 'self' https://icons.app.sbb.ch; img-src 'self' data:; ", |
| 66 | + "font-src 'self' https://cdn.app.sbb.ch/fonts/ data:; style-src 'self' 'unsafe-inline';" |
| 67 | + ) |
| 68 | + } else { |
| 69 | + "default-src 'self'; frame-ancestors 'self'; form-action 'self'; img-src 'self' data:; font-src 'self' data:; style-src 'self';" |
| 70 | + }; |
| 71 | + let fut = self.service.call(req); |
| 72 | + |
| 73 | + Box::pin(async move { |
| 74 | + let mut res = fut.await?; |
| 75 | + if !res.headers().contains_key(CONTENT_SECURITY_POLICY) { |
| 76 | + res.headers_mut().insert( |
| 77 | + CONTENT_SECURITY_POLICY, |
| 78 | + actix_web::http::header::HeaderValue::from_static(csp_string), |
| 79 | + ); |
| 80 | + } |
| 81 | + Ok(res) |
| 82 | + }) |
| 83 | + } |
| 84 | +} |
0 commit comments