[英]Is decoding base64 strings safe
我正在提交一個用戶提交的文件(它應該是一個圖像),但可以提交一個 python 文件。 它作為base64
編碼的字符串發送到后端 Flask 服務器,並解碼為子目錄中的文件。 我擔心安全性,因為用戶提交的文件將作為img
src="/images/fileThatIDecoded.jpg"
標簽包含在動態生成的 HTML 中。 我自己設置了名稱和文件擴展名。 如何驗證解碼的 base64 字符串是有效圖像? 為了使用 imghdr 模塊,我必須已經將解碼的字符串保存到一個文件中,這可能是不安全的。
我的代碼:
mainPond.onaddfile = (err, item) => {
if (err) {
console.warn(err);
return;
}
const base64String = item.getFileEncodeBase64String();
console.log(base64String)
document.getElementById("hiddenFile").value = base64String
}
document.getElementById("submitbtn").onclick = function() {
if (validateForm()) {
document.getElementById("form").submit()
}
}
@app.route("/create", methods=["GET","POST"])
@login_required
def create():
if request.method == "GET":
return render_template("create.html")
else:
imgdata = request.form.get("mainFile")
if helpers.verifyImage(imgdata[:44]):
imgdata = base64.b64decode(imgdata)
filename = 'some_image.jpg'
filename = os.path.join(os.path.abspath(os.curdir), "images", filename)
with open(filename, 'wb') as f:
f.write(imgdata)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.