client-api: Send CORP headers by default for media responses

According to MSC3828.
This commit is contained in:
Kévin Commaille 2022-10-02 16:24:57 +02:00 committed by Kévin Commaille
parent 739c35aaca
commit e6e7e52034
11 changed files with 72 additions and 6 deletions

View File

@ -17,6 +17,7 @@ Improvements:
* Stabilize support for private read receipts * Stabilize support for private read receipts
* Add support for the pagination direction parameter to `/relations` (MSC3715 / Matrix 1.4) * Add support for the pagination direction parameter to `/relations` (MSC3715 / Matrix 1.4)
* Add support for notifications for threads (MSC3773 / Matrix 1.4) * Add support for notifications for threads (MSC3773 / Matrix 1.4)
* Send CORP headers by default for media responses (MSC3828 / Matrix 1.4)
# 0.15.1 # 0.15.1

View File

@ -0,0 +1,10 @@
//! Custom HTTP headers not defined in the `http` crate.
#![allow(clippy::declare_interior_mutable_const)]
use http::header::HeaderName;
/// The [`Cross-Origin-Resource-Policy`] HTTP response header.
///
/// [`Cross-Origin-Resource-Policy`]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Resource-Policy
pub const CROSS_ORIGIN_RESOURCE_POLICY: HeaderName =
HeaderName::from_static("cross-origin-resource-policy");

View File

@ -20,6 +20,7 @@ pub mod directory;
pub mod discovery; pub mod discovery;
pub mod error; pub mod error;
pub mod filter; pub mod filter;
pub mod http_headers;
pub mod keys; pub mod keys;
pub mod knock; pub mod knock;
pub mod media; pub mod media;

View File

@ -5,6 +5,7 @@ pub mod unstable {
//! //!
//! [spec]: https://github.com/tulir/matrix-doc/blob/asynchronous_uploads/proposals/2246-asynchronous-uploads.md //! [spec]: https://github.com/tulir/matrix-doc/blob/asynchronous_uploads/proposals/2246-asynchronous-uploads.md
use http::header::CONTENT_TYPE;
use ruma_common::{api::ruma_api, IdParseError, MxcUri, ServerName}; use ruma_common::{api::ruma_api, IdParseError, MxcUri, ServerName};
ruma_api! { ruma_api! {

View File

@ -10,6 +10,8 @@ pub mod v3 {
use js_int::UInt; use js_int::UInt;
use ruma_common::{api::ruma_api, IdParseError, MxcUri, ServerName}; use ruma_common::{api::ruma_api, IdParseError, MxcUri, ServerName};
use crate::http_headers::CROSS_ORIGIN_RESOURCE_POLICY;
ruma_api! { ruma_api! {
metadata: { metadata: {
description: "Retrieve content from the media store.", description: "Retrieve content from the media store.",
@ -71,6 +73,14 @@ pub mod v3 {
/// [MDN]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#Syntax /// [MDN]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#Syntax
#[ruma_api(header = CONTENT_DISPOSITION)] #[ruma_api(header = CONTENT_DISPOSITION)]
pub content_disposition: Option<String>, pub content_disposition: Option<String>,
/// The value of the `Cross-Origin-Resource-Policy` HTTP header.
///
/// See [MDN] for the syntax.
///
/// [MDN]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Resource-Policy#syntax
#[ruma_api(header = CROSS_ORIGIN_RESOURCE_POLICY)]
pub cross_origin_resource_policy: Option<String>,
} }
error: crate::Error error: crate::Error
@ -98,8 +108,15 @@ pub mod v3 {
impl Response { impl Response {
/// Creates a new `Response` with the given file contents. /// Creates a new `Response` with the given file contents.
///
/// The Cross-Origin Resource Policy defaults to `cross-origin`.
pub fn new(file: Vec<u8>) -> Self { pub fn new(file: Vec<u8>) -> Self {
Self { file, content_type: None, content_disposition: None } Self {
file,
content_type: None,
content_disposition: None,
cross_origin_resource_policy: Some("cross-origin".to_owned()),
}
} }
} }
} }

View File

@ -8,6 +8,8 @@ pub mod v3 {
use http::header::{CONTENT_DISPOSITION, CONTENT_TYPE}; use http::header::{CONTENT_DISPOSITION, CONTENT_TYPE};
use ruma_common::{api::ruma_api, IdParseError, MxcUri, ServerName}; use ruma_common::{api::ruma_api, IdParseError, MxcUri, ServerName};
use crate::http_headers::CROSS_ORIGIN_RESOURCE_POLICY;
ruma_api! { ruma_api! {
metadata: { metadata: {
description: "Retrieve content from the media store, specifying a filename to return.", description: "Retrieve content from the media store, specifying a filename to return.",
@ -58,6 +60,14 @@ pub mod v3 {
/// [MDN]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#Syntax /// [MDN]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#Syntax
#[ruma_api(header = CONTENT_DISPOSITION)] #[ruma_api(header = CONTENT_DISPOSITION)]
pub content_disposition: Option<String>, pub content_disposition: Option<String>,
/// The value of the `Cross-Origin-Resource-Policy` HTTP header.
///
/// See [MDN] for the syntax.
///
/// [MDN]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Resource-Policy#syntax
#[ruma_api(header = CROSS_ORIGIN_RESOURCE_POLICY)]
pub cross_origin_resource_policy: Option<String>,
} }
error: crate::Error error: crate::Error
@ -79,8 +89,15 @@ pub mod v3 {
impl Response { impl Response {
/// Creates a new `Response` with the given file. /// Creates a new `Response` with the given file.
///
/// The Cross-Origin Resource Policy defaults to `cross-origin`.
pub fn new(file: Vec<u8>) -> Self { pub fn new(file: Vec<u8>) -> Self {
Self { file, content_type: None, content_disposition: None } Self {
file,
content_type: None,
content_disposition: None,
cross_origin_resource_policy: Some("cross-origin".to_owned()),
}
} }
} }
} }

View File

@ -9,7 +9,7 @@ pub mod v3 {
use js_int::UInt; use js_int::UInt;
use ruma_common::{api::ruma_api, serde::StringEnum, IdParseError, MxcUri, ServerName}; use ruma_common::{api::ruma_api, serde::StringEnum, IdParseError, MxcUri, ServerName};
use crate::PrivOwnedStr; use crate::{http_headers::CROSS_ORIGIN_RESOURCE_POLICY, PrivOwnedStr};
ruma_api! { ruma_api! {
metadata: { metadata: {
@ -79,6 +79,14 @@ pub mod v3 {
/// The content type of the thumbnail. /// The content type of the thumbnail.
#[ruma_api(header = CONTENT_TYPE)] #[ruma_api(header = CONTENT_TYPE)]
pub content_type: Option<String>, pub content_type: Option<String>,
/// The value of the `Cross-Origin-Resource-Policy` HTTP header.
///
/// See [MDN] for the syntax.
///
/// [MDN]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Resource-Policy#syntax
#[ruma_api(header = CROSS_ORIGIN_RESOURCE_POLICY)]
pub cross_origin_resource_policy: Option<String>,
} }
error: crate::Error error: crate::Error
@ -116,8 +124,14 @@ pub mod v3 {
impl Response { impl Response {
/// Creates a new `Response` with the given thumbnail. /// Creates a new `Response` with the given thumbnail.
///
/// The Cross-Origin Resource Policy defaults to `cross-origin`.
pub fn new(file: Vec<u8>) -> Self { pub fn new(file: Vec<u8>) -> Self {
Self { file, content_type: None } Self {
file,
content_type: None,
cross_origin_resource_policy: Some("cross-origin".to_owned()),
}
} }
} }

View File

@ -1,3 +1,4 @@
use http::header::CONTENT_TYPE;
use ruma_common::{ use ruma_common::{
api::ruma_api, api::ruma_api,
events::{tag::TagEvent, AnyTimelineEvent}, events::{tag::TagEvent, AnyTimelineEvent},

View File

@ -2,6 +2,7 @@
// consume the request/response. // consume the request/response.
mod newtype_body { mod newtype_body {
use http::header::CONTENT_TYPE;
use ruma_common::{api::ruma_api, OwnedUserId}; use ruma_common::{api::ruma_api, OwnedUserId};
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
@ -42,6 +43,7 @@ mod newtype_body {
} }
mod raw_body { mod raw_body {
use http::header::CONTENT_TYPE;
use ruma_common::{api::ruma_api, OwnedUserId}; use ruma_common::{api::ruma_api, OwnedUserId};
ruma_api! { ruma_api! {
@ -79,6 +81,7 @@ mod raw_body {
} }
mod plain { mod plain {
use http::header::CONTENT_TYPE;
use ruma_common::{api::ruma_api, OwnedUserId}; use ruma_common::{api::ruma_api, OwnedUserId};
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]

View File

@ -1,3 +1,4 @@
use http::header::CONTENT_TYPE;
use ruma_common::api::ruma_api; use ruma_common::api::ruma_api;
ruma_api! { ruma_api! {

View File

@ -1,5 +1,5 @@
error: cannot find attribute `not_a_real_attribute_should_fail` in this scope error: cannot find attribute `not_a_real_attribute_should_fail` in this scope
--> $DIR/04-attributes.rs:13:7 --> $DIR/04-attributes.rs:14:7
| |
13 | #[not_a_real_attribute_should_fail] 14 | #[not_a_real_attribute_should_fail]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^