Skip to content

Add bounded parsing support for large exponent inputs #115

Closed

Description

@simPod

Summary

BigNumber::of() accepts scientific notation and expands positive exponents into the unscaled integer representation. A small untrusted input can therefore cause a very large allocation.

Reproduction

BigNumber::of('1e1000000000');

The parser expands the value with str_repeat('0', $zeros). Depending on the PHP memory limit, this can exhaust worker memory or terminate the process.

Impact

Applications that parse externally supplied values through BigNumber::of() can be exposed to input-amplification denial of service. Input-length limits alone do not mitigate this, because the triggering input can be very short.

Proposal

Add an opt-in bounded parsing API or parse context that can limit:

  • Input length
  • Absolute exponent
  • Expanded digit count and/or resulting scale

For example, conceptually:

BigNumber::of(
    '1e1000000000',
    parseLimits: new NumberParseLimits(
        maxInputLength: 256,
        maxExponent: 1_000,
        maxExpandedDigits: 10_000,
    ),
);

The parser should throw NumberFormatException when a limit is exceeded.

An opt-in API preserves arbitrary-precision use cases while allowing HTTP, GraphQL, and CLI boundaries to safely parse untrusted input.

Workaround

Callers should reject exponent notation or independently bound exponent magnitude before calling BigNumber::of().

Reported against Brick Math 0.19.0.

Activity

  1. BenMorel commented on Aug 9, 2026

    @BenMorel
    Member

    Hi @simPod,

    I agree that this is an issue that should be fixed in the library itself, in particular due to the materialization of large exponents.

    However, I think the API as proposed is not a good fit:

    • of() accepts int and BigNumber as well, whereas the limits really only make sense for untrusted string inputs
    • NumberParseLimits makes the job harder for the caller, where a single "max digits" number would do

    I propose adding the following methods instead:

    final public static function ofUntrusted(string $value, int $maxDigits): static;
    final public static function ofNullableUntrusted(?string $value, int $maxDigits): ?static;

    Implementation in #116.

    Please let me know what you think!

  2. BenMorel commented on Aug 26, 2026

    @BenMorel
    Member

    Implemented. ofUntrusted() has been renamed to parse(), and you can now control the allowed syntax as well (decimal, exponent, fraction). The new signature is:

    /**
     * @param list<NumberSyntax> $allowedSyntax
     */
    final public static function parse(string $value, array $allowedSyntax, int $maxDigits): static;
    
    /**
     * @param list<NumberSyntax> $allowedSyntax
     */
    final public static function parseNullable(?string $value, array $allowedSyntax, int $maxDigits): ?static;
  3. BenMorel commented on Aug 28, 2026

    @BenMorel
    Member

    Released in version 0.20.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions