Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CORS policy Access-Control-Allow-Private-Network fails #146

Closed
mkpoli opened this issue Apr 4, 2024 · 5 comments
Closed

CORS policy Access-Control-Allow-Private-Network fails #146

mkpoli opened this issue Apr 4, 2024 · 5 comments

Comments

@mkpoli
Copy link

mkpoli commented Apr 4, 2024

This does not seems to be the same as CSP policy #1 . Can you support file @require?

好像现在 Chrome 更严了?

Chrome version: 123.0.6312.88

example.com/:1 Access to script at 'http://127.0.0.1:5134/__vite-plugin-monkey.entry.js' from origin 'https://example.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Private-Network' header was present in the preflight response for this private network request targeting the `local` address space.

userscript.html?name=server%253Amonkey-project.user.js&id=2b7fb4eb-e66d-4391-9d9c-5b6de9d2af2a:60 
GET http://127.0.0.1:5134/__vite-plugin-monkey.entry.js net::ERR_FAILED
@mkpoli mkpoli changed the title CORS policy fails CORS policy Access-Control-Allow-Private-Network fails Apr 4, 2024
@mkpoli
Copy link
Author

mkpoli commented Apr 4, 2024

Current temporary solution 1:

Disable CORS in Chrome

Current temporary solution 2:

Use file-based approach (Need manual refresh)

import { defineConfig } from 'vite';
import monkey from 'vite-plugin-monkey';
export default defineConfig({
  plugins: [
    monkey({
      entry: 'src/main.ts',
      server: {
        open: false,
      },
      userscript: {
        icon: 'https://vitejs.dev/logo.svg',
        namespace: 'npm/vite-plugin-monkey',
        match: ['https://example.com/'],
      },
    }),
  ],
});
// ==UserScript==
// @name         Monkey
// @namespace    http://tampermonkey.net/
// @version      2024-04-04
// @match        https://example.com/
// @grant        none
// @require      file://C:/Users/User/Desktop/monkey-project/dist/monkey-project.user.js
// ==/UserScript==
vite build --watch

@lisonge
Copy link
Owner

lisonge commented Apr 4, 2024

image
image

@andywang425
Copy link
Contributor

I have the same version of chrome as you do, but everything works fine in dev mode. Testing under Tampermonkey 5.0.1 with default settings.

@lisonge
Copy link
Owner

lisonge commented Apr 4, 2024

没看懂你想表达什么,如何复现你说的这个错误?


Access-Control-Allow-Private-Network 相关的错误只需要设置 server 的 Access-Control-Allow-Private-Network 响应头的值为 true 即可让浏览器继续请求

import { defineConfig } from 'vite';
import monkey from 'vite-plugin-monkey';

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [
        {
            name: 'set-headers',
            apply: 'serve',
            configureServer(server) {
                server.middlewares.use((req, res, next) => {
                    res.setHeader(
                        'Access-Control-Allow-Private-Network',
                        'true'
                    );
                    next();
                });
            },
        },
        monkey({
            entry: 'src/main.ts',
            userscript: {
                icon: 'https://vitejs.dev/logo.svg',
                namespace: 'npm/vite-plugin-monkey',
                match: ['https://example.com/'],
            },
        }),
    ],
});

@mkpoli
Copy link
Author

mkpoli commented Apr 5, 2024

Access-Control-Allow-Private-Network 相关的错误只需要设置 server 的 Access-Control-Allow-Private-Network 响应头的值为 true 即可让浏览器继续请求

import { defineConfig } from 'vite';
import monkey from 'vite-plugin-monkey';

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [
        {
            name: 'set-headers',
            apply: 'serve',
            configureServer(server) {
                server.middlewares.use((req, res, next) => {
                    res.setHeader(
                        'Access-Control-Allow-Private-Network',
                        'true'
                    );
                    next();
                });
            },
        },
        monkey({
            entry: 'src/main.ts',
            userscript: {
                icon: 'https://vitejs.dev/logo.svg',
                namespace: 'npm/vite-plugin-monkey',
                match: ['https://example.com/'],
            },
        }),
    ],
});

谢谢,用这个方法解决了。我以为不是Vite服务器端的错误,而是Chrome就不让在别的地方执行localhost,原来是localhost的Header问题。看来是我孤陋寡闻了。

但是我很好奇为什么我以前用的时候没事,而且其他人也没事……。留这个Issue以供后人研究。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants