Skip to content

Lack of Input Validation in Solver Parameters Allows Arbitrary Key-Value Pairs #3968

Open
@anilbeycorintis

Description

@anilbeycorintis

Describe the current issue

Happy New Year! 🎉

Thank you for developing and maintaining Firedrake—it’s an exceptional tool. We’ve noticed an issue that could enhance its robustness and usability.

Currently, the code snippet below runs without any errors, even with invalid or nonsensical solver_parameters. For example:

  • A typo like "snes_rtolx" instead of "snes_rtol" is silently ignored, potentially causing incorrect solver behavior.
  • Providing a parameter with the wrong type, such as None instead of a boolean like False, also doesn’t raise an error, leading to undefined behavior.

This lack of validation can result in unnoticed configuration errors, wasted resources, or incorrect results, particularly when such mistakes persist for extended periods.

Would it be possible to validate parameter names and types before creating the solver object? This improvement would greatly enhance reliability and help users avoid subtle but critical mistakes. Thank you!

import firedrake as fd

# Minimal problem setup
mesh = fd.UnitSquareMesh(1, 1)
V = fd.FunctionSpace(mesh, "CG", 1)
u = fd.Function(V)
v = fd.TestFunction(V)
F = fd.inner(fd.grad(u), fd.grad(v)) * fd.dx

# Arbitrary solver parameters without validation
variational_solver_parameters = {
    "homotopy_iterations": 10,
    "momotopy_iterations": 9,
    "snes_max_it": 10,
    "snes_max_iter": 2,
    "snes_maximum_it": 3,
    "snes_maximum_iter": 4,
    "snes_maximum_iterations": 5,
    "snes_max_its": 6,
    "is_this_a_valid_parameter?": True,
    "hello": "hi!",
    "answer_to_life_the_universe_and_everything": 42,
    "master_yoda": "validate_we_do_not?",
}

problem = fd.NonlinearVariationalProblem(F, u)
solver = fd.NonlinearVariationalSolver(
    problem,
    solver_parameters=variational_solver_parameters,
)
solver.solve()

# Code runs without error, showing no input validation on solver parameters.

Describe the solution you'd like
To address this issue, input validation should be implemented for solver parameters:

  1. Key Validation:

    • The solver should only accept keys that are recognized as valid.
    • If an unrecognized key is provided, the solver should raise an error during initialization.
  2. Type Validation:

    • The solver should check that the value provided for each parameter is of the correct type.
    • If the type is incorrect, the solver should raise a clear error before attempting to solve.

For example, the following input should raise an error:

solver_parameters = {
    "snes_max_it": "ten",  # Invalid type
    "unknown_key": 42,     # Invalid key
}

Additional info

This issue is a classic example of improper input validation, a vulnerability listed by CWE MITRE as CWE-20: Improper Input Validation. Addressing this not only improves user experience but also aligns Firedrake with best practices for software reliability.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions