Module: Cask::Quarantine Private

Extended by:
SystemCommand::Mixin
Defined in:
cask/quarantine.rb

Overview

This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.

Helper module for quarantining files.

Constant Summary collapse

QUARANTINE_ATTRIBUTE =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

"com.apple.quarantine"
QUARANTINE_SCRIPT =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

(HOMEBREW_LIBRARY_PATH/"cask/utils/quarantine.swift").freeze
COPY_XATTRS_SCRIPT =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

(HOMEBREW_LIBRARY_PATH/"cask/utils/copy-xattrs.swift").freeze

Class Method Summary collapse

Methods included from SystemCommand::Mixin

system_command, system_command!

Class Method Details

.app_management_permissions_granted?(app:, command:) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Ensures that Homebrew has permission to update apps on macOS Ventura. This may be granted either through the App Management toggle or the Full Disk Access toggle. The system will only show a prompt for App Management, so we ask the user to grant that.

Parameters:

Returns:

  • (Boolean)


208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'cask/quarantine.rb', line 208

def self.app_management_permissions_granted?(app:, command:)
  return true unless app.directory?

  # To get macOS to prompt the user for permissions, we need to actually attempt to
  # modify a file in the app.
  test_file = app/".homebrew-write-test"

  # We can't use app.writable? here because that conflates several access checks,
  # including both file ownership and whether system permissions are granted.
  # Here we just want to check whether sudo would be needed.
  looks_writable_without_sudo = if app.owned?
    app.lstat.mode.anybits?(0200)
  elsif app.grpowned?
    app.lstat.mode.anybits?(0020)
  else
    app.lstat.mode.anybits?(0002)
  end

  if looks_writable_without_sudo
    begin
      File.write(test_file, "")
      test_file.delete
      return true
    rescue Errno::EACCES, Errno::EPERM
      # Using error handler below
    end
  else
    begin
      command.run!(
        "touch",
        args:         [
          test_file,
        ],
        print_stderr: false,
        sudo:         true,
      )
      command.run!(
        "rm",
        args:         [
          test_file,
        ],
        print_stderr: false,
        sudo:         true,
      )
      return true
    rescue ErrorDuringExecution => e
      # We only want to handle "touch" errors here; propagate "sudo" errors up
      raise e unless e.stderr.include?("touch: #{test_file}: Operation not permitted")
    end
  end

  opoo <<~EOF
    Your terminal does not have App Management permissions, so Homebrew will delete and reinstall the app.
    This may result in some configurations (like notification settings or location in the Dock/Launchpad) being lost.
    To fix this, go to System Settings > Privacy & Security > App Management and add or enable your terminal.
  EOF

  false
end

.available?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


69
70
71
72
73
# File 'cask/quarantine.rb', line 69

def self.available?
  @status ||= check_quarantine_support

  @status == :quarantine_available
end

.cask!(cask: nil, download_path: nil, action: true) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'cask/quarantine.rb', line 123

def self.cask!(cask: nil, download_path: nil, action: true)
  return if cask.nil? || download_path.nil?

  return if detect(download_path)

  odebug "Quarantining #{download_path}"

  quarantiner = system_command(swift,
                               args:         [
                                 *swift_target_args,
                                 QUARANTINE_SCRIPT,
                                 download_path,
                                 cask.url.to_s,
                                 cask.homepage.to_s,
                               ],
                               print_stderr: false)

  return if quarantiner.success?

  case quarantiner.exit_status
  when 2
    raise CaskQuarantineError.new(download_path, "Insufficient parameters")
  else
    raise CaskQuarantineError.new(download_path, quarantiner.stderr)
  end
end

.check_quarantine_supportSymbol

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'cask/quarantine.rb', line 44

def self.check_quarantine_support
  odebug "Checking quarantine support"

  if xattr.nil? || !system_command(xattr, args: ["-h"], print_stderr: false).success?
    odebug "There's no working version of `xattr` on this system."
    :xattr_broken
  elsif swift.nil?
    odebug "Swift is not available on this system."
    :no_swift
  else
    api_check = system_command(swift,
                               args:         [*swift_target_args, QUARANTINE_SCRIPT],
                               print_stderr: false)

    case api_check.exit_status
    when 2
      odebug "Quarantine is available."
      :quarantine_available
    else
      odebug "Unknown support status"
      :unknown
    end
  end
end

.copy_xattrs(from, to, command:) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'cask/quarantine.rb', line 189

def self.copy_xattrs(from, to, command:)
  odebug "Copying xattrs from #{from} to #{to}"

  command.run!(
    swift,
    args: [
      *swift_target_args,
      COPY_XATTRS_SCRIPT,
      from,
      to,
    ],
    sudo: !to.writable?,
  )
end

.detect(file) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



75
76
77
78
79
80
81
82
83
84
85
# File 'cask/quarantine.rb', line 75

def self.detect(file)
  return if file.nil?

  odebug "Verifying Gatekeeper status of #{file}"

  quarantine_status = !status(file).empty?

  odebug "#{file} is #{quarantine_status ? "quarantined" : "not quarantined"}"

  quarantine_status
end

.propagate(from: nil, to: nil) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Raises:



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'cask/quarantine.rb', line 150

def self.propagate(from: nil, to: nil)
  return if from.nil? || to.nil?

  raise CaskError, "#{from} was not quarantined properly." unless detect(from)

  odebug "Propagating quarantine from #{from} to #{to}"

  quarantine_status = toggle_no_translocation_bit(status(from))

  resolved_paths = Pathname.glob(to/"**/*", File::FNM_DOTMATCH).reject(&:symlink?)

  system_command!("/usr/bin/xargs",
                  args:  [
                    "-0",
                    "--",
                    "/bin/chmod",
                    "-h",
                    "u+w",
                  ],
                  input: resolved_paths.join("\0"))

  quarantiner = system_command("/usr/bin/xargs",
                               args:         [
                                 "-0",
                                 "--",
                                 xattr,
                                 "-w",
                                 QUARANTINE_ATTRIBUTE,
                                 quarantine_status,
                               ],
                               input:        resolved_paths.join("\0"),
                               print_stderr: false)

  return if quarantiner.success?

  raise CaskQuarantinePropagationError.new(to, quarantiner.stderr)
end

.release!(download_path: nil) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'cask/quarantine.rb', line 105

def self.release!(download_path: nil)
  return unless detect(download_path)

  odebug "Releasing #{download_path} from quarantine"

  quarantiner = system_command(xattr,
                               args:         [
                                 "-d",
                                 QUARANTINE_ATTRIBUTE,
                                 download_path,
                               ],
                               print_stderr: false)

  return if quarantiner.success?

  raise CaskQuarantineReleaseError.new(download_path, quarantiner.stderr)
end

.status(file) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



87
88
89
90
91
# File 'cask/quarantine.rb', line 87

def self.status(file)
  system_command(xattr,
                 args:         ["-p", QUARANTINE_ATTRIBUTE, file],
                 print_stderr: false).stdout.rstrip
end

.toggle_no_translocation_bit(attribute) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



93
94
95
96
97
98
99
100
101
102
103
# File 'cask/quarantine.rb', line 93

def self.toggle_no_translocation_bit(attribute)
  fields = attribute.split(";")

  # Fields: status, epoch, download agent, event ID
  # Let's toggle the app translocation bit, bit 8
  # http://www.openradar.me/radar?id=5022734169931776

  fields[0] = (fields[0].to_i(16) | 0x0100).to_s(16).rjust(4, "0")

  fields.join(";")
end