tauri-apps / tauri-apps/plugins-workspace

[bug] request got a 403 response using tauri_plugin_http and unsafe_headers feature

Open
#1,968 9 comments 1 reaction 0 assignees View on GitHub
plugin: http type: bug
Dominant language
Rust
Stars
1.8k
Forks
602
Avg merge
4d 14h
Merged PRs (30d)
9

Description

## Describe the bug

I'm using tauriv2 and tauri_plugin_http in a tauri + vue + rust project.

The url example: `https://api.bilibili.com/x/web-interface/nav`

when I use curl, it works fine:
```shell
$curl https://api.bilibili.com/x/web-interface/nav
{"code":-101,"message":"账号未登录","ttl":1,"data":{"isLogin":false,"wbi_img":{"img_url":"https://i0.hdslb.com/bfs/wbi/7cd084941338484aae1ad9425b84077c.png","sub_url":"https://i0.hdslb.com/bfs/wbi/4932caff0ff746eab6f01bf08b70ac45.png"}}}
```

reqwest also works

```rust
use reqwest::{get, Response};

#[tokio::main]
async fn main() {
let res: Response = get("https://api.bilibili.com/x/web-interface/nav").await.unwrap();
println!("{:?}", res.text().await);
}
```

```text
C:/Users/Admin/.cargo/bin/cargo.exe run --color=always --profile dev --package test_ --bin test_
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.07s
Running `target\debug\test_.exe`
Ok("{\"code\":-101,\"message\":\"账号未登录\",\"ttl\":1,\"data\":{\"isLogin\":false,\"wbi_img\":{\"img_url\":\"https://i0.hdslb.com/bfs/wbi/7cd084941338484aae1ad9425b84077c.png\",\"sub_url\":\"https://slb.com/bfs/wbi/4932caff0ff746eab6f01bf08b70ac45.png\"}}}")

Process finished with exit code 0
```

but I always got a 403 response when using tauri_plugin_http

```toml
tauri-plugin-http = { version = "2.0.0-rc", features = [
"unsafe-headers",
] }
```

```typescript
const response = await fetch(
'https://api.bilibili.com/x/web-interface/nav',
{
method: 'GET',
}
)
console.log(response.status) // e.g. 200
console.log(response.statusText) // e.g. "O
console.log(response.text()) // e.g. { "key": "value" }
```

I can see the request and response through ipc in the Console, if no features, it works well.

```text
http://ipc.localhost/plugin%3Ahttp%7Cfetch
{"clientConfig":{"method":"GET","url":"https://api.bilibili.com/x/web-interface/nav","headers":[],"data":null}}

http://ipc.localhost/plugin%3Ahttp%7Cfetch_send
{"status":200,"statusText":"OK","headers":[["date","Tue, 22 Oct 2024 19:52:41 GMT"],["content-type","application/json; charset=utf-8"],["content-length","242"],["connection","keep-alive"],["bili-status-code","-101"],["bili-trace-id","348876bfbf671802"],["cpu_usage","50"],["x-bili-trace-id","4869bda0d230ca31348876bfbf671802"],["x-ticket-status","1"],["access-control-expose-headers","X-Cache-Webcdn"],["expires","Tue, 22 Oct 2024 19:52:40 GMT"],["cache-control","no-cache"],["x-cache-webcdn","BYPASS from blzone03"]],"url":"https://api.bilibili.com/x/web-interface/nav","rid":1556556238}
```

If add features = [ "unsafe-headers" ], then get a wrong response.

```text
http://ipc.localhost/plugin%3Ahttp%7Cfetch
{"clientConfig":{"method":"GET","url":"https://api.bilibili.com/x/web-interface/nav","headers":[],"data":null}}

http://ipc.localhost/plugin%3Ahttp%7Cfetch_send
{"status":403,"statusText":"Forbidden","headers":[["date","Tue, 22 Oct 2024 19:14:57 GMT"],["content-type","text/html"],["transfer-encoding","chunked"],["connection","keep-alive"],["server","openresty"],["etag","W/\"6708c0f4-dca\""]],"url":"https://api.bilibili.com/x/web-interface/nav","rid":2481797906}
```

## Full tauri info output

```shell
[✔] Environment
- OS: Windows 10.0.26100 x86_64 (X64)
✔ WebView2: 130.0.2849.46
✔ MSVC: Visual Studio Community 2022
✔ rustc: 1.82.0 (f6e511eec 2024-10-15)
✔ cargo: 1.82.0 (8f40fc59f 2024-08-21)
✔ rustup: 1.27.1 (54dd3d00f 2024-04-24)
✔ Rust toolchain: stable-x86_64-pc-windows-msvc (default)
- node: 20.18.0
- pnpm: 9.12.1
- npm: 10.8.2
- bun: 1.1.31

[-] Packages
- tauri 🦀: 2.0.2
- tauri-build 🦀: 2.0.1
- wry 🦀: 0.44.1
- tao 🦀: 0.30.2
- @tauri-apps/api : 2.0.0-rc.6 (outdated, latest: 2.0.3)
- @tauri-apps/cli : 2.0.0-rc.17 (outdated, latest: 2.0.4)

[-] Plugins
- tauri-plugin-dialog 🦀: 2.0.1
- @tauri-apps/plugin-dialog : 2.0.0 (outdated, latest: 2.0.1)
- tauri-plugin-store 🦀: 2.0.0-rc.3
- @tauri-apps/plugin-store : 2.0.0-rc.1 (outdated, latest: 2.1.0)
- tauri-plugin-fs 🦀: 2.0.1
- @tauri-apps/plugin-fs : not installed!
- tauri-plugin-http 🦀: 2.0.1
- @tauri-apps/plugin-http : 2.0.1
- tauri-plugin-log 🦀: 2.0.1
- @tauri-apps/plugin-log : 2.0.0
- tauri-plugin-shell 🦀: 2.0.0-rc.3
- @tauri-apps/plugin-shell : 2.0.0-rc.1 (outdated, latest: 2.0.1)

[-] App
- build-type: bundle
- CSP: unset
- frontendDist: ../dist
- devUrl: http://localhost:1420/
- framework: Vue.js
- bundler: Rollup
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.