Skip to content

Commit

Permalink
Secure Source of Randomness (#1)
Browse files Browse the repository at this point in the history
Co-authored-by: pixeebot[bot] <104101892+pixeebot[bot]@users.noreply.github.com>
  • Loading branch information
pixeebot[bot] authored May 2, 2024
1 parent 3a9b23b commit 4af909d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
6 changes: 3 additions & 3 deletions altair/utils/data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json
import os
import random
import hashlib
import warnings
from typing import Union, MutableMapping, Optional, Dict, Sequence, TYPE_CHECKING, List
Expand All @@ -17,6 +16,7 @@


from typing import Protocol, TypedDict, Literal
import secrets


if TYPE_CHECKING:
Expand Down Expand Up @@ -136,7 +136,7 @@ def sample(
"frac cannot be None if n is None and data is a dictionary"
)
n = int(frac * len(values))
values = random.sample(values, n)
values = secrets.SystemRandom().sample(values, n)
return {"values": values}
else:
# Maybe this should raise an error or return something useful?
Expand All @@ -149,7 +149,7 @@ def sample(
"frac cannot be None if n is None with this data input type"
)
n = int(frac * len(pa_table))
indices = random.sample(range(len(pa_table)), n)
indices = secrets.SystemRandom().sample(range(len(pa_table)), n)
return pa_table.take(indices)
else:
# Maybe this should raise an error or return something useful? Currently,
Expand Down
4 changes: 2 additions & 2 deletions altair/utils/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from http import server
from io import BytesIO as IO
import itertools
import random
import secrets

JUPYTER_WARNING = """
Note: if you're in the Jupyter notebook, Chart.serve() is not the best
Expand Down Expand Up @@ -70,7 +70,7 @@ def do_GET(self):
def find_open_port(ip, port, n=50):
"""Find an open port near the specified port"""
ports = itertools.chain(
(port + i for i in range(n)), (port + random.randint(-2 * n, 2 * n))
(port + i for i in range(n)), (port + secrets.SystemRandom().randint(-2 * n, 2 * n))
)

for port in ports:
Expand Down
6 changes: 3 additions & 3 deletions sphinxext/altairgallery.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import hashlib
import os
import json
import random
import collections
from operator import itemgetter
import warnings
Expand All @@ -25,6 +24,7 @@
from altair.utils.execeval import eval_block
from tests.examples_arguments_syntax import iter_examples_arguments_syntax
from tests.examples_methods_syntax import iter_examples_methods_syntax
import secrets


EXAMPLE_MODULE = "altair.examples"
Expand Down Expand Up @@ -278,8 +278,8 @@ def run(self):
if indices:
examples = [examples[i] for i in indices]
if shuffle:
random.seed(seed)
random.shuffle(examples)
secrets.SystemRandom().seed(seed)
secrets.SystemRandom().shuffle(examples)
if size:
examples = examples[:size]

Expand Down

0 comments on commit 4af909d

Please sign in to comment.