Skip to content

Commit

Permalink
Fix unbundled environment.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Sep 4, 2024
1 parent b1d9c42 commit 4c6d321
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions lib/falcon/service/virtual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,28 @@ module Service
class Virtual < Async::Service::Generic
# Drop privileges according to the user and group of the specified path.
# @parameter path [String] The path to the application directory.
# @returns [Hash] The environment to use for the spawned process.
def assume_privileges(path)
# Process.exec / Process.spawn don't replace the environment but instead update it, so we need to clear out any existing BUNDLE_ variables using `nil` values, which will cause them to be removed from the child environment:
env = ENV.to_h do |key, value|
if key.start_with?('BUNDLE_')
[key, nil]
else
[key, value]
end
end

env['PWD'] = File.dirname(path)

stat = File.stat(path)

Process::GID.change_privilege(stat.gid)
Process::UID.change_privilege(stat.uid)

home = Etc.getpwuid(stat.uid).dir
env['HOME'] = home

return {
'HOME' => home,
}
return env
end

# Spawn an application instance from the specified path.
Expand All @@ -35,9 +46,7 @@ def spawn(path, container, **options)
container.spawn(name: "Falcon Application", restart: true, key: path) do |instance|
env = assume_privileges(path)

instance.exec(env,
"bundle", "exec", "--keep-file-descriptors",
path, ready: false, **options)
instance.exec(env, "bundle", "exec", path, ready: false, **options)
end
end

Expand Down

0 comments on commit 4c6d321

Please sign in to comment.