import gixy
from gixy.plugins.plugin import Plugin


class unanchored_regex(Plugin):
    r"""
    Insecure example:
        location ~ \.php {

        }
    """

    summary = "Regular expressions without anchors can be slow."
    severity = gixy.severity.LOW
    description = "Regular expressions without anchors can be slow."
    help_url = "https://gixy.io/plugins/unanchored_regex/"
    directives = ["location"]

    def audit(self, directive):
        # check for `location ~ \.php` instead of `location ~ \.php$`
        if not directive.is_regex:
            return
        if directive.needs_anchor():
            self.add_issue(
                severity=gixy.severity.LOW,
                directive=directive,
                reason="Regular expressions without anchors can be slow.",
            )
