dpicx06888 2013-07-26 19:02
浏览 62
已采纳

通过外部URL允许图像配置文件的最佳安全方式[关闭]

Suppose that I have a web application where users can attach a link in their profile to show the avatar (displayed after with img tag).

This is safe? I think it is not.

What is the best way to check if the url provided is a real image? There are other solutions? (for now I don't want any upload).

  • 写回答

3条回答 默认 最新

  • duanliaoyin3171 2013-07-26 19:50
    关注

    No, it is not safe. This approach is vulnerable to XSS (Cross-Site Scripting) and CSRF (Cross-Site Request Forgery) attacks. Number of ways to construct possible XSS attacks with img tags is huge. Have a look at OWASP list Malformed img tags just to give you an idea.

    You may say that escaping can help you. Yes it is, but escaping alone does not guarantee XSS prevention. Here is what OWASP (The Open Web Application Security Project) recommends in general:

    You MUST use the escape syntax for the part of the HTML document you're putting untrusted data into

    Plus check what data is allowed (white list) instead of checking endless list of not allowed data. Here is OWASP's XSS prevention cheat sheet. OWASP also released a library ESAPI to be used for escaping in the right context.

    But let's say you are done with XSS. With CSRF malicious user can construct legitimate request inside your img tag for updating some user's profile. And let's say such user comes to see this profile and all of the sudden his/her profile is updated! In this case you will probably need to generate "challenge" / validation token associated with user's session which will be attached to each user's request.

    My recommendation is either use external trusted service like gravatar or implement upload. If you do want to use user's input then I strongly encourage you to familiarize yourself with ESAPI library for this case and OWASP XSS and CSRF prevention cheat sheets.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算