来自: https://ourcodeworld.com/articles/read/182/the-canvas-has-been-tainted-by-cross-origin-data-and-tainted-canvases-may-not-be-exported

These errors happens when you try to manipulate an image on a canvas that doesn't seems to have the legitim permission to be handled for your code.

They're related (and caused) by the Access-Control-Allow-Origin header of a request (and allowed by the server). The image is from another domain, therefore this behaviour is disallowed in most browsers as this would be a security breach.

What is a tainted canvas?

The HTML specification introduces a crossorigin attribute for images that, in combination with an appropriate CORS header, allows images defined by the img element loaded from foreign origins to be used in canvas as if they were being loaded from the current origin.

See CORS settings attributes for details on how the crossorigin attribute is used.

Although you can use images without CORS approval in your canvas, doing so taints the canvas. Once a canvas has been tainted, you can no longer pull data back out of the canvas. For example, you can no longer use the canvas toBlob()toDataURL(), or getImageData() methods; doing so will throw a security error.

The canvas has been tainted by cross-origin data

Analyze the following code :

var canvas = document.getElementById("canvas");

function drawImageFromWebUrl(sourceurl){
var img = new Image(); img.addEventListener("load", function () {
// The image can be drawn from any source
canvas.getContext("2d").drawImage(img, 0, 0, img.width, img.height, 0, 0, canvas.width, canvas.height); // However the execution of any of the following methods will make your script fail
// if your image doesn't has the right permissions
canvas.getContext("2d").getImageData();
canvas.toDataURL();
canvas.toBlob();
}); img.setAttribute("src", sourceurl);
}
// But, this code is being executed from ourcodeworld and the image fcomes from google.
drawImageFromWebUrl("https://www.google.de/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png");

If you have this issue, your code may have some of the methods in common and the image doesn't come from your domain.

Tainted canvases may not be exported

Uncaught SecurityError: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.

You'll find this error if you was smart enought to think :

Hey, if i can get the data of a tainted canvas from another domain, let's convert it into a base64 string and then redraw it.

- You, Nobel prize philosophy - 2016

But not smart enough to think that do this action is being blocked too as the image "doesn't belong to you".

Possible solutions

Javascript

You may able to prevent this error by simply setting in your image the crossorigin attribute (with Javascript or HTML) :

<img src="otherdomain.com" crossorigin="Anonymous" />

<!-- Or with Javascript -->
<script>
var image = new Image();
image.crossOrigin = "Anonymous";
...
</script>

However, this will only work if your server response has the following header on it :

Access-Control-Allow-Origin "*"

Otherwise you'll get instead a new error :

Image from origin 'otherdomain.com' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'yourdomain.com' is therefore not allowed access.

Server side

As the problem is that the image doesn't come from your domain, you can create a proxy with your server language (the same technique used for display http (insecure) content in https (secure) content).

The workflow (in your server language, PHP, C# etc) should be :

You can read more about this technique here and may be the force with you.

he canvas has been tainted by cross-origin data and tainted canvases may not be exported的更多相关文章

  1. Ajax本地跨域问题 Cross origin requests are only supported for HTTP

    问题:打开本地html文件时,报错如下 Cross origin requests are only supported for protocol schemes: http, data,chrome ...

  2. Blocking Cross Origin API request for /api/contents Creating Notebook Failed An error occurred while creating a new notebook.

    anacoda安装的jupyter,使用nginx进行了转发,远程访问可以进去,但是创建文件和创建目录都会报错 浏览器页面报错: 第一次使用jupyter创建python时错误:Creating No ...

  3. jquery读取本地文件,Windows上报错。XMLHttpRequest cannot load xxx. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.k.cors.a.c

    问题: 测试报告,使用本地的json.txt文件,结果文件读取失败,报错如下: XMLHttpRequest cannot load xxx. Cross origin requests are on ...

  4. CORS (Cross Origin Resources Share) 跨域

    CORS 跨域 1 什么是跨域问题 基于安全考虑,浏览器会限制使用脚本发起任何跨域请求. 所谓的跨域请求,就是与当前页面的 http/ip/port 不一样的请求. 但在实际运用中,跨域获取数据的需求 ...

  5. nodejs报错 XMLHttpRequest cannot load localhost:3000/test_date/. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

    z在请求本地的时候  如果ajax的URL 前面没有http的话 就会报错 jq.js:2 XMLHttpRequest cannot load localhost:3000/test_date/. ...

  6. Cross origin requests are only supported for protocol schemes: http, data, chrome,chrome-extension的问题

    Cross origin requests are only supported for protocol schemes: http, data, chrome,chrome-extension的问 ...

  7. 用临时用户数据目录启动Chrome,关闭安全检查等(解决Cross origin requests are only supported for HTTP?)

    Cross origin requests are only supported for HTTP? 参考:https://www.zhihu.com/question/20948649 批处理: s ...

  8. 【chrome错误】Cross origin requests are only supported for protocol schemes: http, data,chrome-extension, https, chrome-extension-reso

    使用ajax请求本地文件,chrome会报跨域错误. XMLHttpRequest cannot loadfile:///C:/Users/Li/Desktop/images/alist.json.C ...

  9. 利用 pyhon 解决 Cross Origin Requests

    在学习 ajax 时遇到了一个问题 XMLHttpRequest cannot load file:xxxxxxxx . Cross origin requests are only supporte ...

随机推荐

  1. python3 + selenium 之警告和弹窗

    获取警告框信息 texts = driver.switch_to_alert().text #获取警告文本 print(texts)#得到弹窗的文本消息,比如得到:请输入用户名! 使用switch_t ...

  2. 一个判断男女性别的JS脚本

    var xb= "男" if(xb=="男"){ document.write("你是男生") }else{ document.write( ...

  3. git代码提交步骤,教程

    代码提交 代码提交一般有五个步骤: 1.查看目前代码的修改状态 2.查看代码修改内容 3.暂存需要提交的文件 4.提交已暂存的文件 5.同步到服务器 1.     查看目前代码的修改状态 提交代码之前 ...

  4. python算法双指针问题:两个有序数组的合并

    最近在看<你也能看得懂的python算法书>, 自己来实现一下里面的算法吧. 有书里的有所不同. 比如这个题目,我的实现如下: from django.test import TestCa ...

  5. c++ primer 笔记 (一)

    昨天开始看的<C++ Primer>,确实不错.希望这周抓紧看完,每天做下笔记,以便以后复习. main函数返回一个值给操作系统   操作系统通过main函数返回的值来确定程序是否成功执行 ...

  6. BZOJ1084 [SCOI2005]最大子矩阵 动态规划

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1084 题意概括 这里有一个n*m的矩阵,请你选出其中k个子矩阵,使得这个k个子矩阵分值之和最大.注 ...

  7. HDU3342Legal or Not 拓扑排序

    有向图判断是否成环  如果是环输出NO 只要入度为0的点的个数 等于 总的点的个数则无环 #include<bits/stdc++.h> using namespace std; //in ...

  8. [OpenCV-Python] OpenCV 中的图像处理 部分 IV (二)

    部分 IVOpenCV 中的图像处理 OpenCV-Python 中文教程(搬运)目录 16 图像平滑 目标 • 学习使用不同的低通滤波器对图像进行模糊 • 使用自定义的滤波器对图像进行卷积(2D 卷 ...

  9. Hash值破解工具(findmyhash与hash-identifier破解Hash值)

    Hash值破解工具(findmyhash与hash-identifier破解Hash值) 前言: Kali Linux提供各种哈希密文破解工具,如hashcat.john.rainbows.不论哪一种 ...

  10. 软工实践第五次作业-WordCount进阶需求

    软工实践作业(五) GitHub 作业链接 结对博客 031602240 具体分工 PSP表格 代码规范 解题思路与设计说明 爬虫使用 代码组织与内部实现设计(类图) 算法关键 实现方法 流程图 附加 ...