;;; Copyright (c) 2024 Apple Inc.  All Rights reserved.
;;;
;;; WARNING: The sandbox rules in this file currently constitute
;;; Apple System Private Interface and are subject to change at any time and
;;; without notice.
;;;
(version 1)

;;; TODO: Change these to deny before finalizing this profile.
(allow (with report) default)
(allow (with report) file-map-executable process-info* nvram*)
(allow (with report) dynamic-code-generation)

(deny mach-priv-host-port)

(import "system.sb")
(import "com.apple.corefoundation.sb")
(corefoundation)

;;; Homedir-relative path filters
(define (home-regex home-relative-regex)
    (regex (string-append "^" (regex-quote (param "HOME")) home-relative-regex)))

(define (home-subpath home-relative-subpath)
    (subpath (string-append (param "HOME") home-relative-subpath)))

(define (home-prefix home-relative-prefix)
    (prefix (string-append (param "HOME") home-relative-prefix)))

(define (home-literal home-relative-literal)
    (literal (string-append (param "HOME") home-relative-literal)))


(allow process-info* (target self))

;; For resolving symlinks, realpath(3), and equivalents.
(allow file-read-metadata)

;; For validating the entitlements of clients.
(allow process-info-codesignature)

(allow file-read* (extension "com.apple.app-sandbox.read"))
(allow file-read* (literal "/AppleInternal/Library/Audio/Plug-Ins/Components/CoreAudioInternal.component"))
(allow file-read* (literal "/AppleInternal/Library/Audio/Plug-Ins/Components/CoreAudioInternal.component/Contents"))
(allow file-read* (literal "/AppleInternal/Library/Audio/Plug-Ins/Components/CoreAudioInternal.component/Contents/Info.plist"))

;; Your preference domain
(allow user-preference-read user-preference-write
       (preference-domain "com.apple.accessibility.AccessibilityReader")
       (preference-domain "com.apple.Accessibility")
       (preference-domain "com.apple.HIToolbox")
       (preference-domain "com.apple.SpeakSelection")
       (preference-domain "com.apple.universalaccess"))

(allow user-preference-read
    (preference-domain "kCFPreferencesAnyApplication"))
    
;; Mach lookup
;; TODO: this is missing but it will cause speech to crash
;;(global-name "com.apple.audio.audiohald")

(allow mach-lookup
    (global-name "com.apple.audio.AudioComponentRegistrar")
    (global-name "com.apple.audio.AudioComponentRegistrar.daemon")
    (global-name "com.apple.audio.audiohald")
    (global-name "com.apple.audio.AudioSession")
    (global-name "com.apple.axserver")
    (global-name "com.apple.audio.coreaudiod")
    (global-name "com.apple.cmio.registerassistantservice.system-extensions")
    (global-name "com.apple.coreservices.quarantine-resolver")
    (global-name "com.apple.corespeech.corespeechd.xpc")
    (global-name "com.apple.corespeech.xpc")
    (global-name "com.apple.dock.fullscreen")
    (global-name "com.apple.iconservices")
    (global-name "com.apple.iconservices.store")
    (global-name "com.apple.pluginkit.pkd")
    (global-name "com.apple.tccd")
    (global-name "com.apple.tccd.system")
    (global-name "com.apple.tsm.uiserver")
    (global-name "com.apple.window_proxies")
    (global-name "com.apple.windowmanager.server")
    (global-name "com.apple.windowserver.active"))

(allow mach-register
    (global-name "com.apple.audio.coreaudiod")
    (global-name "com.apple.audio.audiohald"))

;; com.apple.axserver (per-pid)
(allow mach-lookup (local-name-prefix "com.apple.axserver"))

;; IOKit user clients
(allow iokit-open
    (iokit-user-client-class "IOHIDParamUserClient"))

;; Read/write access to a temporary directory.
(allow file-read* file-write*
       (subpath (param "TMPDIR"))
       (subpath (param "DARWIN_CACHE_DIR")))

;; Read/write cache access
(let ((cache-path-filter (home-subpath "/Library/Caches/com.apple.accessibility.AccessibilityReader")))
  (allow file-read* file-write* cache-path-filter)
  (allow file-issue-extension
    (require-all
      (extension-class "com.apple.app-sandbox.read" "com.apple.app-sandbox.read-write")
      cache-path-filter)))

(allow file-issue-extension
    (require-all
        (extension-class "com.apple.app-sandbox.read-write")
        (subpath "/")))
        
 (allow file-read* file-write*
    (home-subpath "/Library/Keyboard Layouts")
    (home-subpath "/Library/Input Methods")
    (home-subpath "/Library/Application Support/CrashReporter")
    (home-subpath "/Library/Saved Application State")
    (home-subpath "/Library/Saved Application State/com.apple.AccessibilityReader.savedState"))
    
(allow file-map-executable
    (subpath "/System/Library/Accessibility/")
    (subpath "/System/Library/Components/")
    (subpath "/System/Library/Extensions/"))

(allow mach-task-name)


