【问题标题】:Restrict user file access via relative url通过相对 url 限制用户文件访问
【发布时间】:2017-05-03 09:47:24
【问题描述】:

我已经使用 Yii2 php 框架设置了一个 cms 应用程序。但是,我遇到过这样的情况,如果用户可以创建包含图像或文档的帖子,她/他可以在 WYSIWIG 编辑器中查看该帖子的 html 源代码,并将 src 链接更改为其他用户的内容。例如,如果 Fred 有类似 userfiles/fred/myimage.png 的链接,他可以查看 html 源并将其更改为 userfiles/john/anotherimage.png。

我怎样才能让 Fred 只能访问他的 cms 帖子中具有相关 URL 的用户文件/fred?

我曾尝试研究 .htaccess,但不知道如何将其应用于登录到我的应用程序的特定用户。

【问题讨论】:

    标签: php .htaccess yii2 httpd.conf


    【解决方案1】:
    1. 不要将文件存储在@web,而是更高级别
    2. 编写自定义操作,流式传输请求的文件。你可以在这里处理权限(Yii::$app->user->can('doit')),发送可能是:

      if (file_exists($filename)) {
          Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
          Yii::$app->response->headers->add('Content-Type', FileHelper::getMimeTypeByExtension($filename));
          Yii::$app->response->headers->add('Content-Length', filesize($filename));
      
          return file_get_contents($filename);
      } else {
          throw new yii\web\NotFoundHttpException($filename);
      }
      

    getMimeTypeByExtension

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-13
      • 2015-12-15
      • 1970-01-01
      • 1970-01-01
      • 2019-07-11
      • 2020-02-25
      相关资源
      最近更新 更多