Insecure Random Number Generation in PyTorch Dataset
Description
Using the NumPy RNG inside of a PyTorch dataset can lead to issues with loading data, including identical augmentations. Instead, use the random number generators built into Python and PyTorch.
Examples
Insecure Code
python
import numpy
class MyDataset(torch.utils.data.Dataset):
def __getitem__(self, index):
# Using NumPy RNG
random_value = numpy.random.randint(0, 10)
return random_valueSecure Code
python
import torch
class MyDataset(torch.utils.data.Dataset):
def __getitem__(self, index):
# Using PyTorch RNG
random_value = torch.randint(0, 10, (1,)).item()
return random_valueRemediation
Replace NumPy RNG with PyTorch's random number generator.
Rule Details
| Field | Value |
|---|---|
| ID | CODE-0249 |
| Category | InsecureConfig |
| Severity | MEDIUM |
| CWE | CWE-330 |
| Confidence | HIGH |
| Impact | LOW |
| Likelihood | MEDIUM |
| Exploitability | MODERATE |
| Tags | random number generation, pytorch dataset |
| OWASP | N/A |