Skip to content

nserver.rules 🔗

ALL_QTYPES module-attribute 🔗

ALL_QTYPES: List[str] = list(dnslib.QTYPE.reverse.keys())

All supported Query Types

New in 2.0.

ResponseFunction module-attribute 🔗

ResponseFunction = Callable[[Query], RuleResult]

Type Alias for functions that will be called when a rule is matched

RuleResult module-attribute 🔗

RuleResult = Union[
    Response, RecordBase, List[RecordBase], None
]

Type Alias for the result of a rule response function

RegexRule 🔗

RegexRule(
    regex: Pattern,
    allowed_qtypes: List[str],
    func: ResponseFunction,
    case_sensitive: bool = False,
)

Bases: RuleBase

Rule that uses the provided regex to attempt to match the query name.

Parameters:

Name Type Description Default
regex Pattern

compiled regex for matching

required
allowed_qtypes List[str]

match only the given query types

required
func ResponseFunction

response function to call

required
case_sensitive bool

how to case when matching if False will recompile regex with re.IGNORECASE

False

get_func 🔗

get_func(query: Query) -> Optional[ResponseFunction]

Same as parent class

RuleBase 🔗

Base class for all Rules to inherit from.

get_func 🔗

get_func(query: Query) -> Optional[ResponseFunction]

From the given query return the function to run, if any.

If no function should be run (i.e. because it does not match the rule), then return None.

This is to allow more efficient methods when determining a match and getting the rule function may be expensive (e.g. blueprints).

StaticRule 🔗

StaticRule(
    match_string: str,
    allowed_qtypes: List[str],
    func: ResponseFunction,
    case_sensitive: bool = False,
)

Bases: RuleBase

Rule that matches only the given string

StaticRule is more efficient than using a WildcardStringRule for static strings.

New in 2.0.

Parameters:

Name Type Description Default
match_string str

string to match

required
allowed_qtypes List[str]

match only the given query types

required
func ResponseFunction

response function to call

required
case_sensitive bool

how to case when matching

False

get_func 🔗

get_func(query: Query) -> Optional[ResponseFunction]

Same as parent class

WildcardStringRule 🔗

WildcardStringRule(
    wildcard_string: str,
    allowed_qtypes: List,
    func: ResponseFunction,
    case_sensitive: bool = False,
)

Bases: RuleBase

Rule that allows a more concise way of matching query names.

The following substitutions can be made:

  • * will match a single domain label
  • ** will match multiple domain labels
  • base_domain will match the registered domain using the Public Suffix List (PSL)

Examples:

  • _dmarc.{base_domain}
  • *._dkim.**
  • foo.*.bar.com

When operating with case_sensitive=False, both the wildcard string and the query name are covereted to lowercase prior to matching.

Parameters:

Name Type Description Default
wildcard_string str

wildcard string to use

required
allowed_qtypes List

match only the given query types

required
func ResponseFunction

response function to call

required
case_sensitive bool

how to case when matching

False

get_func 🔗

get_func(query: Query) -> Optional[ResponseFunction]

Same as parent class

ZoneRule 🔗

ZoneRule(
    zone: str,
    allowed_qtypes: List[str],
    func: ResponseFunction,
    case_sensitive: bool = False,
)

Bases: RuleBase

Rule that matches the given domain or any subdomain

An empty zone ("") will match any domain as this refers to the domain root (.).

New in 2.0.

Parameters:

Name Type Description Default
zone str

zone root

required
allowed_qtypes List[str]

match only the given query types.

required
func ResponseFunction

response function to call

required
case_sensitive bool

how to case when matching

False

get_func 🔗

get_func(query: Query) -> Optional[ResponseFunction]

Same as parent class

smart_make_rule 🔗

smart_make_rule(
    rule: Union[Type[RuleBase], str, Pattern],
    *args,
    **kwargs
) -> RuleBase

Create a rule using shorthand notation.

The exact type of rule returned depends on what is povided by rule.

If rule is a

  • RuleBase class, then it is used directly.
  • str then it is checked to see if it contains substitutions. If it does then it will be a WildcardStringRule, else a StaticRule.
  • Pattern then a RegexRule.

New in 2.0

Parameters:

Name Type Description Default
rule Union[Type[RuleBase], str, Pattern]

input to process

required
args

extra arguments to provide to the constructor

()
kwargs

extra keyword arguments to provide to the constructor

{}