File upload security in Cybersecurity - Time & Space Complexity
When checking uploaded files for security, the time it takes to scan each file matters.
We want to know how the scanning time grows as more files or bigger files are uploaded.
Analyze the time complexity of the following file scanning process.
for file in uploaded_files:
if file.size > max_size:
reject(file)
else:
for byte in file.content:
if byte matches malicious_pattern:
reject(file)
accept(file)
This code checks each uploaded file's size and scans its content byte by byte for harmful patterns.
Look at what repeats as input grows.
- Primary operation: Scanning each byte of every file's content.
- How many times: Once for each byte in each file, repeated for all files uploaded.
As you upload more files or bigger files, the scanning work grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 files, 1KB each | About 10,000 byte checks |
| 100 files, 1KB each | About 100,000 byte checks |
| 100 files, 10KB each | About 1,000,000 byte checks |
Pattern observation: The scanning time grows roughly with the total number of bytes across all files.
Time Complexity: O(n * m)
This means the time grows with the number of files (n) times the size of each file in bytes (m).
[X] Wrong: "Scanning time depends only on the number of files, not their size."
[OK] Correct: Larger files have more bytes to check, so bigger files take more time even if the file count stays the same.
Understanding how scanning time grows helps you design secure systems that stay fast as users upload more or bigger files.
"What if the scanning only checked the first 100 bytes of each file? How would the time complexity change?"