Skip to content

Hook cheat sheet: Swift ​

How to add a vulnboxd runtime hook to a Swift service by hand. Find your setup in the table, then follow its block.

  • Without source access: only the compose file changes; redeploy. With source access: you change the code or Dockerfile and rebuild.
  • Hub automation says how much the hub's proposals do for you. Fully: approve the proposal in the hub; the block is the fallback when it failed. With conditions: the same, when the conditions listed hold. Partly: the hub pushes the compose part, the rest is yours. Not at all: everything is by hand.
  • /usr/local/lib/vulnboxd/trace is the hook directory vulnboxd installs on the vulnbox. Compose snippets mount it read-only at /opt/vbxtrace in the container.
  • How to check that the hook works and undo it is at the end of the page.
  • Runtime hooks explains what a hook does.

Swift ​

SetupUse this whenChangeHub automation
Add the vulnboxd hook to a Swift service (Vapor / Hummingbird / SwiftNIO)Depends on vapor, hummingbird or swift-nio (in its manifest)source, rebuildpartly

With source access ​

These change the service's code or Dockerfile, then it is rebuilt.

Add the vulnboxd hook to a Swift service (Vapor / Hummingbird / SwiftNIO) ​

  • Use this when: Swift service that depends on vapor, hummingbird or swift-nio (in its manifest).
  • Hub automation: partly. The hub pushes the compose lines; the source change is yours.
  • Check and undo: hook mounted into the container

Vendors the VBXTrace SwiftPM package into vendor/VBXTrace (path dependency, no other dependencies), copies the framework's integration file into the app target, installs the middleware (Vapor: app.middleware.use(VBXTraceMiddleware(), at: .beginning); Hummingbird: Router(context: VBXRequestContext.self) + VBXTraceMiddleware(); NIO: VBXHTTPServerHandler after the HTTP codec), and replaces Process.run / open / ClientBootstrap.connect / Task.detached with the VBXTrace helpers where their effects should be linked; mounts the hook directory read-only.

1. Compose: merge into the service (app is its name) and redeploy:

yaml
services:
  app:
    volumes:
      - /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro

2. Source change, then rebuild:

  1. vendor: run /usr/local/lib/vulnboxd/trace/native/swift/vendor.sh <service-dir> (copies the VBXTrace package to vendor/VBXTrace with vbxhook.c/.h and vbxtrace.h in its C target)
  2. Package.swift: dependencies += .package(path: "vendor/VBXTrace"); the executable target's dependencies += .product(name: "VBXTrace", package: "VBXTrace"); make sure the Dockerfile copies vendor/ before swift build (and before swift package resolve if it resolves in an earlier layer)
  3. copy the integration file into the executable target (Sources/<App>/): native/swift/integrations/VBXTraceVapor.swift (Vapor), VBXTraceHummingbird.swift (Hummingbird), VBXTraceNIO.swift (plain NIO, and for NIO ClientBootstrap.vbxConnect with any framework)
  4. Vapor: app.middleware.use(VBXTraceMiddleware(), at: .beginning) in configure (request index unknown: Vapor exposes no connection)
  5. Hummingbird 2: Router(context: VBXRequestContext.self) instead of BasicRequestContext (or add let vbxConnection: VBXConnection = VBXConnection.of(source.channel) to the existing context and conform it to VBXTraceRequestContext), then router.add(middleware: VBXTraceMiddleware()) as the first middleware
  6. plain NIO: add VBXHTTPServerHandler() right after configureHTTPServerPipeline(withPipeliningAssistance: true), before the app's handler
  7. Process: try process.run() → try VBXTrace.run(process); open(path, flags, mode) → VBXTrace.open(path, flags, mode); FileHandle(forWritingAtPath:) → VBXTrace.opened(FileHandle(forWritingAtPath:)); ClientBootstrap.connect(host:port:) → .vbxConnect(host:port:); Task.detached { } → VBXTrace.detached { }; DispatchQueue.async { } → VBXTrace.dispatch(on:execute:)
  8. Swift >= 5.10 (the integrations use nonisolated(unsafe)); Linux glibc images (the default build dlopen()s the core)

Example change on a sample service (hooks/native/c3/proposals/swift-vapor.diff):

diff
--- a/Package.swift
+++ b/Package.swift
@@ -9,6 +9,7 @@ let package = Package(
         .package(url: "https://github.com/vapor/vapor.git", from: "4.110.0"),
         .package(url: "https://github.com/vapor/fluent.git", from: "4.12.0"),
+        .package(path: "vendor/VBXTrace"),
     ],
     targets: [
         .executableTarget(
@@ -16,6 +17,7 @@ let package = Package(
             dependencies: [
                 .product(name: "Vapor", package: "vapor"),
                 .product(name: "Fluent", package: "fluent"),
+                .product(name: "VBXTrace", package: "VBXTrace"),
             ]
         ),
     ]
--- a/Sources/App/configure.swift
+++ b/Sources/App/configure.swift
@@ -1,7 +1,9 @@
 import Vapor
+import VBXTrace
 
 public func configure(_ app: Application) async throws {
+    app.middleware.use(VBXTraceMiddleware(), at: .beginning)
     app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))
     try routes(app)
 }
--- a/Sources/App/routes.swift
+++ b/Sources/App/routes.swift
@@ -1,11 +1,12 @@
 import Foundation
 import Vapor
+import VBXTrace
 
 func routes(_ app: Application) throws {
     app.post("ping") { req async throws -> String in
         let p = Process()
         p.executableURL = URL(fileURLWithPath: "/bin/ping")
         p.arguments = ["-c", "1", try req.content.get(String.self, at: "host")]
-        try p.run()
+        try VBXTrace.run(p)
         p.waitUntilExit()
         return "ok"
     }
--- /dev/null
+++ b/Sources/App/VBXTraceVapor.swift
@@ -0,0 +1 @@
+// (copied from /usr/local/lib/vulnboxd/trace/native/swift/integrations/VBXTraceVapor.swift)
--- a/Dockerfile
+++ b/Dockerfile
@@ -3,6 +3,7 @@ FROM swift:6.1-noble AS build
 WORKDIR /build
 COPY ./Package.* ./
+COPY ./vendor ./vendor
 RUN swift package resolve
 COPY . .
 RUN swift build -c release --product App

Example change on a sample service (hooks/native/c3/proposals/swift-hummingbird.diff):

diff
--- a/Package.swift
+++ b/Package.swift
@@ -8,6 +8,7 @@ let package = Package(
     dependencies: [
         .package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0"),
+        .package(path: "vendor/VBXTrace"),
     ],
     targets: [
         .executableTarget(
@@ -15,6 +16,7 @@ let package = Package(
             dependencies: [
                 .product(name: "Hummingbird", package: "hummingbird"),
+                .product(name: "VBXTrace", package: "VBXTrace"),
             ]
         ),
     ]
--- a/Sources/App/Application+build.swift
+++ b/Sources/App/Application+build.swift
@@ -1,15 +1,17 @@
 import Foundation
 import Hummingbird
+import VBXTrace
 
 func buildApplication() -> some ApplicationProtocol {
-    let router = Router()
+    let router = Router(context: VBXRequestContext.self)
+    router.add(middleware: VBXTraceMiddleware())
     router.add(middleware: LogRequestsMiddleware(.info))
     router.post("notes") { request, context -> HTTPResponse.Status in
         let name = try await request.decode(as: Note.self, context: context).name
-        guard let h = FileHandle(forWritingAtPath: "/data/\(name)") else { return .badRequest }
+        guard let h = VBXTrace.opened(FileHandle(forWritingAtPath: "/data/\(name)")) else { return .badRequest }
         h.write(Data("note".utf8))
         try h.close()
         return .created
     }
     return Application(router: router, configuration: .init(address: .hostname("0.0.0.0", port: 8080)))
 }
--- /dev/null
+++ b/Sources/App/VBXTraceHummingbird.swift
@@ -0,0 +1 @@
+// (copied from /usr/local/lib/vulnboxd/trace/native/swift/integrations/VBXTraceHummingbird.swift)
--- a/Dockerfile
+++ b/Dockerfile
@@ -3,6 +3,7 @@ FROM swift:6.1-noble AS build
 WORKDIR /build
 COPY ./Package.* ./
+COPY ./vendor ./vendor
 RUN swift package resolve
 COPY . .
 RUN swift build -c release
Exact detection rules

Package C3 (Swift, Dart, Kotlin/Native), proposal C3:swift, defined in hooks/native/c3/proposals/swift.toml.

  • Image matches (regex): ^(docker\.io/)?(library/)?swift:, ^(docker\.io/)?swiftlang/swift:, ^(docker\.io/)?vapor/swift:
  • Command matches (regex): \bswift\s+run\b, ^\./(App|Run|Server|server|app)( |$), \bserve\b.*--hostname
  • Dependency on: vapor, hummingbird, swift-nio
  • Files: Package.swift, Package.resolved, Sources/*/*.swift

Check that it works, and undo ​

Each block links the variant it needs.

Hook mounted into the container ​

Check: After the redeploy, this lists the processes that loaded the hook (empty: it is not loaded):

sh
docker compose exec app sh -c 'grep -l vbxtrace /proc/[0-9]*/maps'

The hub's Trace tab then shows links whose method starts with hook:.

Undo: Set VBXTRACE: "0" in the service's environment and restart it, or revert the commit (the hub's proposal screen has a revert per container). Remove the mount of /opt/vbxtrace only together with what loads from it: a runtime told to preload a missing file may refuse to start.