Skip to content

Commit c4d78b8

Browse files
committed
http: add support for reqwest's danger flags
1 parent 802399a commit c4d78b8

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

plugins/http/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,4 @@ charset = ["reqwest/charset"]
7373
macos-system-configuration = ["reqwest/macos-system-configuration"]
7474
unsafe-headers = []
7575
tracing = ["dep:tracing"]
76+
dangerous-settings = []

plugins/http/guest-js/index.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ export interface ClientOptions {
8484
* Configuration of a proxy that a Client should pass requests to.
8585
*/
8686
proxy?: Proxy
87+
/**
88+
* Configuration for dangerous settings on the client such as disabling SSL verification.
89+
*/
90+
danger?: DangerousSettings
91+
}
92+
93+
/**
94+
* Configuration for dangerous settings on the client such as disabling SSL verification.
95+
*
96+
* @since 2.2.0
97+
*/
98+
export interface DangerousSettings {
99+
/**
100+
* Disables SSL verification.
101+
*/
102+
acceptInvalidCerts?: boolean,
103+
/**
104+
* Disables hostname verification.
105+
*/
106+
acceptInvalidHostnames?: boolean
87107
}
88108

89109
const ERROR_REQUEST_CANCELLED = 'Request canceled'
@@ -115,12 +135,14 @@ export async function fetch(
115135
const maxRedirections = init?.maxRedirections
116136
const connectTimeout = init?.connectTimeout
117137
const proxy = init?.proxy
138+
const danger = init?.danger
118139

119140
// Remove these fields before creating the request
120141
if (init) {
121142
delete init.maxRedirections
122143
delete init.connectTimeout
123144
delete init.proxy
145+
delete init.danger
124146
}
125147

126148
const headers = init?.headers
@@ -172,7 +194,8 @@ export async function fetch(
172194
data,
173195
maxRedirections,
174196
connectTimeout,
175-
proxy
197+
proxy,
198+
danger
176199
}
177200
})
178201

plugins/http/src/commands.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ pub struct FetchResponse {
7575
rid: ResourceId,
7676
}
7777

78+
#[derive(Debug, Deserialize)]
79+
#[serde(rename_all = "camelCase")]
80+
pub struct DangerousSettings {
81+
accept_invalid_certs: bool,
82+
accept_invalid_hostnames: bool,
83+
}
84+
7885
#[derive(Debug, Deserialize)]
7986
#[serde(rename_all = "camelCase")]
8087
pub struct ClientConfig {
@@ -85,6 +92,7 @@ pub struct ClientConfig {
8592
connect_timeout: Option<u64>,
8693
max_redirections: Option<usize>,
8794
proxy: Option<Proxy>,
95+
danger: Option<DangerousSettings>,
8896
}
8997

9098
#[derive(Debug, Deserialize)]
@@ -181,6 +189,7 @@ pub async fn fetch<R: Runtime>(
181189
connect_timeout,
182190
max_redirections,
183191
proxy,
192+
danger,
184193
} = client_config;
185194

186195
let scheme = url.scheme();
@@ -220,6 +229,23 @@ pub async fn fetch<R: Runtime>(
220229
{
221230
let mut builder = reqwest::ClientBuilder::new();
222231

232+
if let Some(danger_config) = danger {
233+
#[cfg(not(feature = "dangerous-settings"))]
234+
{
235+
#[cfg(debug_assertions)]
236+
{
237+
eprintln!("[\x1b[33mWARNING\x1b[0m] using dangerous settings requires `dangerous-settings` feature flag in your Cargo.toml");
238+
}
239+
let _ = danger_config;
240+
return Err(Error::DangerousSettings);
241+
}
242+
#[cfg(feature = "dangerous-settings")]{
243+
builder = builder
244+
.danger_accept_invalid_certs(danger_config.accept_invalid_certs)
245+
.danger_accept_invalid_hostnames(danger_config.accept_invalid_hostnames)
246+
}
247+
}
248+
223249
if let Some(timeout) = connect_timeout {
224250
builder = builder.connect_timeout(Duration::from_millis(timeout));
225251
}

plugins/http/src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ pub enum Error {
4141
Tauri(#[from] tauri::Error),
4242
#[error(transparent)]
4343
Utf8(#[from] std::string::FromUtf8Error),
44+
#[error("dangerous settings used but are not enabled")]
45+
DangerousSettings,
4446
}
4547

4648
impl Serialize for Error {

0 commit comments

Comments
 (0)