Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Sources/SPGlassRegistry/RegistryPath.swift8// Module : SPGlassRegistry9// Repository : https://git.spboucher.ai/sp-glasskit10// Project Website : https://glass.spboucher.ai11// Created : 2026-12-0212// Last Modified : 2026-12-0213//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : MIT33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation4243/// Rules for the relative paths found in manifests. A path that passes can be joined to a destination44/// directory without leaving it.45public enum RegistryPath {46 public static let maximumLength = 24047 public static let maximumDepth = 124849 /// Validates a manifest path: relative, forward slashes, no `.`/`..`/empty component, portable characters.50 public static func validate(_ path: String) throws(RegistryError) {51 func fail(_ reason: String) throws(RegistryError) -> Never { throw .unsafePath(path, reason: reason) }52 if path.isEmpty { try fail("it is empty") }53 if path.utf8.count > maximumLength { try fail("it is longer than \(maximumLength) bytes") }54 if path.hasPrefix("/") || path.hasPrefix("~") { try fail("it is absolute") }55 if path.contains("\\") { try fail("it contains a backslash") }56 if path.unicodeScalars.contains(where: { $0.value < 0x20 || $0.value == 0x7F }) { try fail("it contains a control character") }57 if path.contains(":") { try fail("it contains a colon") }58 let components = path.split(separator: "/", omittingEmptySubsequences: false)59 if components.count > maximumDepth { try fail("it is nested more than \(maximumDepth) levels") }60 for component in components {61 if component.isEmpty { try fail("it has an empty component") }62 if component == "." || component == ".." { try fail("it contains \"\(component)\"") }63 if component.hasPrefix(".") { try fail("a component starts with a dot") }64 if component.hasSuffix(".") || component.hasSuffix(" ") || component.hasPrefix(" ") { try fail("a component starts or ends with a space or dot") }65 let allowed = component.unicodeScalars.allSatisfy { scalar in66 (scalar.isASCII && (scalar.properties.isAlphabetic || ("0"..."9").contains(scalar))) || "+-_. ".unicodeScalars.contains(scalar)67 }68 if !allowed { try fail("a component uses characters outside A-Z a-z 0-9 + - _ . and space") }69 }70 }7172 /// The key used to detect collisions on case-insensitive file systems.73 public static func collisionKey(_ path: String) -> String {74 path.lowercased()75 }7677 /// Validates a set of paths together: each must be safe, and no two may collide, nor may one be a78 /// directory prefix of another (a file cannot also be a directory).79 public static func validateSet(_ paths: [String]) throws(RegistryError) {80 var seen: [String: String] = [:]81 for path in paths {82 try validate(path)83 let key = collisionKey(path)84 if let other = seen[key] { throw .pathCollision(other, path) }85 seen[key] = path86 }87 for path in paths {88 var prefix = ""89 for component in path.split(separator: "/").dropLast() {90 prefix += (prefix.isEmpty ? "" : "/") + component91 if let other = seen[collisionKey(prefix)] { throw .pathCollision(other, path) }92 }93 }94 }9596 public static func isValidIdentifier(_ id: String) -> Bool {97 guard (1...64).contains(id.utf8.count), !id.hasPrefix("-"), !id.hasSuffix("-"), !id.contains("--") else { return false }98 return id.utf8.allSatisfy { ($0 >= 97 && $0 <= 122) || ($0 >= 48 && $0 <= 57) || $0 == 45 }99 }100101 public static func validateIdentifier(_ id: String) throws(RegistryError) {102 guard isValidIdentifier(id) else { throw .invalidIdentifier(id) }103 }104}105