【发布时间】:2019-03-14 00:10:09
【问题描述】:
所以,在整个社区寻找解决方案后,我的问题如下:
我在 Wordpress 环境中工作,Apache 服务器。我在上传中有一个名为 /restricted/ 的文件夹。此处的所有内容(任何文件扩展名)只有在以下情况下才能访问::
- 设置了名为“custom_cookie”的 cookie
- 并且这个cookie值必须是URL请求的部分匹配
如果这些条件失败,则会提供图像。在这个/restricted/ 文件夹中,我得到了一个.htaccess 文件。一切都必须(首选)在那个 htaccess 文件中完成,而不是在 root htaccess 文件中。
cookie是functions.php设置的,没问题 部分。而关于安全性的 cmets 不是这里的问题
这是一个 url 示例(本地主机):http://localhost/komfortkonsult/wp-content/uploads/restricted/some-file.jpg?r=870603c9d23f2b7ea7882e89923582d7
第一个条件设置了一个名为 custom_cookie 的 cookie,一切都在工作:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /komfortkonsult/
RewriteCond %{REQUEST_URI} ^.*uploads/restricted/.*
RewriteCond %{HTTP_COOKIE} !custom_cookie
RewriteRule . /komfortkonsult/restricted.png [R,L]
</IfModule>
但是,下一部分我完全出局了,但是我尝试失败了以下方法:
RewriteCond %{HTTP_COOKIE} custom_cookie=(.*)$
RewriteCond %1::%{REQUEST_URI} ^(.*?)::/\1/?
RewriteRule . /komfortkonsult/restricted.png [R,L]
同样:
RewriteCond %{QUERY_STRING} ^r=(.*)$
RewriteRule ^/ - [E=COOKIE_MATCH:%1]
RewriteCond %{HTTP_COOKIE} !custom_cookie="%{ENV:COOKIE_MATCH}"
RewriteRule . /komfortkonsult/restricted.png [R,L]
同样:
RewriteCond %{HTTP_COOKIE} custom_cookie=([^;]+) [NC]
RewriteCond %{REQUEST_URI} !%1 [NC]
RewriteRule . /komfortkonsult/restricted.png [R,L]
等等。我真的想把它保存在.htaccess 中,而不是通过.php 文件调用进行验证。但是如果这是我架构的唯一解决方案,请提供一个完整的工作示例(不是 foo=bar,你的重定向在这里...)
我的目标的任何其他方法都是受欢迎的。
非常感谢您帮我解决这个问题。
/Intervik
更新(在接受答案并工作后)使用示例
目标是在 Wordpress 单一安装中一层保护。如果A) ,所有上传并附加到页面的媒体、图像或其他文件都被隐藏用户未登录或B)用户已登录但没有'edit_post'功能。
但限制仅适用于上传到名为/restricted/ 的唯一文件夹的文件。该文件夹位于 Wordpress 原始 /uploads/ 根目录中。此受限材料不允许直接链接或被搜索引擎等访问。不允许浏览器缓存,并且限制必须在注销后立即生效。还有更多……但我想你明白了。
命名空间'custom_cookie' 只是一个示例。显示 Wordpress 安装的示例位于 localhost 的子文件夹中。喜欢h**p://example.com/workspace/。如果在 root 中,则删除 'workspace/'。
cookie 架构,functions.php
function intervik_theme_set_custom_cookie(){
if(is_user_logged_in()){
global $current_user;
if(current_user_can('edit_posts')){
if(!isset($_COOKIE['custom_cookie'])){
$cookie_value = $current_user->ID . '|' . $current_user->user_login . '|' . $current_user->roles;
$salt = wp_salt('auth');
$cookie_hash = hash_hmac('md5', $cookie_value, $salt);
setcookie('custom_cookie', $cookie_hash, time()+36, '/');
$_COOKIE['custom_cookie'] = $cookie_hash;
} else {
$cookie_value = $current_user->ID . '|' . $current_user->user_login . '|' . $current_user->roles;
$salt = wp_salt('auth');
$cookie_hash = hash_hmac('md5', $cookie_value, $salt);
if($cookie_hash != $_COOKIE['custom_cookie']){
setcookie('custom_cookie', '', 1, '/');
unset($_COOKIE['custom_cookie']);
}
}
} else {
if(isset($_COOKIE['custom_cookie'])){
setcookie('custom_cookie', '', 1, '/');
unset($_COOKIE['custom_cookie']);
}
}
} else {
if(isset($_COOKIE['custom_cookie'])){
setcookie('custom_cookie', '', 1, '/');
unset($_COOKIE['custom_cookie']);
}
}
}
add_action('init', 'intervik_theme_set_custom_cookie');
如您所见,对于 每个有效用户,每个 cookie 是唯一的,对于每 +36 秒的时间段(足够加载页面 - 但使用 +120 2分钟)。此“令牌”应用于发送到服务器的每个请求:
附件网址过滤器的链接:
function intervik_restricted_wp_get_attachment_url($url, $post_id){
if(strpos($url, '/restricted/') !== FALSE){
if(isset($_COOKIE['custom_cookie'])){
$url = add_query_arg('r', $_COOKIE['custom_cookie'], $url);
}
}
return $url;
}
add_filter('wp_get_attachment_url', 'intervik_restricted_wp_get_attachment_url', 10, 2);
我们不允许任何其他查询字符串。备注,必须为尺寸添加更多过滤器,例如wp_get_attachment_image_src 等。但是直接链接到媒体,这就足够了。
将
if(current_user_can('edit_posts')替换为另一个if(is_user_logged_in() ...将所有内容更改为仅登录/退出 用户。然后使用 if(!is_admin() && strpos($url, '/restricted/')!== FALSE) ...跳过管理后端中的过滤器
最后是.htaccess 文件,位于uploads/restricted/ 文件夹的根目录中:
# BEGIN Intervik
Options +FollowSymLinks
Options All -Indexes
<IfModule !mod_rewrite.c>
Deny from all
</IfModule>
<IfModule mod_headers.c>
Header set Cache-Control "no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires 0
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_COOKIE}::%{QUERY_STRING} !\bcustom_cookie=([0-9a-f]{32})\b.*::r=\1(&|$)
RewriteRule . /workspace/restricted.png? [R,L]
# END Intervik
我还在 Wordpress 安装根目录中放置了漂亮的 PNG IMAGE“受限访问超时”。这也作为非有效管理员的图书馆管理区域的缩略图。上传过滤器或后端是另一个区域。
我们不是在这里保护英格兰的财务计划,但我们想保留 为一个组织和一些来自 Google 和来自的图片 你的妻子。
请发表评论
它确实有效,欢迎您评论缺陷或安全风险。然而,在我们的安装中,在这一层之上还有另一层使用 PHP 进行验证,但我们需要速度来处理不那么重要的事情。
【问题讨论】:
-
这似乎是一种非常有趣的方法——以这种方式路由值。如果我理解正确,示例是当 Wordpress 安装在子文件夹中时(我想是 localhost 环境)?
-
是的,我在上次编辑时添加了有关示例的信息,位于 Wordpres 子文件夹安装中,谢谢 Anna。
-
除了简单地确保它们相同之外,您如何验证 cookie 和 URL 参数值?
-
add_filter('wp_get_attachment_url'过滤器将用户 cookie 值 填充为r=query string。人们必须明白,此解决方案需要 由 id 加载 的图像(如画廊或菜单),而不是在帖子和页面中硬编码嵌入 src:s。但是可以过滤内容和 wp_get_attachment_imagesrc 以获得更复杂的输出。 -
我确认它有效。但是如果 edit_posts 层被删除,它更容易调整,并且仅用于登录和注销。删除
'|' . time()并将+36 更改为2 分钟后,一切都感觉更稳定了。漂亮的 "restricted file" PNG 已在缓存中,重新登录可能会在此处刷新...
标签: .htaccess url mod-rewrite cookies match