从Nginx隐藏密码查询参数error_log

时间:2018-10-02 22:50:40

标签: nginx logging rate-limiting

我使用Oauth2密码授予来为我的客户端生成访问令牌。这意味着用户密码将作为查询参数发送:

example.com/oauth/token?grant_type=password&username=MyUsername&password=MyPassword

我使用以下log_filter来屏蔽Nginx access_log进行身份验证的密码:

http {
    log_format hide_password '$remote_addr $remote_user [$time_local] "$request_without_password" $status $body_bytes_sent "$http_referer" "$http_user_agent"';

    ...

    server {
        set $request_without_password $request;
        if ($request_without_password ~ (.*)password=[^&]*(.*)) {
            set $request_without_password $1password=****$2;
        }
        access_log /var/log/nginx/access.log hide_password;

        ....
    }
}

我现在使用Nginx的limit_req引入了速率限制。所有受速率限制的请求都记录到Nginx error_log中。问题是,似乎无法在log_filter上使用error_log,这意味着速率受限的身份验证请求将在用户密码可见的情况下记录,这当然是不可接受的。由于我想跟踪速率受限的请求,因此我不想禁用error_log

是否可以在Nginx中记录速率受限的请求,而密码却不显示为查询参数?

1 个答案:

答案 0 :(得分:2)

这并不意味着密码需要作为查询参数发送,并且密码不应作为查询参数发送。值应在正文中正确发送,specified in the Oauth protocol

  

客户端通过添加
向令牌端点发出请求   以下参数使用“ application / x-www-form-urlencoded”
  附录B中的格式,在HTTP中使用UTF-8字符编码
  请求实体-正文

     

grant_type            需要。值必须设置为“密码”。

     

用户名            需要。资源所有者的用户名。

     

密码            需要。资源所有者密码。

     

范围           可选的。访问请求的范围,如           3.3节。

另请参阅the example in the same section of the Oauth specification

 POST /token HTTP/1.1
 Host: server.example.com
 Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
 Content-Type: application/x-www-form-urlencoded

 grant_type=password&username=johndoe&password=A3ddj3w

因此,请遵循标准-不要将其放在查询参数中,这样就不会出现此问题。