douxi3977 2015-06-16 13:48
浏览 56
已采纳

Symfony2防火墙:保护/ web文件夹

I am developping a SF2 web-app which is fully behind a firewall: nobody shouldn't be able to see or modify anything before behing logged (except login form, of course).

So here is the firewall part of my security.yml file:

firewalls:
    dev:
        pattern:  ^/(_(profiler|wdt)|css|images|js)/
        security: false
    main_login:
        pattern:   ^/login$
        anonymous: true
    main:
        pattern:        ^/
        anonymous:      false
        form_login:
            login_path: fos_user_security_login
            check_path: fos_user_security_check
        logout:
            path:       fos_user_security_logout
            target:     /

This works fine: if I type the url http://mywebsite.com/app.php/article/show/1 while unlogged, I am forwarded to the login page.

My problem is that I have some documents and media files located in Symfony's web directory (e.g. myapp/web/document/myTextFile.txt). They are accessible via my app for logged users, but also for non-logged users!

Anybody who types http://mywebsite.com/app.php/document/myTextFile.txt can download the file...

Why doesn't the pattern: ^/ line prevent this? Is the web folder excluded by default because it contains app.php and js/and css/ folder?

How do I protect my documents?


Update: Display protected images

I tried the solution suggested by Gerry, it works fine to protect the download of my documents.

However, I also have pictures in my document folder and I would like to display these pictures, directly included in the relevant pages.

For example, in http://mywebsite.com/app.php/article/show/1 there will be some text and the picture myapp/app/Resources/document/AAA.jpg, and in http://mywebsite.com/app.php/article/show/2 there will be some text and the picture myapp/app/Resources/document/BBB.jpg, etc.

I tried to do it with Assetic but it seems that it is done for "static" images (like top logo, or images which are not object-dependent).

A solution I see is to convert the image in Base64 and include it like this : <img alt="" src="data:image/png;base64(...)" />, but it seems really ugly...

  • 写回答

2条回答 默认 最新

  • doueta6642 2015-06-16 15:43
    关注

    The web directory is your public root directory, being served by the webserver (Apache/Nginx/...).

    By default any request to an existing file does not pass Symfony at all, so no firewall setting is going to prevent access to files residing in the web root.

    The clean solution is to move these files to another directory, outside the webroot, for example app/Resources/uploads. Then you could write a Symfony controller for downloading these files.

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

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵