dtyw10299 2016-04-13 20:24
浏览 164
已采纳

在img src中授予用户输出访问权限是否危险?

here is the situation. I'm building a small site and no file is confidential in it. In many pages, I needed to isolate and print an image for the user. So, I made a small script this way:

<?php
    echo '<img src="'. $_GET['src'] .'" />';
?>

And I like the image source in the URL so it's easy for me to reuse. I am wondering if somebody could try to access other file in the server (for example mysite.com?src=../../SECRET_FILE.txt). I tried to break it myself and couldn't do anything dangerous with this but I'm wondering if it has any flaw? (In the perspective that no image is secret in this website, they are all public and it wouldn't bother me at all if they would find any of these)

Note that the code is not saved in anyway, here is the full script of this page (really just used to save time for user that wants to print an image on the website)

<?php
    echo '<img src="'. $_GET['src'] .'" />';
?>
<script>
    window.print();
</script>
  • 写回答

3条回答 默认 最新

  • douzhantanju1849 2016-04-13 20:30
    关注

    Never trust user input. If every file or script you expose is safe to run, there is no danger of unsafe server-side code running. But you're allowing users to alter your HTML in any way they desire, which is unsafe.

    I would check they enter something valid, such as:

    echo '<img src="/path/to/images/'. basename($_GET['src']) .'" />';
    

    This will confirm they are only requesting a file from your images directory. Even better would be to confirm the file exists:

    $image = '/path/to/images/' . basename($_GET['src']);
    if (file_exists($image)) {
         echo '<img src="'. $image .'" />';
    } else {
         header("HTTP/1.0 404 Not Found");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化