Class GuardRuleParser

java.lang.Object
org.frontcache.guard.GuardRuleParser

public class GuardRuleParser extends Object
Parses one line of guard-rules.conf into a GuardRule. Line format (see docs/guard-getting-started.md): <name> | <predicate> ; <predicate> ... | <action> [| dry-run] [| on-error=allow] [| trap] A rate: predicate is moved to the END of its condition here (see parseCondition(String, String)); a rule may hold at most one. Actions: allow reject:<status> [body] redirect:<301|302|303|307|308> <target> challenge <provider> [else <reject:...|redirect:...>] Every rejection is reported as a GuardConfigException so the caller can skip that single line and keep the rest of the file working.
  • Field Details

    • ON_ERROR_ALLOW

      public static final String ON_ERROR_ALLOW
      The flag an allow rule naming a plugin's predicate (ext:) must carry: its author has accepted that when the predicate has no answer - the plugin threw, is over its budget, or is absent - the rule does not match, so the exemption does not apply to that request.
      See Also:
    • TRAP

      public static final String TRAP
      The flag of a honeypot rule - a path no visitor is ever sent to, whose fetch identifies the client: the rule matches a client's request only, never an include, and its log line names the address this node resolved (GuardRule.logReason(RequestContext, boolean)). Refused on an allow rule, which records nothing.
      See Also:
  • Constructor Details

    • GuardRuleParser

      public GuardRuleParser(Set<String> allowedRedirectHosts)
      Parameters:
      allowedRedirectHosts - hosts an absolute redirect target may point at. Relative targets are always allowed; anything else is refused at load time, so a stray config line cannot point the whole site at another host.
    • GuardRuleParser

      public GuardRuleParser(Set<String> allowedRedirectHosts, VisitorTypes visitorTypes)
      Parameters:
      visitorTypes - what client-type: is checked against; null - the node's loaded vocabulary, read when each line is parsed. A validator with no node passes the bots.conf declarations.
  • Method Details

    • parse

      public GuardRule parse(String line)
    • splitFields

      public static List<String> splitFields(String line)
      Splits a rule line into its name | condition | action [| flag] fields. Public because bots.conf uses the same line format and must split it the same way - see BotRuleParser. One splitter, so the alternation bug below cannot be re-introduced in the second file. The obvious line.split("\\|") is wrong, and was: | is also regex alternation, so a perfectly ordinary predicate like uri~^/(recent|user)-contributions\.htm got shredded at the alternation and the rule was refused at load time as "invalid regex ... Unclosed group". The rest of the file kept working, which is what made it invisible: on a production node this silently disabled 5 of 19 rules - and they were every anti-scraper redirect, i.e. exactly the rules whose absence costs nothing until a flood arrives. So a | separates fields only where a regex cannot be using it: outside any (...) group, outside any [...] character class, and not backslash-escaped. Fields come back byte-identical - nothing is unescaped - so \| still reads as a literal pipe to the regex engine, and a top-level alternation is written the way it already was, in parentheses.