- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 NodeJS/Formidable 并尝试保护我的后端以上传图像。
检查文件是否为图像的经典方法是使用如下正则表达式:
if(!file.name || file.name.match(/\.(jpg|jpeg|png)$/i)) {
console.log("the file is an image");
}else{
console.log("the file is not an image");
}
还有这个:
var fileType = file.type.split('/').pop();
if(fileType == 'jpg' || fileType == 'png' || fileType == 'jpeg' ){
console.log("the file is an image");
} else {
console.log( 'incorrect file type: ' + fileType );
}
这是一个很好的检查,但这还不够安全;事实上,如果我将 PDF 重命名为 JPG,浏览器会根据文件的扩展名提供 MIME/type : image/jpg 。这是一个安全问题,因为您可以将 JS 文件或其他任何内容重命名为 JPG 并将其上传到您的后端文件系统。
我发现这篇文章非常有趣:How to check file MIME type with javascript before upload?
它非常适合客户端检查,但我无法在后端重现它。
我想理想的方法是在上传前 4 个字节后即时分析流并检查真实的 MIME 类型。
有什么想法吗?
谢谢!
最佳答案
有效!您可以安装类似 readChunk 的模块将文件转换为缓冲区并编写类似的内容:
form.on('file', function(field, file) {
buffer = readChunk.sync(file.path, 0, 4100);
filetype = fileType(buffer);
if(filetype.ext.match(/(jpg|jpeg|png)$/i)) {
fs.rename(file.path, path.join(form.uploadDir, file.name));
}else {
fs.unlink(file.path);
}
});
关于javascript - NodeJS/强大的 : secure the backend for images upload,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41626124/
https://learn.microsoft.com/en-us/azure/api-management/api-management-howto-policies
https://learn.microsoft.com/en-us/azure/api-management/api-management-howto-policies
首先,我是 django/python 的新手。 我正在尝试创建一个登录网站,允许用户注册一个帐户并通过电子邮件或直接登录验证 通过 fb 或 google(Oauth) 当我点击发送到 email
我正在ubuntu服务器上部署Django Web应用程序。配置已设置。但是,以某种方式,它显示了500 Internal Server Error。我检查了apache错误日志,发现以下错误条目:
我是深度学习的初学者,在执行实际作业时,在 keras.backend 上看到了 Keras 文档. 我解释了很多遍。但是,我不能完全理解 max 和 argmax 函数之间的区别。 最佳答案 我将使
我的后端作业基于 cron 作业(每 4 小时一次)运行。但它在没有处理数据的情况下终止。服务器日志显示如下: 500 15377121ms 0kb instance=0 AppEngine-Goog
django.db.backends.dummy 和 django.db.backends.mysql 有什么区别?我发现有人使用dummy和mysql,但我搜索后不知道它们的区别。 最佳答案 dja
我注意到 tf.keras.backend.get_session() 和 keras.backend.get_session() 返回不同的 session 对象。 无论如何要确保它们返回相同的对象
如果一个 gis 应用程序有一些没有 gis 相关列的表,使用多个数据库(django.db.backends.postgresql_psycopg2 和 django.contrib.gis.db.
I have a question about Ariflow oracle connection. I tried a lot but couldn't find a solution, I
django_redis导入失败 six来自 django.utils 追溯: Internal Server Error: /harem/reponse/ Traceback (most recen
我想运行 python 文件。但我在运行时可以检查到这个错误。 导入错误:pycurl:libcurl 链接时 ssl 后端 (openssl) 与编译时 ssl 后端(无/其他)不同 我的系统是Ma
我正在尝试实现自定义损失函数 def lossFunction(self,y_true,y_pred): maxi=K.argmax(y_true) return K.mean((
当我的神经网络的 forward 函数(训练阶段完成后)正在执行时,我遇到了 RuntimeError: Expected object of backend CUDA but got backend
我从头开始创建了一个Kotlin原生应用程序,我得到了上面的错误。我怎么才能解决这个问题呢?这是一款简单的“Hello World”应用程序。我有其他计算机,但该错误没有出现在那里。我认为这与科南没有
我目前正在学习 Redux,我发现拥有一个状态真实来源有很大好处。我想知道后端是否也可以从状态管理中受益,但我在网上看不到任何相关信息,所以我认为不会。虽然我找到了this这似乎实际上是为了将 red
我有一个 TYPO3 网站,其中默认网站语言是德语。不久前,我添加了另一种网站语言,即英语。 我现在需要做的是在后端交换这两种语言: 从: 默认:德语 替代:英语 到: 默认:英语 替代:德语 所以在
客户端渲染和服务器端渲染有什么区别。他们听起来有点相似。我参加了一个网络研讨会,那里的开发人员说为客户端做这个,为服务器端做这个。两者有什么区别吗? 最佳答案 嗯,客户端意味着服务器向客户端发送一些结
目前我正在使用 Dialogflow 处理 LINE 应用程序中来自用户的消息。 我计划将 Dialogflow 与自定义后端实现集成。 fulfillment 应该有一些特定于用户的消息(例如每个用
我已经在本地环境中为这个问题苦苦挣扎了一段时间了。我一直到处寻找许多不同的解决方案。 设置:我在配置文件中创建一个后端,该后端指向没有缓存(通过)的外部网站。 行为:随机地,后端连接关闭,并显示消息“
我是一名优秀的程序员,十分优秀!