-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
brew.sh: enforce HOMEBREW_FORCE_BREW_WRAPPER
more strictly
#20400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a7c124c
brew.sh: enforce `HOMEBREW_FORCE_BREW_WRAPPER` more strictly
carlocab e8828c1
Apply review suggestions for `pid_path.rb`
carlocab 144c7f6
Restore `HOMEBREW_BREW_WRAPPER` and `HOMEBREW_FORCE_BREW_WRAPPER`
carlocab b7d8072
Restore handling of `HOMEBREW_BREW_WRAPPER`
carlocab b2d14af
Add custom implementations for `brew_wrapper` and `no_force_brew_wrap…
carlocab a4ea970
Improve error handling when determining parent process path
carlocab d9c661a
Fix `brew style` errors
carlocab ead3af9
Deduplicate `odie` calls into `utils/wrapper.sh`
carlocab 83d8a41
Move all wrapper checks to utils/wrapper.sh
carlocab 145c65d
Merge remote-tracking branch 'origin/main' into stricter-brew-wrappers
carlocab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/usr/bin/env ruby | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
pid = ARGV[0]&.to_i | ||
raise "Missing `pid` argument!" unless pid | ||
|
||
require "fiddle" | ||
|
||
libproc = Fiddle.dlopen("/usr/lib/libproc.dylib") | ||
|
||
libproc_proc_pidpath_function = Fiddle::Function.new( | ||
libproc["proc_pidpath"], | ||
[Fiddle::TYPE_INT, Fiddle::TYPE_VOIDP, Fiddle::TYPE_UINT32_T], | ||
Fiddle::TYPE_INT, | ||
) | ||
|
||
# We have to allocate a (char) buffer of exactly `PROC_PIDPATHINFO_MAXSIZE` to use `proc_pidpath` | ||
# From `include/sys/proc_info.h`, PROC_PIDPATHINFO_MAXSIZE = 4 * MAXPATHLEN | ||
# From `include/sys/param.h`, MAXPATHLEN = PATH_MAX | ||
# From `include/sys/syslimits.h`, PATH_MAX = 1024 | ||
# https://github.com/apple-oss-distributions/xnu/blob/e3723e1f17661b24996789d8afc084c0c3303b26/libsyscall/wrappers/libproc/libproc.c#L268-L275 | ||
buffer_size = 4 * 1024 # PROC_PIDPATHINFO_MAXSIZE = 4 * MAXPATHLEN | ||
buffer = "\0" * buffer_size | ||
pointer_to_buffer = Fiddle::Pointer.to_ptr(buffer) | ||
|
||
# `proc_pidpath` returns a positive value on success. See: | ||
# https://stackoverflow.com/a/8149198 | ||
# https://github.com/chromium/chromium/blob/86df41504a235f9369f6f53887da12a718a19db4/base/process/process_handle_mac.cc#L37-L44 | ||
# https://github.com/apple-oss-distributions/xnu/blob/e3723e1f17661b24996789d8afc084c0c3303b26/libsyscall/wrappers/libproc/libproc.c#L263-L283 | ||
return_value = libproc_proc_pidpath_function.call(pid, pointer_to_buffer, buffer_size) | ||
raise "Call to `proc_pidpath` failed! `proc_pidpath` returned #{return_value}." unless return_value.positive? | ||
|
||
puts pointer_to_buffer.to_s.strip |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# `brew` wrapper handling helpers. | ||
|
||
# HOMEBREW_LIBRARY, HOMEBREW_BREW_FILE, HOMEBREW_ORIGINAL_BREW_FILE, HOMEBREW_PREFIX are set by bin/brew. | ||
# HOMEBREW_FORCE_BREW_WRAPPER is set by the user environment. | ||
# shellcheck disable=SC2154 | ||
odie-with-wrapper-message() { | ||
source "${HOMEBREW_LIBRARY}/Homebrew/utils/helpers.sh" | ||
|
||
local CUSTOM_MESSAGE="${1}" | ||
local HOMEBREW_FORCE_BREW_WRAPPER_WITHOUT_BREW="${HOMEBREW_FORCE_BREW_WRAPPER%/brew}" | ||
|
||
odie <<EOS | ||
conflicting Homebrew wrapper configuration! | ||
HOMEBREW_FORCE_BREW_WRAPPER was set to ${HOMEBREW_FORCE_BREW_WRAPPER} | ||
${CUSTOM_MESSAGE} | ||
|
||
$(bold "Ensure you run ${HOMEBREW_FORCE_BREW_WRAPPER} directly (not ${HOMEBREW_ORIGINAL_BREW_FILE})")! | ||
|
||
Manually setting your PATH can interfere with Homebrew wrappers. | ||
Ensure your shell configuration contains: | ||
eval "\$(${HOMEBREW_BREW_FILE} shellenv)" | ||
or that ${HOMEBREW_FORCE_BREW_WRAPPER_WITHOUT_BREW} comes before ${HOMEBREW_PREFIX}/bin in your PATH: | ||
export PATH="${HOMEBREW_FORCE_BREW_WRAPPER_WITHOUT_BREW}:${HOMEBREW_PREFIX}/bin:\$PATH" | ||
EOS | ||
} | ||
|
||
check-brew-wrapper() { | ||
[[ -z "${HOMEBREW_FORCE_BREW_WRAPPER:-}" ]] && return | ||
[[ -z "${HOMEBREW_DISABLE_NO_FORCE_BREW_WRAPPER:-}" && -n "${HOMEBREW_NO_FORCE_BREW_WRAPPER:-}" ]] && return | ||
|
||
# Require HOMEBREW_BREW_WRAPPER to be set if HOMEBREW_FORCE_BREW_WRAPPER is set | ||
# (and HOMEBREW_NO_FORCE_BREW_WRAPPER and HOMEBREW_DISABLE_NO_FORCE_BREW_WRAPPER are not set). | ||
if [[ -z "${HOMEBREW_DISABLE_NO_FORCE_BREW_WRAPPER:-}" && -z "${HOMEBREW_NO_FORCE_BREW_WRAPPER:-}" ]] | ||
then | ||
if [[ -z "${HOMEBREW_BREW_WRAPPER:-}" ]] | ||
then | ||
odie-with-wrapper-message "but HOMEBREW_BREW_WRAPPER was unset." | ||
elif [[ "${HOMEBREW_FORCE_BREW_WRAPPER}" != "${HOMEBREW_BREW_WRAPPER}" ]] | ||
then | ||
odie-with-wrapper-message "but HOMEBREW_BREW_WRAPPER was set to ${HOMEBREW_BREW_WRAPPER}" | ||
fi | ||
|
||
return | ||
fi | ||
|
||
# If HOMEBREW_FORCE_BREW_WRAPPER and HOMEBREW_DISABLE_NO_FORCE_BREW_WRAPPER are set, | ||
# verify that the path to our parent process is the same as the value of HOMEBREW_FORCE_BREW_WRAPPER, | ||
if [[ -n "${HOMEBREW_DISABLE_NO_FORCE_BREW_WRAPPER:-}" ]] | ||
then | ||
local HOMEBREW_BREW_CALLER HOMEBREW_BREW_CALLER_CHECK_EXIT_CODE | ||
|
||
if [[ -n "${HOMEBREW_MACOS:-}" ]] | ||
then | ||
source "${HOMEBREW_LIBRARY}/Homebrew/utils/ruby.sh" | ||
setup-ruby-path | ||
HOMEBREW_BREW_CALLER="$("${HOMEBREW_RUBY_PATH}" "${HOMEBREW_LIBRARY}/Homebrew/utils/pid_path.rb" "${PPID}")" | ||
else | ||
HOMEBREW_BREW_CALLER="$(readlink -f "/proc/${PPID}/exe")" | ||
fi | ||
HOMEBREW_BREW_CALLER_CHECK_EXIT_CODE="$?" | ||
|
||
if ((HOMEBREW_BREW_CALLER_CHECK_EXIT_CODE != 0)) | ||
then | ||
# Error message already printed above when populating `HOMEBREW_BREW_CALLER`. | ||
odie "failed to check the path to the parent process!" | ||
fi | ||
|
||
if [[ "${HOMEBREW_BREW_CALLER:-}" != "${HOMEBREW_FORCE_BREW_WRAPPER}" ]] | ||
then | ||
source "${HOMEBREW_LIBRARY}/Homebrew/utils/wrapper.sh" | ||
odie-with-wrapper-message "but \`brew\` was invoked by ${HOMEBREW_BREW_CALLER}." | ||
fi | ||
fi | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.