Sha256: 2e6e9f400933c78cef3c67fc66d5b0bd6a7c0a31ecf9b31361e94cfe9fe249bf

Contents?: true

Size: 1.9 KB

Versions: 72

Compression:

Stored size: 1.9 KB

Contents

#
# Author:: Seth Chisamore (<schisamo@chef.io>)
# Copyright:: Copyright 2011-2017, Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

class Chef
  module Mixin
    module PathSanity

      def enforce_path_sanity(env = ENV)
        if Chef::Config[:enforce_path_sanity]
          env["PATH"] = sanitized_path(env)
        end
      end

      def sanitized_path(env = ENV)
        env_path = env["PATH"].nil? ? "" : env["PATH"].dup
        path_separator = Chef::Platform.windows? ? ";" : ":"
        # ensure the Ruby and Gem bindirs are included
        # mainly for 'full-stack' Chef installs
        new_paths = env_path.split(path_separator)
        [ ruby_bindir, gem_bindir ].compact.each do |path|
          new_paths = [ path ] + new_paths unless new_paths.include?(path)
        end
        sane_paths.each do |path|
          new_paths << path unless new_paths.include?(path)
        end
        new_paths.join(path_separator).encode("utf-8", invalid: :replace, undef: :replace)
      end

      private

      def sane_paths
        @sane_paths ||= begin
          if Chef::Platform.windows?
            %w{}
          else
            %w{/usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin}
          end
        end
      end

      def ruby_bindir
        RbConfig::CONFIG["bindir"]
      end

      def gem_bindir
        Gem.bindir
      end

    end
  end
end

Version data entries

72 entries across 72 versions & 1 rubygems

Version Path
chef-14.15.6 lib/chef/mixin/path_sanity.rb
chef-14.15.6-universal-mingw32 lib/chef/mixin/path_sanity.rb
chef-14.14.29 lib/chef/mixin/path_sanity.rb
chef-14.14.29-universal-mingw32 lib/chef/mixin/path_sanity.rb
chef-15.4.45 lib/chef/mixin/path_sanity.rb
chef-15.4.45-universal-mingw32 lib/chef/mixin/path_sanity.rb
chef-14.14.25-universal-mingw32 lib/chef/mixin/path_sanity.rb
chef-14.14.25 lib/chef/mixin/path_sanity.rb
chef-14.14.14-universal-mingw32 lib/chef/mixin/path_sanity.rb
chef-14.14.14 lib/chef/mixin/path_sanity.rb
chef-15.3.14 lib/chef/mixin/path_sanity.rb
chef-15.3.14-universal-mingw32 lib/chef/mixin/path_sanity.rb
chef-15.2.20 lib/chef/mixin/path_sanity.rb
chef-15.2.20-universal-mingw32 lib/chef/mixin/path_sanity.rb
chef-15.1.36 lib/chef/mixin/path_sanity.rb
chef-15.1.36-universal-mingw32 lib/chef/mixin/path_sanity.rb
chef-14.13.11 lib/chef/mixin/path_sanity.rb
chef-15.0.300 lib/chef/mixin/path_sanity.rb
chef-15.0.298 lib/chef/mixin/path_sanity.rb
chef-15.0.293 lib/chef/mixin/path_sanity.rb