fileaudit.json_check module#

License MPL-2.0 (C) 2026 Created by Maikel Mardjan - https://nocomplexity.com/ FileAudit - JSON File Security Checker

exception fileaudit.json_check.FileValidationError(message)[source]#

Bases: Exception

Custom exception for JSON validation failures in FileAudit.

class fileaudit.json_check.HTTPSOnlyRedirectHandler[source]#

Bases: HTTPRedirectHandler

Redirect handler that blocks any redirect to a non-HTTPS URL. Prevents downgrade attacks (e.g. https -> http redirects).

redirect_request(req, fp, code, msg, headers, newurl)[source]#

Return a Request or None in response to a redirect.

This is called by the http_error_30x methods when a redirection response is received. If a redirection should take place, return a new Request to allow http_error_30x to perform the redirect. Otherwise, raise HTTPError if no-one else should try to handle this url. Return None if you can’t but another Handler might.

fileaudit.json_check.limited_parse(obj, max_depth, depth=0)[source]#

Recursively validates nesting depth limits.

fileaudit.json_check.validate_json(func_or_path=None, max_depth=None, max_file_size=None)[source]#

Validate JSON files via decorator or direct invocation.

A JSON file validator that can operate in two modes:

  1. Decorator mode — wraps a function to validate a JSON file path passed as an argument before the function body runs.

  2. Direct call / CLI mode — validates a file immediately and returns a boolean result.

Usage:

@validate_json @validate_json() @validate_json(“custom_arg_name”, max_depth=50) @validate_json(max_file_size=5000) validate_json(“path/to/file.json”, max_depth=10) # CLI / direct call usage

Parameters:
  • func_or_path (callable, str, pathlib.Path, or None) –

    • If a callable: the function to decorate (bare decorator usage: @validate_json).

    • If a str or Path that looks like a file path or URL: the file path to validate (direct call usage).

    • If a str that is a valid Python identifier (not a path): treated as the target argument name to inspect in decorator mode (e.g., @validate_json("config_path")).

    • If None: returns a decorator factory (@validate_json() or @validate_json(max_depth=50)).

  • max_depth (int or None) – Maximum allowed JSON nesting depth. Falls back to DEFAULT_MAX_DEPTH if omitted.

  • max_file_size (int or None) – Maximum allowed file size in bytes. Falls back to DEFAULT_MAX_FILE_SIZE if omitted.

Returns:

  • In decorator mode: the wrapped function.

  • In direct call mode: True if validation passes, False if it fails (errors are printed to stdout).

Return type:

Union[callable, bool, function]

Raises:

FileValidationError – If validation fails in decorator mode, or if the decorated function has no arguments, the target argument is missing, or the argument type is not str or Path.

Examples

Bare decorator (validates the first argument):

@validate_json
def process_data(file_path):
    ...

Decorator with custom limits:

@validate_json(max_depth=50, max_file_size=5000)
def process_data(file_path):
    ...

Decorator targeting a specific argument by name:

@validate_json("config_path", max_depth=10)
def process_data(config_path, other_arg):
    ...

Direct call / CLI usage:

result = validate_json("path/to/file.json", max_depth=10)
# Returns True on success, False on failure.