I am testing iThemes security plugin for Wordpress. It adds the following code:

        # Rules to help reduce spam
        location /wp-comments-post.php {
            valid_referers jetpack.wordpress.com/jetpack-comment/ *.mysite.com;
            set $rule_0 0;
            if ($request_method ~ "POST"){ set $rule_0 1$rule_0; }
            if ($invalid_referer) { set $rule_0 2$rule_0; }
            if ($http_user_agent ~ "^$"){ set $rule_0 3$rule_0; }
            if ($rule_0 = "3210") { return 403; }

The author describes it as "This option will cut down on comment spam by denying comments from bots with no referrer or without a user-agent identified."

it looks like to me that ALL the conditions must be met.

What does this code really do? What does $rule_0 = "3210" mean?

  • 1
    Looks like it set $rule_0 to 0 and then prepend "1" to it if the first test is true, and then prepend "2" to the string built so far if the second test is true and finally prepend "3" if the third test is true. So if all tests are true, $rule_0 ends up with the value "3210". Indeed, it looks that ALL the conditions must be met to return 403. – lgeorget Apr 15 '14 at 11:31

NOTE: Apparently this is not php - this is merely an nginx config file, as is noted in the comments below.

I don't know php but this looks pretty straight forward. This is how I think it works:

valid_referers jetpack.wordpress.com/jetpack-comment/ *.mysite.com;
        set $rule_0 0;

That just sets up default ok values. Notice here that we're defining $rule0_0 to have a value of 0.

 if ($request_method ~ "POST"){ set $rule_0 1$rule_0; }

If the http request is BITWISE NOT the word POST, set $rule_0 to equal 10.

It continues in that pattern, adding 2 and 3 to the value of $rule_0 if there is an invalid referrer or an invalid user_agent.

if ($rule_0 = "3210") { return 403; }

Here if there has been a combination of invalid $request_method, $http_user_agent and referrer then $rule_0 will equal 3210 and the php server will return a 403: FORBIDDEN.

  • 2
    This is not PHP, it is nginx configuration file ;) – phoops Apr 15 '14 at 11:41
  • It looks a like php to me... Can its config interpret php? – mikeserv Apr 15 '14 at 11:57
  • No it cannot. It only looks similar. – phoops Apr 15 '14 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.