Cordova won't fetch anything base64, no matter what's in the CSP
I'm using Cordova's camera plugin to allow my app to take photos, and so far just cannot get it to do anything with the returned data.
I've given up on trying to load it from the filesystem URL, and am now trying with base64.
My CSP is:
<meta http-equiv="Content-Security-Policy" content="
default-src *;
style-src * 'unsafe-inline';
script-src * 'unsafe-inline' 'unsafe-eval';
media-src *;
img-src * data:;
connect-src * ws: wss:;
" />
...however I've tried every other CSP I can find on the internet. No matter what's in there, my console always logs:
"Refused to connect to 'data:image/jpg;base64... because it violates
the document's Content Security Policy.", source:
http://localhost:8080/js/main.js"
Does anyone know why this is the case, or how I'd go about fixing it?
The code to turn the image data into a file is:
navigator.camera.getPicture(response => {
this.urltoFile('data:image/jpg;base64,'+response, 'test.jpg')
.then(file => {
console.log(file);
})
}, () => {}, {
destinationType : navigator.camera.DestinationType.DATA_URL,
})
...and the urlToFile
function is:
urltoFile(url, filename){
let mimeType = (url.match(/^data:([^;]+);/)||'')[1];
return (fetch(url)
.then(res => {
return res.arrayBuffer();
})
.then(buf => {
return new File([buf], filename, {
type: mimeType
});
})
);
}
javascript cordova base64 cordova-plugins content-security-policy
add a comment |
I'm using Cordova's camera plugin to allow my app to take photos, and so far just cannot get it to do anything with the returned data.
I've given up on trying to load it from the filesystem URL, and am now trying with base64.
My CSP is:
<meta http-equiv="Content-Security-Policy" content="
default-src *;
style-src * 'unsafe-inline';
script-src * 'unsafe-inline' 'unsafe-eval';
media-src *;
img-src * data:;
connect-src * ws: wss:;
" />
...however I've tried every other CSP I can find on the internet. No matter what's in there, my console always logs:
"Refused to connect to 'data:image/jpg;base64... because it violates
the document's Content Security Policy.", source:
http://localhost:8080/js/main.js"
Does anyone know why this is the case, or how I'd go about fixing it?
The code to turn the image data into a file is:
navigator.camera.getPicture(response => {
this.urltoFile('data:image/jpg;base64,'+response, 'test.jpg')
.then(file => {
console.log(file);
})
}, () => {}, {
destinationType : navigator.camera.DestinationType.DATA_URL,
})
...and the urlToFile
function is:
urltoFile(url, filename){
let mimeType = (url.match(/^data:([^;]+);/)||'')[1];
return (fetch(url)
.then(res => {
return res.arrayBuffer();
})
.then(buf => {
return new File([buf], filename, {
type: mimeType
});
})
);
}
javascript cordova base64 cordova-plugins content-security-policy
Could you show us the code where you're trying to load the photo?
– Phonolog
Nov 23 '18 at 8:49
Good point! Updated.
– MitchEff
Nov 25 '18 at 8:55
Could you try to adddata:
to your connect-src? Likeconnect-src * data: ws: wss:;
– Phonolog
Nov 25 '18 at 17:27
Oh my god @Phonolog thank you so much. Perhaps set this as an answer and I'll give it a tick?
– MitchEff
Nov 26 '18 at 1:58
add a comment |
I'm using Cordova's camera plugin to allow my app to take photos, and so far just cannot get it to do anything with the returned data.
I've given up on trying to load it from the filesystem URL, and am now trying with base64.
My CSP is:
<meta http-equiv="Content-Security-Policy" content="
default-src *;
style-src * 'unsafe-inline';
script-src * 'unsafe-inline' 'unsafe-eval';
media-src *;
img-src * data:;
connect-src * ws: wss:;
" />
...however I've tried every other CSP I can find on the internet. No matter what's in there, my console always logs:
"Refused to connect to 'data:image/jpg;base64... because it violates
the document's Content Security Policy.", source:
http://localhost:8080/js/main.js"
Does anyone know why this is the case, or how I'd go about fixing it?
The code to turn the image data into a file is:
navigator.camera.getPicture(response => {
this.urltoFile('data:image/jpg;base64,'+response, 'test.jpg')
.then(file => {
console.log(file);
})
}, () => {}, {
destinationType : navigator.camera.DestinationType.DATA_URL,
})
...and the urlToFile
function is:
urltoFile(url, filename){
let mimeType = (url.match(/^data:([^;]+);/)||'')[1];
return (fetch(url)
.then(res => {
return res.arrayBuffer();
})
.then(buf => {
return new File([buf], filename, {
type: mimeType
});
})
);
}
javascript cordova base64 cordova-plugins content-security-policy
I'm using Cordova's camera plugin to allow my app to take photos, and so far just cannot get it to do anything with the returned data.
I've given up on trying to load it from the filesystem URL, and am now trying with base64.
My CSP is:
<meta http-equiv="Content-Security-Policy" content="
default-src *;
style-src * 'unsafe-inline';
script-src * 'unsafe-inline' 'unsafe-eval';
media-src *;
img-src * data:;
connect-src * ws: wss:;
" />
...however I've tried every other CSP I can find on the internet. No matter what's in there, my console always logs:
"Refused to connect to 'data:image/jpg;base64... because it violates
the document's Content Security Policy.", source:
http://localhost:8080/js/main.js"
Does anyone know why this is the case, or how I'd go about fixing it?
The code to turn the image data into a file is:
navigator.camera.getPicture(response => {
this.urltoFile('data:image/jpg;base64,'+response, 'test.jpg')
.then(file => {
console.log(file);
})
}, () => {}, {
destinationType : navigator.camera.DestinationType.DATA_URL,
})
...and the urlToFile
function is:
urltoFile(url, filename){
let mimeType = (url.match(/^data:([^;]+);/)||'')[1];
return (fetch(url)
.then(res => {
return res.arrayBuffer();
})
.then(buf => {
return new File([buf], filename, {
type: mimeType
});
})
);
}
javascript cordova base64 cordova-plugins content-security-policy
javascript cordova base64 cordova-plugins content-security-policy
edited Nov 25 '18 at 8:55
MitchEff
asked Nov 23 '18 at 5:02
MitchEffMitchEff
1539
1539
Could you show us the code where you're trying to load the photo?
– Phonolog
Nov 23 '18 at 8:49
Good point! Updated.
– MitchEff
Nov 25 '18 at 8:55
Could you try to adddata:
to your connect-src? Likeconnect-src * data: ws: wss:;
– Phonolog
Nov 25 '18 at 17:27
Oh my god @Phonolog thank you so much. Perhaps set this as an answer and I'll give it a tick?
– MitchEff
Nov 26 '18 at 1:58
add a comment |
Could you show us the code where you're trying to load the photo?
– Phonolog
Nov 23 '18 at 8:49
Good point! Updated.
– MitchEff
Nov 25 '18 at 8:55
Could you try to adddata:
to your connect-src? Likeconnect-src * data: ws: wss:;
– Phonolog
Nov 25 '18 at 17:27
Oh my god @Phonolog thank you so much. Perhaps set this as an answer and I'll give it a tick?
– MitchEff
Nov 26 '18 at 1:58
Could you show us the code where you're trying to load the photo?
– Phonolog
Nov 23 '18 at 8:49
Could you show us the code where you're trying to load the photo?
– Phonolog
Nov 23 '18 at 8:49
Good point! Updated.
– MitchEff
Nov 25 '18 at 8:55
Good point! Updated.
– MitchEff
Nov 25 '18 at 8:55
Could you try to add
data:
to your connect-src? Like connect-src * data: ws: wss:;
– Phonolog
Nov 25 '18 at 17:27
Could you try to add
data:
to your connect-src? Like connect-src * data: ws: wss:;
– Phonolog
Nov 25 '18 at 17:27
Oh my god @Phonolog thank you so much. Perhaps set this as an answer and I'll give it a tick?
– MitchEff
Nov 26 '18 at 1:58
Oh my god @Phonolog thank you so much. Perhaps set this as an answer and I'll give it a tick?
– MitchEff
Nov 26 '18 at 1:58
add a comment |
1 Answer
1
active
oldest
votes
As per your code you are using the Fetch API to download pictures as base64.
Looking at the docs, the Fetch API is restricted by the connect-src
directive (emphasis is mine):
The HTTP Content-Security-Policy (CSP)
connect-src
directive restricts
the URLs which can be loaded using script interfaces. The APIs that
are restricted are:
<a> ping
,
Fetch
,
XMLHttpRequest
,
WebSocket
, and
EventSource
.
So we'll have to alter your connect-src
to accept data Urls by adding data:
to the directive like so:
connect-src * data: ws: wss:;
One quick last one, just cause I'm useless at CSP stuff - if I change it back to fetch from the device's URL (instead of loading base64), would adding this to the CSP enable it to fetch correctly there also?
– MitchEff
Nov 26 '18 at 8:26
Yeah I would assume that should work aswell...
– Phonolog
Nov 26 '18 at 12:54
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440884%2fcordova-wont-fetch-anything-base64-no-matter-whats-in-the-csp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
As per your code you are using the Fetch API to download pictures as base64.
Looking at the docs, the Fetch API is restricted by the connect-src
directive (emphasis is mine):
The HTTP Content-Security-Policy (CSP)
connect-src
directive restricts
the URLs which can be loaded using script interfaces. The APIs that
are restricted are:
<a> ping
,
Fetch
,
XMLHttpRequest
,
WebSocket
, and
EventSource
.
So we'll have to alter your connect-src
to accept data Urls by adding data:
to the directive like so:
connect-src * data: ws: wss:;
One quick last one, just cause I'm useless at CSP stuff - if I change it back to fetch from the device's URL (instead of loading base64), would adding this to the CSP enable it to fetch correctly there also?
– MitchEff
Nov 26 '18 at 8:26
Yeah I would assume that should work aswell...
– Phonolog
Nov 26 '18 at 12:54
add a comment |
As per your code you are using the Fetch API to download pictures as base64.
Looking at the docs, the Fetch API is restricted by the connect-src
directive (emphasis is mine):
The HTTP Content-Security-Policy (CSP)
connect-src
directive restricts
the URLs which can be loaded using script interfaces. The APIs that
are restricted are:
<a> ping
,
Fetch
,
XMLHttpRequest
,
WebSocket
, and
EventSource
.
So we'll have to alter your connect-src
to accept data Urls by adding data:
to the directive like so:
connect-src * data: ws: wss:;
One quick last one, just cause I'm useless at CSP stuff - if I change it back to fetch from the device's URL (instead of loading base64), would adding this to the CSP enable it to fetch correctly there also?
– MitchEff
Nov 26 '18 at 8:26
Yeah I would assume that should work aswell...
– Phonolog
Nov 26 '18 at 12:54
add a comment |
As per your code you are using the Fetch API to download pictures as base64.
Looking at the docs, the Fetch API is restricted by the connect-src
directive (emphasis is mine):
The HTTP Content-Security-Policy (CSP)
connect-src
directive restricts
the URLs which can be loaded using script interfaces. The APIs that
are restricted are:
<a> ping
,
Fetch
,
XMLHttpRequest
,
WebSocket
, and
EventSource
.
So we'll have to alter your connect-src
to accept data Urls by adding data:
to the directive like so:
connect-src * data: ws: wss:;
As per your code you are using the Fetch API to download pictures as base64.
Looking at the docs, the Fetch API is restricted by the connect-src
directive (emphasis is mine):
The HTTP Content-Security-Policy (CSP)
connect-src
directive restricts
the URLs which can be loaded using script interfaces. The APIs that
are restricted are:
<a> ping
,
Fetch
,
XMLHttpRequest
,
WebSocket
, and
EventSource
.
So we'll have to alter your connect-src
to accept data Urls by adding data:
to the directive like so:
connect-src * data: ws: wss:;
answered Nov 26 '18 at 7:08
PhonologPhonolog
3,53731641
3,53731641
One quick last one, just cause I'm useless at CSP stuff - if I change it back to fetch from the device's URL (instead of loading base64), would adding this to the CSP enable it to fetch correctly there also?
– MitchEff
Nov 26 '18 at 8:26
Yeah I would assume that should work aswell...
– Phonolog
Nov 26 '18 at 12:54
add a comment |
One quick last one, just cause I'm useless at CSP stuff - if I change it back to fetch from the device's URL (instead of loading base64), would adding this to the CSP enable it to fetch correctly there also?
– MitchEff
Nov 26 '18 at 8:26
Yeah I would assume that should work aswell...
– Phonolog
Nov 26 '18 at 12:54
One quick last one, just cause I'm useless at CSP stuff - if I change it back to fetch from the device's URL (instead of loading base64), would adding this to the CSP enable it to fetch correctly there also?
– MitchEff
Nov 26 '18 at 8:26
One quick last one, just cause I'm useless at CSP stuff - if I change it back to fetch from the device's URL (instead of loading base64), would adding this to the CSP enable it to fetch correctly there also?
– MitchEff
Nov 26 '18 at 8:26
Yeah I would assume that should work aswell...
– Phonolog
Nov 26 '18 at 12:54
Yeah I would assume that should work aswell...
– Phonolog
Nov 26 '18 at 12:54
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440884%2fcordova-wont-fetch-anything-base64-no-matter-whats-in-the-csp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Could you show us the code where you're trying to load the photo?
– Phonolog
Nov 23 '18 at 8:49
Good point! Updated.
– MitchEff
Nov 25 '18 at 8:55
Could you try to add
data:
to your connect-src? Likeconnect-src * data: ws: wss:;
– Phonolog
Nov 25 '18 at 17:27
Oh my god @Phonolog thank you so much. Perhaps set this as an answer and I'll give it a tick?
– MitchEff
Nov 26 '18 at 1:58