SPB Git

Repository Public

PathGuard.swift 3.1 KB 61 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Core/Security/PathGuard.swift3// Description: Confines every deletion and move to folders the app created and marked; safe file names.4import Foundation56/// Every deletion or move the app performs on the listener's disk goes through here: a path is touched only when it7/// is provably inside a folder the app itself created and marked.8public enum PathGuard {9    public static let markerDirectory = ".spmusic"10    public static let markerFile = "library.json"1112    /// The real location of a path: `..` removed and every symbolic link followed, including when the last13    /// components do not exist yet (the deepest existing ancestor is resolved, the rest is appended).14    public static func resolved(_ url: URL) -> URL {15        var existing = url.standardizedFileURL16        var tail: [String] = []17        while existing.pathComponents.count > 1, !FileManager.default.fileExists(atPath: existing.path) {18            tail.insert(existing.lastPathComponent, at: 0)19            existing = existing.deletingLastPathComponent()20        }21        var real = existing22        if let p = realpath(existing.path, nil) { real = URL(fileURLWithPath: String(cString: p)); free(p) }23        return tail.reduce(real) { $0.appendingPathComponent($1) }24    }2526    /// True when `url` is strictly inside `root` (component-wise, after resolving `..` and symbolic links).27    public static func isInside(_ url: URL, root: URL) -> Bool {28        let a = resolved(url).pathComponents, r = resolved(root).pathComponents29        return a.count > r.count && Array(a.prefix(r.count)) == r30    }3132    public static func hasMarker(_ root: URL) -> Bool {33        FileManager.default.fileExists(atPath: root.appendingPathComponent(markerDirectory).appendingPathComponent(markerFile).path)34    }3536    /// A file may be deleted only inside a marked library folder.37    public static func mayDelete(_ url: URL, root: URL) -> Bool { hasMarker(root) && isInside(url, root: root) }3839    /// One path component made safe for the file system, still readable by a human.40    public static func safeComponent(_ raw: String, fallback: String, maxLength: Int = 110) -> String {41        var s = raw.precomposedStringWithCanonicalMapping42        s = String(s.map { c -> Character in43            if c == "/" || c == ":" || c == "\\" { return "-" }44            if c.unicodeScalars.contains(where: { $0.value < 0x20 || $0.value == 0x7f }) { return " " }45            return c46        })47        s = s.trimmingCharacters(in: .whitespacesAndNewlines)48        while s.hasPrefix(".") { s.removeFirst() }49        while s.hasSuffix(".") || s.hasSuffix(" ") { s.removeLast() }50        if s.count > maxLength { s = String(s.prefix(maxLength)).trimmingCharacters(in: .whitespaces) }51        return s.isEmpty ? fallback : s52    }53}5455public enum Log {56    /// Diagnostic line on stderr. Callers never pass tokens, cookies or request bodies.57    public static func info(_ area: String, _ message: @autoclosure () -> String) {58        FileHandle.standardError.write(Data("[\(area)] \(message())\n".utf8))59    }60}61
Files main
Menu

Theme