Skip to content

Commit

Permalink
Restrict the length of JSON output indent to 32
Browse files Browse the repository at this point in the history
Too big a value and the system will exhause memory. Normally I
trust users not to do things like but it doesn't hurt to constrain
sometimes.

Fixes: freeipa#197

Signed-off-by: Rob Crittenden <rcritten@redhat.com>
  • Loading branch information
rcritten committed Mar 28, 2023
1 parent c1091f5 commit 23a4af1
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/ipahealthcheck/core/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright (C) 2019 FreeIPA Contributors see COPYING for license
#

from argparse import ArgumentTypeError
from datetime import datetime
import json
import sys
Expand All @@ -16,6 +17,26 @@ class OutputRegistry(Registry):
output_registry = OutputRegistry()


class Int:
def __init__(self, minimum=0, maximum=0):
self.minimum = minimum
self.maximum = maximum

def __call__(self, arg):
try:
value = int(arg)
except ValueError:
raise ArgumentTypeError("'%s' is not an integer" % arg)

if (value < self.minimum) or (value > self.maximum):
raise ArgumentTypeError(
"'%s' is not in the range %s-%s"
% (value, self.minimum, self.maximum)
)

return value


class Output:
"""Base class for writing/displaying the output of results
Expand Down Expand Up @@ -105,7 +126,7 @@ class JSON(Output):
"""Output information in JSON format"""

options = (
('--indent', dict(dest='indent', type=int, default=2,
('--indent', dict(dest='indent', type=Int(0, 32), default=2,
help='Indention level of JSON output')),
)

Expand Down

0 comments on commit 23a4af1

Please sign in to comment.