【问题标题】:Apache PerlAuthzHandler and form POST varsApache PerlAuthzHandler 和表单 POST 变量
【发布时间】:2015-03-14 11:54:48
【问题描述】:

我有一个 mod_perl PerlAuthzHandler 授权访问 Apache2 提供的目录。在控制访问方面,它运行良好。

不过,一个副作用是它似乎阻止了 POST 表单变量对受保护位置的 PHP 应用程序可用。至少当我注释掉 PerlAuthzHandler 并重新加载 Apache PHP 应用程序功能时。我认为 Perl 没有继承完整的环境,或者 Perl 正在发送一个经过清理的环境。

是否可以确保所有POST变量在授权后对PHP应用程序可用?


详情

用户身份验证由 simplesamlphp 在此框(SP)和 ADFS(IdP)上管理。除了 simplesamlphp 安装之外,还有 PerlAuthzHandler 的使用。

Apache 配置

相关位置的 Apache 配置如下所示:

<Location /activity>

   ErrorDocument 401 "/simplesaml/authmemcookie.php"
   AuthName "MCLAnet"
   AuthType Cookie
   Require valid-user

   PerlSetvar Auth_memCookie_SessionTableSize "40"
   PerlAuthzHandler My::simple
   PerlSetVar VALID_GROUP_EXPR "status-acad or staff-g"

</Location>

授权处理程序

授权处理程序检索设置会话时记录的组成员资格,并与 PerlSetVar 中的VALID_GROUP_EXPRESSION 进行比较:

#! /usr/bin/perl

package My::simple;

# Version 1.0  MCLA CSS

use Apache2::Access ();
use Apache2::RequestUtil ();

# load modules that are going to be used
use Data::Dumper;
use CGI qw(:standard);
use CGI::Cookie;
use Cache::Memcached;
use PHP::Serialization qw(serialize unserialize);

# compile (or import) constants
use Apache2::Const -compile => qw(OK FORBIDDEN);

$debug=0;
$debug_file="/tmp/xxx";

dmp('prerun',('test'));

sub handler {

      my $r = shift;

      my $user = $r->user;
      dmp('0 user',$user);

      # ------------------------ get valid group(s) for this session

      my $valid_group_expr=$r->dir_config("VALID_GROUP_EXPR");
      dmp('1 valid group list',$valid_group_expr);

      # -- get the session cooke to retrieve the session <ID>

      $query = new CGI;
      # fetch existing cookies
      my %cookies = CGI::Cookie->fetch;
      # dmp('Cookies',%cookies);

      my $ID = $cookies{'SimpleSAMLSessionID'}->value;
      dmp('2 SimpleSAMLSessionID value',$ID);

      my $SessionID='simpleSAMLphp.session.' . $ID;

      # -- use the session ID to look up the value of memcached key simpleSAMLphp.session.<ID>

      my $cache =  new Cache::Memcached {
         'servers' => ['127.0.0.1:11211'],
         'compress_threshold' => 10_000,
       };

      # Get the value from cache:
      my $value = $cache->get($SessionID);

      # dmp('mamcache value',($value));

      # -- use the value data to find the groups

      my $hashref = unserialize($value);
      # dmp('mamcache unserialized',($hashref));

      my %hash = %{ $hashref };
      %hash = % { $hash {'data'}{chr(0) . 'SimpleSAML_Session' . chr(0) . 'authData'}{'default-sp'}{'Attributes'} };
      my @groups = @ { $hash{'groups'} };

      dmp("3 Comparing $valid_group_expr to", \@groups);
      my $result=evaluate($valid_group_expr,@groups);
      if ($result) {
             dmp("this guy oK",$result);
             return Apache2::Const::HTTP_OK;
      }

     dmp("blowing this guy off",$result);
     $r->log_reason("Not a member of group " . $valid_group_expr);
      return Apache2::Const::FORBIDDEN;

    # return Apache2::Const::HTTP_FORBIDDEN;
    # return Apache2::Const::HTTP_OK;
    # return Apache2::Const::DECLINED;
}

# ======================= utility functions

# evaluate returns the boolean value of the expression $expr
#          after substituting membership information in @groups
#
# valid operators are
#
#    &&, and, AND             logical AND
#    ||, or, OR               logical OR
#    !, NOT, not              logical NOT
#
# expression must be infix and precidence can be indicated by ()

sub evaluate {

   my ($expr,@groups)=@_;
   # print "$expr\n";
   # print Dumper(\%group_hash);

   # operator tokens
   my %token_hash = (
       '(' => '(',
       ')' => ')',
       'AND' => '&&',
       'and' => '&&',
       'or' => '||',
       'OR' => '||',
       '!' => '!',
       'not' => '!',
       'NOT' => '!',
   ) ;

   # add the group array into the token hash as TRUEs
   foreach $v (@groups) {
      $v=~s/ /_/g;
      $token_hash{$v} = 1;
   }
   dmp('merged hash',\%token_hash);

   # merge the two hashes into %token_hash
   # foreach my $tkey ( keys %group_hash) { $token_hash{$tkey} = $group_hash{$tkey}; }
   # print Dumper(\%token_hash);

   $expr=~s/\(/ ( /g;
   $expr=~s/\)/ ) /g;
   $expr=~s/\!/ ! /g;

   # print "$expr\n";
   my @expr_hash=split (/ /,$expr);

   $expr='';
   foreach my $t (@expr_hash) {
       if ($t ne '') {
          if (exists ($token_hash{$t})) { $t = $token_hash{$t} } else {$t = 0;}
          $expr = $expr . "$t ";
       }
   }
   dmp("expression",$expr);
   my $result=0;
   my $assignment="\$result = $expr;";
   dmp("assignment",$assignment);
   eval($assignment);
   dmp("result",$result);
   return $result;

}

# debug dump structure funcion
sub dmp {

   if ($debug == 1) {

     my ($label,@value) = @_;
     my $temp = Dumper(@value);
     open (T, ">>$debug_file"); #   || die "Can't open $debug_file: $!\n";
     print T "$label: $temp\n";
     close (T);

   }

}

1;

PHP 脚本失败

当启用 Perl 授权时,下面的简单 PHP 表单不显示任何值。如果我注释掉 PerlAuthzHandler 行,$_POST'[submit'] 和 $_POST['in1'] 都已设置。

<?php

   if (isset($_POST['submit'])) { print_r($_POST); }

   $form=<<<EOT
      <form name="test" action="y.php" method="post">
         <input name="in1">
         <input type="submit" name="submit">
      </form>
EOT;

   print $form;

?>

同样,身份验证 (simplesamlphp/ADFS) 和授权都按预期工作。例外情况是,当使用授权时,没有可用的 $_POST 变量。

【问题讨论】:

  • 请显示您的 Apache 配置、PerlAuthzHandler 的内容,以及一个简单的、可重现的失败 PHP 脚本示例。
  • 嗨 pha,我已经编辑了您的问题以解决您遇到的格式问题,以及其他一些小的调整。在编号列表中包含代码块很棘手(您需要在列表项和代码块之间使用空的 HTML 注释,例如 &lt;!-- --&gt;,注释前后各有一个空行);不用说,这在Markdown help 中没有描述,所以不用担心不知道。如果您对如何在 SO 上做事有任何疑问,可以搜索 meta.stackoverflow.com,如果还没有问题,请提出问题。
  • 另外,这是一个写得很好的问题。欢迎来到 Stack Overflow,祝你好运!
  • 感谢您的帮助和欢迎!

标签: apache perl authorization mod-perl


【解决方案1】:

分辨率

问题常常是我自己造成的。 perlmonks.org 的一位善良的僧侣指出 Perl 处理程序包含以下行:

$query = new CGI;

删除它。由于我实际上只是访问但没有更改任何内容,因此没有任何意义,实际上 $query 从未使用过。

【讨论】:

    猜你喜欢
    • 2011-03-25
    • 2014-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-01
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多