how to run python script from javascript file? - javascript

I'm trying to run a python script in a chrome extension with brython and I'm stuck because of the content security policy. The only tutorial I could find recommended I set up an html file like this:
<body onLoad="">
<iframe src="C:\\hello.py" id="frame" seamless="seamless" scrolling="no"></iframe>
</body>
but an error always pops up in the console saying:
"Refused to execute inline event handler because it violates the following
Content Security Policy directive: "script-src 'self'". Either the 'unsafe-
inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required
to enable inline execution."
I have this line in my manifest file:
"content_security_policy": "script-src 'self' 'unsafe-inline'; object-src 'self'",
but I assume it's not doing anything since people have said that the 'unsafe-inline" keyword is deprecated.
Is there any way to do this in a javascript file and not in the html, and is that a way to get around this problem? I'm really not sure what I'm doing here, so can someone please point me in the right direction?

Related

Browser refuses to execute inline script

I'm running vue3 app using vite.
I want to add this script <script src="https://js.stripe.com/v3"></script> to my index.html; in order to handle payments with Stripe.
But I face these error in console:
VM262:5 Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-GEy81O1cBXMUtzNmiNgydJFrTMOlLkoqKvaHpNDLcrA='), or a nonce ('nonce-...') is required to enable inline execution.
I did some couple of researches and with the help of official documentation; find out that we need to add meta tag that allows this action:
<meta
http-equiv="Content-Security-Policy"
content="connect-src 'self' https://api.stripe.com ws://127.0.0.1:3000; frame-src 'self' https://js.stripe.com https://hooks.stripe.com; script-src 'self' https://js.stripe.com 'unsafe-inline'"
/>
But nothing changes...
First of all, why CSP is enabled in my project (Because I didn't see same problem in Stripe videos in Youtube) and then how can I fix that?
Thanks

Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self'" when page refreshed

I have a Node/React app and get this error when I refresh the page in my production build, for all routes apart from "/", and a blank page is shown.
Refused to execute inline script because it violates the following
Content Security Policy directive: "script-src 'self'". Either the
'unsafe-inline' keyword, a hash
('sha256-eE1k/Cs1U0Li9/ihPPQ7jKIGDvR8fYw65VJw+txfifw='), or a nonce
('nonce-...') is required to enable inline execution.
Page refreshing functions normally in my local build.
I have seen a similar issue here:
Inline script because it violates the following Content Security Policy directive: "script-src 'self'"
and so have tried the INLINE_RUNTIME_CHUNK=false, which made no difference, and when I tried to add the cross-env, I got a CORS error. I don't really understand why cross-env helps or why it could be causing my requests to be blocked by CORS.
Does anyone have any advice? Or let me know if you need more information about my setup.
Thanks!

Custom script injection

Can I inject custom JS in Microsoft Teams? When I tried injecting it says
Refused to load the script 'xxxxxxxxxx.js' because it violates the following Content Security Policy directive: "script-src *.protection.outlook.com 'nonce-yaXPKdhE1aa/JhA/PFsoyw==' 'report-sample' 'self' 'unsafe-eval' 'unsafe-inline' blob: *.office.net *.office365.us *.cms.rt.microsoft.com *.delve.office.com *.teams.microsoft.com *.onenote.com *.presence.skype.com *.streaming.mediaservices.windows.net *.trouter.io ajax.aspnetcdn.com amp.azure.net.
Of course, It's a genuine error. My question is that is there any ethical, legal way to inject even if it requires permission from the admin. In short, Is there any right way to do it?

Is it possible to integrate Stripe into a chrome extension

I am trying to integrate Stripe but facing the following issues.
When I am loading normally via ngx-stripe, it's giving me this error in the console.
Refused to load the script 'https://js.stripe.com/v3/' because it
violates the following Content Security Policy directive: "script-src
'self' 'unsafe-eval'". Note that 'script-src-elem' was not explicitly
set, so 'script-src' is used as a fallback.
I change content_security_policy in manifest to "script-src 'self' https://js.stripe.com/v3/; object-src 'self' " but it's giving me this error in the console.
Uncaught EvalError: Refused to evaluate a string as JavaScript because
'unsafe-eval' is not an allowed source of script in the following
Content Security Policy directive: "script-src 'self'
https://js.stripe.com/v3/".
After that, I tried adding content_scripts but it's giving me this error and don't let me to import the zip file as well while saying
Could not load javascript '' for content script.
How can I overcome this issue? and is it possible to integrate Stripe into a chrome extension because Stripe only works via https but extension working with chrome://
The answer provided by #EndersJeesh works for me with Chrome extensions with manifest version 2.
I was wondering whether it was going to stop working in manifest version 3:
https://developer.chrome.com/docs/extensions/mv3/intro/mv3-migration/#remotely-hosted-code
The Remotely Hosted Code says that you will not be able to load remote hosted code so I imagine that loading https://js.stripe.com/v3 will not be possible.
The best solution would be for all the Stripe code to be embedded in the extension but there's no npm package for this Stripe code.
Any comments from #EndersJeesh or others would be appreciated.
I ran into these and several subsequent issues integrating stripe into a chrome extension.
Explanation:
I'll first state what I believe was happening. Using the stripe react libraries, I believe they have an inline js call somewhere, causing the error you're seeing about js.stripe.com. I think this would be fixed by adding unsafe-inline into your content_security_policy, but that will not be executed by chrome extensions per the extension CSP.
Solutions:
So here are the various things I did (solving one typically led to having to solve the next set of errors).
I initially had my constent_security_policy set to
"content_security_policy": "script-src 'self' https://js.stripe.com/v3; object-src 'self';"
I was running into your issue above, and so I added the script into my header call, leading my index.html file to be the following:
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://js.stripe.com/v3" async></script>
</head>
<body>
<div id="modal-window"></div>
</body>
</html>
That resolved the first set of errors which matched yours above, but it led to the next set of issues:
I then hit a series of errors around Refused to frame ... because it violates the following Content Security Policy directive: "frame-src"..., so I fixed these by adding to my content_security_policy the following:
frame-src https://js.stripe.com/v3
So my final version of my content_security_policy is as follows:
"content_security_policy": "script-src 'self' https://js.stripe.com/v3; object-src 'self'; frame-src https://js.stripe.com/v3"
I hope that does it for you. It took several hours to work through all of that for me.

Chrome extension error when adding script tags to html

My problem is, when developing a chrome extension, I am getting error;
Refused to execute inline script because it violates the following
Content Security Policy directive: "script-src 'self' blob:
filesystem: chrome-extension-resource:". Either the 'unsafe-inline'
keyword, a hash
('sha256-+BWoieEB23JsqONQi994gklHUNPq5RCtit+I45ejZPU='), or a nonce
('nonce-...') is required to enable inline execution.
When I try to add to the html.
What can I do?

Categories