tauri-apps / tauri

Build smaller, faster, and more secure desktop and mobile applications with a web frontend.
https://tauri.app
Apache License 2.0
85.48k stars 2.58k forks source link

url not allowed on the configured scope #11735

Closed 151819880412 closed 5 days ago

151819880412 commented 5 days ago

Describe the bug

lib.rs

// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
#[tauri::command]
fn greet(name: &str) -> String {
    format!("Hello, {}! You've been greeted from Rust!", name)
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()
    .plugin(tauri_plugin_http::init())
    .run(tauri::generate_context!())
    .expect("error while running tauri application");
}

default.json

{
  "$schema": "../gen/schemas/desktop-schema.json",
  "identifier": "default",
  "description": "Capability for the main window",
  "windows": ["main"],
  "permissions": [
    {
        "identifier": "http:default",
        "allow": [{
           "url": "http://*:*"
        }],
        "deny": [{
            "url": "http://*:*"
        }]
    }
  ]
}

node test

const http = require('http');

const hostname = 'localhost';
const port = 3000;

const server = http.createServer((req, res) => {
  if (req.method === 'GET' && req.url === '/test') {
    res.statusCode = 200; 
    res.setHeader('Content-Type', 'text/plain');
    res.end('test'); 
  } else {
    res.statusCode = 404;
    res.end('Not Found'); 
  }
});

server.listen(port, hostname, () => {
});

const response = await fetch("http://localhost:3000/aaa", { method: "GET", }); error: "url not allowed on the configured scope: http://localhost:3000/aaa"

Reproduction

No response

Expected behavior

No response

Full tauri info output

"url not allowed on the configured scope: http://localhost:3000/aaa"

Stack trace

No response

Additional context

No response

151819880412 commented 5 days ago

I write it wrongly http://localhost:3000/test

FabianLars commented 5 days ago
  "permissions": [
    {
        "identifier": "http:default",
        "allow": [{
           "url": "http://*:*"
        }],
        "deny": [{
            "url": "http://*:*" <-----
        }]
    }
  ]

The urls in deny will always be denied even if they are also in allow. So in this case if you remove the deny entry it should work.

151819880412 commented 5 days ago
  "permissions": [
    {
        "identifier": "http:default",
        "allow": [{
           "url": "http://*:*"
        }],
        "deny": [{
            "url": "http://*:*" <-----
        }]
    }
  ]

即使中的 URLdeny也位于 中,也始终会被拒绝allow。因此,在这种情况下,如果您删除拒绝条目,它应该会起作用。

Thank you, it has been resolved

151819880412 commented 5 days ago
  "permissions": [
    {
        "identifier": "http:default",
        "allow": [{
           "url": "http://*:*"
        }],
        "deny": [{
            "url": "http://*:*" <-----
        }]
    }
  ]

The urls in deny will always be denied even if they are also in allow. So in this case if you remove the deny entry it should work.

npm run tauri build How to set it up afterwards

1. I have set it up axios-->baseURL import axiosTauriApiAdapter from 'axios-tauri-api-adapter'; axios.create({ ...config, adapter: axiosTauriApiAdapter, })

2. import { fetch } from "@tauri-apps/plugin-http"; await fetch("http://localhost:8087/auth/login", { method: "POST", body: JSON.stringify({ username: "1", password: "1" }) })

neither of these methods works