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 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示