Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 15, 2025

This PR closes #3349

Description

Adds MASTG v2 test and demo for detecting insecure random number generation in iOS apps.

Changes

  • Test: tests-beta/ios/MASVS-CRYPTO/MASTG-TEST-0063.md

    • Static analysis test for insecure random APIs (rand, random, srand, srandom)
    • Maps to MASWE-0027 (Insecure Random API Usage)
    • Focuses on security-relevant contexts: keys, tokens, nonces, IVs
  • Demo: demos/ios/MASVS-CRYPTO/MASTG-DEMO-0063/

    • Swift sample contrasting insecure (rand()/srand()) vs secure (SecRandomCopyBytes)
    • radare2 script detecting insecure random function calls
    • Expected disassembly output showing symbol references
// Insecure: predictable, deterministic
srand(UInt32(time(nil)))
let token = rand() % 256

// Secure: cryptographically secure from entropy pool
SecRandomCopyBytes(kSecRandomDefault, count, &randomBytes)

Dynamic analysis omitted per issue requirements.


[x] I have read the contributing guidelines.

Original prompt

This section details on the original issue you should resolve

<issue_title>MASTG v1->v2 MASTG-TEST-0063: Testing Random Number Generation (ios)</issue_title>
<issue_description>
Create a new MASTG v2 test covering for":

Follow the guidelines
</issue_description>

<agent_instructions>Create a static test in tests-beta similar to others and also one demo. Ignore the dynamic analysis part of the original test.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI self-assigned this Nov 15, 2025
Copilot AI changed the title [WIP] Add testing for random number generation in MASTG v2 Add MASTG-TEST-0063: Testing Random Number Generation (iOS) Nov 15, 2025
Copilot finished work on behalf of cpholguera November 15, 2025 20:28
Copilot AI requested a review from cpholguera November 15, 2025 20:28
@cpholguera cpholguera requested a review from Copilot November 20, 2025 09:01
Copilot finished reviewing on behalf of cpholguera November 20, 2025 09:05
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds comprehensive MASTG v2 test coverage for detecting insecure random number generation on iOS, addressing issue #3349.

Key Changes:

  • New static analysis test (MASTG-TEST-xx63) for detecting insecure random APIs (rand, srand, drand48)
  • Comprehensive knowledge article (MASTG-KNOW-0070) explaining iOS random number generation APIs
  • Two demos: radare2-based static detection (MASTG-DEMO-0063) and frida-trace runtime monitoring (MASTG-DEMO-0064)
  • Best practices guide (MASTG-BEST-00x1) recommending secure random APIs
  • Deprecation of old v1 test with reference to new version

Reviewed Changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 13 comments.

Show a summary per file
File Description
tests/ios/MASVS-CRYPTO/MASTG-TEST-0063.md Marks v1 test as deprecated and references new v2 test
tests-beta/ios/MASVS-CRYPTO/MASTG-TEST-xx63.md New v2 static test for insecure random API detection, maps to MASWE-0027
knowledge/ios/MASVS-CRYPTO/MASTG-KNOW-0070.md Comprehensive documentation of iOS random number generation including secure/insecure APIs with code examples
demos/ios/MASVS-CRYPTO/MASTG-DEMO-0063/ Static analysis demo using radare2 to detect insecure random function calls with Swift sample code
demos/ios/MASVS-CRYPTO/MASTG-DEMO-0064/ Dynamic analysis demo using frida-trace to monitor random API usage at runtime
best-practices/MASTG-BEST-00x1.md Best practices guide for using secure random number generators on iOS

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +25 to +26
- In Flutter or Dart use [`Random.secure()`](https://api.flutter.dev/flutter/dart-math/Random/Random.secure.html), which is documented as cryptographically secure. It reaches `SecRandomCopyBytes` through [the platform integration layers](https://github.com/dart-lang/sdk/blob/47e77939fce74ffda0b7252f33ba1ced2ea09c52/runtime/bin/crypto_macos.cc#L16). See [this article](https://www.zellic.io/blog/proton-dart-flutter-csprng-prng/) for a security review.
- In React Native use a library such as [`react-native-secure-random`](https://github.com/robhogan/react-native-securerandom) or [`react-native-get-random-values`](https://github.com/LinusU/react-native-get-random-values), which internally calls `SecRandomCopyBytes` on iOS.
Copy link

Copilot AI Nov 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing punctuation in list items: According to the guidelines, list items that are complete sentences should end with periods. These list items are complete sentences and should have periods at the end.

Copilot generated this review using guidance from repository custom instructions.
Comment on lines +31 to +32
- `rand` and `srand`, which expose the insecure libc PRNG.
- `drand48`, which also uses an insecure linear congruential generator.
Copy link

Copilot AI Nov 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing punctuation in list items: According to the guidelines, list items that are complete sentences should end with periods. These list items are complete sentences and should have periods at the end.

Copilot generated this review using guidance from repository custom instructions.

### Sample

The following sample demonstrates various methods of generating random tokens, and contrasts insecure and secure approaches. It includes
Copy link

Copilot AI Nov 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incomplete sentence: The paragraph ends with "It includes" but doesn't complete the thought before starting the list. Either complete the introductory sentence (e.g., "It includes:") or rephrase to make it a complete sentence. Per the guidelines, introductory sentences before lists should be complete.

Copilot generated this review using guidance from repository custom instructions.
profiles: [L1, L2]
status: deprecated
covered_by: [MASTG-TEST-xx63]
deprecation_note: New version available in MASTG V2
Copy link

Copilot AI Nov 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent capitalization: "MASTG V2" should be "MASTG v2" (lowercase 'v') to maintain consistency with how version numbers are typically written.

Suggested change
deprecation_note: New version available in MASTG V2
deprecation_note: New version available in MASTG v2

Copilot uses AI. Check for mistakes.
Comment on lines +13 to +15
- Insecure methods using libc `rand`, `srand`, and `drand48`
- Other secure methods such as direct reads from `/dev/random`, `arc4random`, `arc4random_uniform`, `SystemRandomNumberGenerator`, and `CCRandomGenerateBytes`
- A preferred secure method using `SecRandomCopyBytes`
Copy link

Copilot AI Nov 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing punctuation in list items: According to the guidelines, list items that complete the introductory sentence should have punctuation. These list items should end with commas (except the last) or be reformatted as complete sentences with periods.

Copilot generated this review using guidance from repository custom instructions.
Comment on lines 12 to 15
- **Security Framework (preferred)**: Use the [`SecRandomCopyBytes`](https://developer.apple.com/documentation/security/secrandomcopybytes(_:_:_:)) API from the Security framework, which produces cryptographically secure random bytes backed by the system CSPRNG.
- **CommonCrypto**: You _could_ use `CCRandomCopyBytes` or `CCRandomGenerateBytes` (not documented on the Apple Developers website), which are also backed by the system CSPRNG. However, prefer `SecRandomCopyBytes` which is a wrapper around these functions.
- **Swift Standard Library**: You can use the Swift Standard Library `.random` APIs which are backed by `SystemRandomNumberGenerator`. However, note that their random number generator can be customized, so ensure you use the default `SystemRandomNumberGenerator` (e.g., by not specifying a custom generator) or a secure alternative (ensure it is cryptographically secure).
- ***CryptoKit**: CryptoKit doesn't expose a direct random byte generator, but it provides secure random nonces and keys through its cryptographic operations, which are backed by the system CSPRNG. For example, you can use `SymmetricKey` for keys and `AES.GCM.Nonce` for nonces without needing to manage raw random bytes directly.
Copy link

Copilot AI Nov 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing punctuation in list items: According to the guidelines, list items that are complete sentences should end with periods. These list items appear to be complete sentences and should have periods at the end.

Copilot generated this review using guidance from repository custom instructions.
@@ -0,0 +1,72 @@
---

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Duplicate MASTG-DEMO ID Detected

This file has the ID MASTG-DEMO-0063 which already exists in demos/android/MASVS-PLATFORM/MASTG-DEMO-0063/MASTG-DEMO-0063.md.

IMPORTANT: Please use the next available ID: MASTG-DEMO-0068

@@ -0,0 +1,38 @@
---

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Duplicate MASTG-DEMO ID Detected

This file has the ID MASTG-DEMO-0064 which already exists in demos/android/MASVS-STORAGE/MASTG-DEMO-0064/MASTG-DEMO-0064.md.

IMPORTANT: Please use the next available ID: MASTG-DEMO-0069

@cpholguera cpholguera requested a review from serek8 November 20, 2025 16:49
@cpholguera cpholguera marked this pull request as ready for review November 20, 2025 16:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MASTG v1->v2 MASTG-TEST-0063: Testing Random Number Generation (ios)

2 participants