← Back to Pull requests

[Swift] Verify size-prefixed roots from reader position #9196

Merged
carrerasdarren-cell merged 3 files (108 lines) on Aug 11, 2026 19:49 UTC
View on GitHub
carrerasdarren-cell carrerasdarren-cell commented on Aug 07, 2026 00:49 UTC
Author

Summary

  • verify Swift roots from the active ByteBuffer.reader position

  • verify optional file identifiers from that same position

  • add regression coverage for malformed decoy roots, truncated identifiers, and a valid prefixed identifier
  • Problem

    Both size-prefixed checked-root APIs call skipPrefix() before delegating to getCheckedRoot. The returned table is constructed from the post-prefix reader position, but verification previously followed a root offset from byte zero and checked a file identifier at byte four.

    A malformed size-prefixed buffer could therefore place a valid decoy root where the verifier looked while returning a different malformed table from the post-prefix root. The same malformed table is rejected when checked directly, but the prefixed wrappers accepted it. Generated accessors or mutators could then operate outside the buffer bounds despite the checked API succeeding.

    Fix

    Capture the active reader position once and use it consistently for file-identifier verification, root verification, and table construction. The file-identifier bounds check now accounts for that position without wrapping on short buffers.

    Validation

  • added a Swift regression covering both size-prefixed checked-root APIs

  • added short file-identifier bounds coverage and a valid prefixed control

  • parsed all modified Swift source and test files with swiftc

  • compiled the production Swift sources with a deterministic harness; direct malformed verification rejected, both patched prefixed paths rejected, a truncated identifier rejected, and a valid prefixed identifier was accepted

  • git diff --check passes
  • Diff: 108 lines (+97 / -11) across 3 files Reviewer: mustiikhalil Review duration: 6900m
    All Vouch review confidence checks passed
    Residual risk 0.07 = Change Risk (0.34) × (1 − Review Confidence (0.79)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.34 · 108 lines diff across 3 files
    0.34
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.65 · 5 substantive comment(s)
    0.65
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    79%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.281
    Real PR Conversation Timeline
    14 items
    mustiikhalil mustiikhalil commented on Aug 07, 2026 06:38 UTC
    Contributor

    @carrerasdarren-cell Thanks for opening the PR. I was looking at the cpp approach. And it would be better if the swift implementation also follows a similar convention. Where we only skip after we have verified the prefixed value too. It's fine if we duplicate the logic for the verification here getPrefixedSizeCheckedRoot.

    https://github.com/google/flatbuffers/blob/81edeb17d9118143f2c81caf27edfb0df401279e/include/flatbuffers/verifier.h#L256
    https://github.com/google/flatbuffers/blob/81edeb17d9118143f2c81caf27edfb0df401279e/tests/monster_test.cpp#L609

    carrerasdarren-cell carrerasdarren-cell commented on Aug 07, 2026 07:23 UTC
    Author

    Thanks, updated in 83b8c03. Both checked size-prefixed entry points now read and validate the UOffset prefix before calling skipPrefix(). getPrefixedSizeCheckedRoot applies the C++-style prefix <= available bytes check, while getCheckedPrefixedSizeRoot preserves its exact-size requirement. I also added an oversized-prefix regression that verifies rejection occurs without advancing ByteBuffer.reader. The production Swift modules compile with Swift 6.3.3, the modified sources/tests parse, git diff --check passes, and the deterministic malformed/valid harness still passes.

    mustiikhalil mustiikhalil commented on code on Aug 07, 2026 07:44 UTC
    Code Review
    swift/Sources/FlatBuffers/Verifier.swift:1
    @@ -215,12 +215,16 @@ public struct Verifier {
       }
     
       @inline(__always)
    -  func verify(id: String) throws {
    +  func verify(id: String, at position: Int) throws {
         let size = MemoryLayout<Int32>.size
    -    guard storage.capacity >= (size &* 2) else {
    +    guard
    +      position >= 0,
    +      position <= storage.capacity,
    +      storage.capacity - position >= size * 2

    nit: & instead of normal

    mustiikhalil mustiikhalil commented on code on Aug 07, 2026 07:44 UTC
    Code Review
    swift/Sources/FlatBuffers/Verifier.swift:1
    @@ -215,12 +215,16 @@ public struct Verifier {
       }
     
       @inline(__always)
    -  func verify(id: String) throws {
    +  func verify(id: String, at position: Int) throws {
         let size = MemoryLayout<Int32>.size
    -    guard storage.capacity >= (size &* 2) else {
    +    guard
    +      position >= 0,
    +      position <= storage.capacity,
    +      storage.capacity - position >= size * 2
    +    else {
           throw FlatbuffersErrors.bufferDoesntContainID
         }
    -    let str = _buffer.readString(at: size, count: size)
    +    let str = _buffer.readString(at: position + size, count: size)

    nit: &+

    mustiikhalil mustiikhalil commented on code on Aug 07, 2026 07:55 UTC
    Code Review
    swift/Sources/FlatBuffers/Root.swift:1
    @@ -16,6 +16,30 @@
     
     import Foundation
     
    +@inline(__always)
    +private func verifySizePrefix(
    +  byteBuffer: inout ByteBuffer,
    +  requireExactSize: Bool,
    +  options: VerifierOptions) throws
    +{
    +  let verifier = try Verifier(buffer: &byteBuffer, options: options)
    +  let prefixPosition = byteBuffer.reader
    +  let prefix: UOffset = try verifier.getValue(at: prefixPosition)
    +  let availableSize = byteBuffer.size &- UOffset(MemoryLayout<UOffset>.size)
    +
    +  if requireExactSize {
    +    guard prefix == availableSize else {
    +      throw FlatbuffersErrors.prefixedSizeNotEqualToBufferSize
    +    }
    +  } else if prefix > availableSize {
    +    throw FlatbuffersErrors.outOfBounds(
    +      position: UInt(prefixPosition)
    +        &+ UInt(MemoryLayout<UOffset>.size)
    +        &+ UInt(prefix),
    +      end: byteBuffer.capacity)
    +  }

    Not needed; but if you see fit, and the logic is movable. Would it make sense for this to live in the verifier instead?

    mustiikhalil mustiikhalil commented on Aug 07, 2026 07:55 UTC
    Reviewer

    LGTM! amazing work! one of the comments can be disregarded if not needed

    mustiikhalil mustiikhalil commented on Aug 11, 2026 19:00 UTC
    Contributor

    @carrerasdarren-cell just pinging you so we can get this PR to the finish line

    carrerasdarren-cell carrerasdarren-cell commented on code on Aug 11, 2026 19:11 UTC
    Code Review
    swift/Sources/FlatBuffers/Verifier.swift:1
    @@ -215,12 +215,16 @@ public struct Verifier {
       }
     
       @inline(__always)
    -  func verify(id: String) throws {
    +  func verify(id: String, at position: Int) throws {
         let size = MemoryLayout<Int32>.size
    -    guard storage.capacity >= (size &* 2) else {
    +    guard
    +      position >= 0,
    +      position <= storage.capacity,
    +      storage.capacity - position >= size * 2

    Addressed in b76d5d5.

    carrerasdarren-cell carrerasdarren-cell commented on code on Aug 11, 2026 19:11 UTC
    Code Review
    swift/Sources/FlatBuffers/Verifier.swift:1
    @@ -215,12 +215,16 @@ public struct Verifier {
       }
     
       @inline(__always)
    -  func verify(id: String) throws {
    +  func verify(id: String, at position: Int) throws {
         let size = MemoryLayout<Int32>.size
    -    guard storage.capacity >= (size &* 2) else {
    +    guard
    +      position >= 0,
    +      position <= storage.capacity,
    +      storage.capacity - position >= size * 2
    +    else {
           throw FlatbuffersErrors.bufferDoesntContainID
         }
    -    let str = _buffer.readString(at: size, count: size)
    +    let str = _buffer.readString(at: position + size, count: size)

    Addressed in b76d5d5.

    carrerasdarren-cell carrerasdarren-cell commented on Aug 11, 2026 19:12 UTC
    Author

    Thanks for the ping. I addressed the remaining wrapping-arithmetic nits in b76d5d5, replied to the optional placement suggestion, and resolved all three review threads. The production Swift module compiles cleanly locally with Swift 6.3.3; the refreshed CI run is now starting.

    mustiikhalil mustiikhalil approved these changes on Aug 11, 2026 19:49 UTC
    Approved Reviewer

    Approved these changes.

    mustiikhalil mustiikhalil commented on Aug 11, 2026 19:50 UTC
    Contributor

    @carrerasdarren-cell thanks for your contribution

    carrerasdarren-cell carrerasdarren-cell commented on Sep 04, 2026 17:45 UTC
    Author

    Ownership verification for Google OSS VRP report 543524003: I am the author of this pull request and the reporter of that issue. The report was submitted using artemis@perpetualsai.com; this GitHub account uses its GitHub-provided noreply commit address.

    Reviewers
    Assignees
    carrerasdarren-cell carrerasdarren-cell
    Labels
    risk: low confidence: 79.3% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 34.0% (LOW) · Higher = larger blast radius
    34.0%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 79.3% · Lower = rushed or shallow review
    79.3%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 7% · Safe to merge
    7%