CVE-2012-3864: Exposure of Sensitive Information to an Unauthorized Actor (CWE-200)
value: Puppet before 2.6.17 and 2.7.x before 2.7.18, and Puppet Enterprise before 2.5.2, allows remote authenticated users to read arbitrary files on the puppet master server by leveraging an arbitrary user's certificate and private key in a GET request.
CWE description: The product exposes sensitive information to an actor that is not explicitly authorized to have access to that information.
Published: 2012-08-06T16:55Z
Repository: https://github.com/puppetlabs/puppet
Language: Ruby
Files changed: lib/puppet/application/master.rb, lib/puppet/file_serving/configuration.rb, lib/puppet/file_serving/content.rb, lib/puppet/file_serving/metadata.rb, lib/puppet/file_serving/terminus_selector.rb, lib/puppet/indirector/file_content/selector.rb, lib/puppet/indirector/file_metadata/selector.rb, spec/shared_behaviours/file_serving.rb, spec/unit/file_serving/content_spec.rb, spec/unit/file_serving/metadata_spec.rb, spec/unit/file_serving/terminus_selector_spec.rb
Commit date: 2012-06-26 17:11:57 -0700
Commit message: Add Selector terminus for file_content/file_metadata

This terminus is now the default, and encapsulates the behavior that was
previously in the IndirectionHooks. That is, we dynamically select the
terminus to use for a file request based on the key. However, for the
puppet master, we instead explicitly always use the FileServer terminus,
so that *all* requests for files from the master will go through
the file server. This ensures that we will never accidentally
serve local files on the puppet master.

Diff: diff --git a/lib/puppet/application/master.rb b/lib/puppet/application/master.rb
index b4da770f0..62d9f5578 100644
--- a/lib/puppet/application/master.rb
+++ b/lib/puppet/application/master.rb
@@ -229,10 +229,13 @@ Copyright (c) 2011 Puppet Labs, LLC Licensed under the Apache 2.0 License
     Puppet.settings.use :main, :master, :ssl, :metrics
 
     # Cache our nodes in yaml.  Currently not configurable.
     Puppet::Node.indirection.cache_class = :yaml
 
+    Puppet::FileServing::Content.indirection.terminus_class = :file_server
+    Puppet::FileServing::Metadata.indirection.terminus_class = :file_server
+
     # Configure all of the SSL stuff.
     if Puppet::SSL::CertificateAuthority.ca?
       Puppet::SSL::Host.ca_location = :local
       Puppet.settings.use :ca
       Puppet::SSL::CertificateAuthority.instance
diff --git a/lib/puppet/file_serving/configuration.rb b/lib/puppet/file_serving/configuration.rb
index 02bca1bea..fb8aff7a8 100644
--- a/lib/puppet/file_serving/configuration.rb
+++ b/lib/puppet/file_serving/configuration.rb
@@ -62,11 +62,11 @@ class Puppet::FileServing::Configuration
     # Reparse the configuration if necessary.
     readconfig
 
     mount_name, path = request.key.split(File::Separator, 2)
 
-    raise(ArgumentError, "Cannot find file: Invalid path '#{mount_name}'") unless mount_name =~ %r{^[-\w]+$}
+    raise(ArgumentError, "Cannot find file: Invalid mount '#{mount_name}'") unless mount_name =~ %r{^[-\w]+$}
 
     return nil unless mount = find_mount(mount_name, request.environment)
     if mount.name == "modules" and mount_name != "modules"
       # yay backward-compatibility
       path = "#{mount_name}/#{path}"
diff --git a/lib/puppet/file_serving/content.rb b/lib/puppet/file_serving/content.rb
index 689e45af4..a6706bfcc 100644
--- a/lib/puppet/file_serving/content.rb
+++ b/lib/puppet/file_serving/content.rb
@@ -1,16 +1,15 @@
 require 'puppet/indirector'
 require 'puppet/file_serving'
 require 'puppet/file_serving/base'
-require 'puppet/file_serving/indirection_hooks'
 
 # A class that handles retrieving file contents.
 # It only reads the file when its content is specifically
 # asked for.
 class Puppet::FileServing::Content < Puppet::FileServing::Base
   extend Puppet::Indirector
-  indirects :file_content, :extend => Puppet::FileServing::IndirectionHooks
+  indirects :file_content, :terminus_class => :selector
 
   attr_writer :content
 
   def self.supported_formats
     [:raw]
diff --git a/lib/puppet/file_serving/metadata.rb b/lib/puppet/file_serving/metadata.rb
index 587c2196d..9756503fb 100644
--- a/lib/puppet/file_serving/metadata.rb
+++ b/lib/puppet/file_serving/metadata.rb
@@ -1,19 +1,18 @@
 require 'puppet'
 require 'puppet/indirector'
 require 'puppet/file_serving'
 require 'puppet/file_serving/base'
 require 'puppet/util/checksums'
-require 'puppet/file_serving/indirection_hooks'
 
 # A class that handles retrieving file metadata.
 class Puppet::FileServing::Metadata < Puppet::FileServing::Base
 
   include Puppet::Util::Checksums
 
   extend Puppet::Indirector
-  indirects :file_metadata, :extend => Puppet::FileServing::IndirectionHooks
+  indirects :file_metadata, :terminus_class => :selector
 
   attr_reader :path, :owner, :group, :mode, :checksum_type, :checksum, :ftype, :destination
 
   PARAM_ORDER = [:mode, :ftype, :owner, :group]
 
diff --git a/lib/puppet/file_serving/indirection_hooks.rb b/lib/puppet/file_serving/terminus_selector.rb
similarity index 86%
rename from lib/puppet/file_serving/indirection_hooks.rb
rename to lib/puppet/file_serving/terminus_selector.rb
index 4aafcb9fd..8bafc5bd9 100644
--- a/lib/puppet/file_serving/indirection_hooks.rb
+++ b/lib/puppet/file_serving/terminus_selector.rb
@@ -1,17 +1,14 @@
-require 'uri'
 require 'puppet/file_serving'
-require 'puppet/util'
 
 # This module is used to pick the appropriate terminus
 # in file-serving indirections.  This is necessary because
 # the terminus varies based on the URI asked for.
-module Puppet::FileServing::IndirectionHooks
+module Puppet::FileServing::TerminusSelector
   PROTOCOL_MAP = {"puppet" => :rest, "file" => :file}
 
-  # Pick an appropriate terminus based on the protocol.
-  def select_terminus(request)
+  def select(request)
     # We rely on the request's parsing of the URI.
 
     # Short-circuit to :file if it's a fully-qualified path or specifies a 'file' protocol.
     return PROTOCOL_MAP["file"] if Puppet::Util.absolute_path?(request.key)
     return PROTOCOL_MAP["file"] if request.protocol == "file"
diff --git a/lib/puppet/indirector/file_content/selector.rb b/lib/puppet/indirector/file_content/selector.rb
new file mode 100644
index 000000000..7aac4ee68
--- /dev/null
+++ b/lib/puppet/indirector/file_content/selector.rb
@@ -0,0 +1,29 @@
+require 'puppet/indirector/file_content'
+require 'puppet/indirector/code'
+require 'puppet/file_serving/terminus_selector'
+
+class Puppet::Indirector::FileContent::Selector < Puppet::Indirector::Code
+  desc "Select the terminus based on the request"
+  include Puppet::FileServing::TerminusSelector
+
+  def get_terminus(request)
+    indirection.terminus(select(request))
+  end
+
+  def find(request)
+    get_terminus(request).find(request)
+  end
+
+  def search(request)
+    get_terminus(request).search(request)
+  end
+
+  def authorized?(request)
+    terminus = get_terminus(request)
+    if terminus.respond_to?(:authorized?)
+      terminus.authorized?(request)
+    else
+      true
+    end
+  end
+end
diff --git a/lib/puppet/indirector/file_metadata/selector.rb b/lib/puppet/indirector/file_metadata/selector.rb
new file mode 100644
index 000000000..26793b5c4
--- /dev/null
+++ b/lib/puppet/indirector/file_metadata/selector.rb
@@ -0,0 +1,29 @@
+require 'puppet/indirector/file_metadata'
+require 'puppet/indirector/code'
+require 'puppet/file_serving/terminus_selector'
+
+class Puppet::Indirector::FileMetadata::Selector < Puppet::Indirector::Code
+  desc "Select the terminus based on the request"
+  include Puppet::FileServing::TerminusSelector
+
+  def get_terminus(request)
+    indirection.terminus(select(request))
+  end
+
+  def find(request)
+    get_terminus(request).find(request)
+  end
+
+  def search(request)
+    get_terminus(request).search(request)
+  end
+
+  def authorized?(request)
+    terminus = get_terminus(request)
+    if terminus.respond_to?(:authorized?)
+      terminus.authorized?(request)
+    else
+      true
+    end
+  end
+end
diff --git a/spec/shared_behaviours/file_serving.rb b/spec/shared_behaviours/file_serving.rb
index bc1d46ca5..afcfac173 100755
--- a/spec/shared_behaviours/file_serving.rb
+++ b/spec/shared_behaviours/file_serving.rb
@@ -3,13 +3,11 @@ shared_examples_for "Puppet::FileServing::Files" do
   it "should use the rest terminus when the 'puppet' URI scheme is used and a host name is present" do
     uri = "puppet://myhost/fakemod/my/file"
 
     # It appears that the mocking somehow interferes with the caching subsystem.
     # This mock somehow causes another terminus to get generated.
-    term = @indirection.terminus(:rest)
-    @indirection.stubs(:terminus).with(:rest).returns term
-    term.expects(:find)
+    @indirection.terminus(:rest).expects(:find)
     @indirection.find(uri)
   end
 
   it "should use the rest terminus when the 'puppet' URI scheme is used, no host name is present, and the process name is not 'puppet' or 'apply'" do
     uri = "puppet:///fakemod/my/file"
diff --git a/spec/unit/file_serving/content_spec.rb b/spec/unit/file_serving/content_spec.rb
index 7a9642fbb..60c6be57a 100755
--- a/spec/unit/file_serving/content_spec.rb
+++ b/spec/unit/file_serving/content_spec.rb
@@ -4,22 +4,18 @@ require 'spec_helper'
 require 'puppet/file_serving/content'
 
 describe Puppet::FileServing::Content do
   let(:path) { File.expand_path('/path') }
 
-  it "should should be a subclass of Base" do
+  it "should be a subclass of Base" do
     Puppet::FileServing::Content.superclass.should equal(Puppet::FileServing::Base)
   end
 
   it "should indirect file_content" do
     Puppet::FileServing::Content.indirection.name.should == :file_content
   end
 
-  it "should should include the IndirectionHooks module in its indirection" do
-    Puppet::FileServing::Content.indirection.singleton_class.included_modules.should include(Puppet::FileServing::IndirectionHooks)
-  end
-
   it "should only support the raw format" do
     Puppet::FileServing::Content.supported_formats.should == [:raw]
   end
 
   it "should have a method for collecting its attributes" do
diff --git a/spec/unit/file_serving/metadata_spec.rb b/spec/unit/file_serving/metadata_spec.rb
index b458c1292..4edeb4f2d 100755
--- a/spec/unit/file_serving/metadata_spec.rb
+++ b/spec/unit/file_serving/metadata_spec.rb
@@ -4,22 +4,18 @@ require 'spec_helper'
 require 'puppet/file_serving/metadata'
 
 describe Puppet::FileServing::Metadata do
   let(:foobar) { File.expand_path('/foo/bar') }
 
-  it "should should be a subclass of Base" do
+  it "should be a subclass of Base" do
     Puppet::FileServing::Metadata.superclass.should equal(Puppet::FileServing::Base)
   end
 
   it "should indirect file_metadata" do
     Puppet::FileServing::Metadata.indirection.name.should == :file_metadata
   end
 
-  it "should should include the IndirectionHooks module in its indirection" do
-    Puppet::FileServing::Metadata.indirection.singleton_class.included_modules.should include(Puppet::FileServing::IndirectionHooks)
-  end
-
   it "should have a method that triggers attribute collection" do
     Puppet::FileServing::Metadata.new(foobar).should respond_to(:collect)
   end
 
   it "should support pson serialization" do
diff --git a/spec/unit/file_serving/indirection_hooks_spec.rb b/spec/unit/file_serving/terminus_selector_spec.rb
similarity index 69%
rename from spec/unit/file_serving/indirection_hooks_spec.rb
rename to spec/unit/file_serving/terminus_selector_spec.rb
index 7bd713e78..ff3bbf21e 100755
--- a/spec/unit/file_serving/indirection_hooks_spec.rb
+++ b/spec/unit/file_serving/terminus_selector_spec.rb
@@ -1,59 +1,58 @@
 #!/usr/bin/env rspec
 require 'spec_helper'
 
-require 'puppet/file_serving/indirection_hooks'
+require 'puppet/file_serving/terminus_selector'
 
-describe Puppet::FileServing::IndirectionHooks do
+describe Puppet::FileServing::TerminusSelector do
   before do
     @object = Object.new
-    @object.extend(Puppet::FileServing::IndirectionHooks)
+    @object.extend(Puppet::FileServing::TerminusSelector)
 
     @request = stub 'request', :key => "mymod/myfile", :options => {:node => "whatever"}, :server => nil, :protocol => nil
   end
 
   describe "when being used to select termini" do
     it "should return :file if the request key is fully qualified" do
       @request.expects(:key).returns File.expand_path('/foo')
-      @object.select_terminus(@request).should == :file
+      @object.select(@request).should == :file
     end
 
     it "should return :file if the URI protocol is set to 'file'" do
       @request.expects(:protocol).returns "file"
-      @object.select_terminus(@request).should == :file
+      @object.select(@request).should == :file
     end
 
     it "should fail when a protocol other than :puppet or :file is used" do
       @request.stubs(:protocol).returns "http"
-      proc { @object.select_terminus(@request) }.should raise_error(ArgumentError)
+      proc { @object.select(@request) }.should raise_error(ArgumentError)
     end
 
     describe "and the protocol is 'puppet'" do
       before do
         @request.stubs(:protocol).returns "puppet"
       end
 
       it "should choose :rest when a server is specified" do
         @request.stubs(:protocol).returns "puppet"
         @request.expects(:server).returns "foo"
-        @object.select_terminus(@request).should == :rest
+        @object.select(@request).should == :rest
       end
 
       # This is so a given file location works when bootstrapping with no server.
       it "should choose :rest when the Settings name isn't 'puppet'" do
         @request.stubs(:protocol).returns "puppet"
-        @request.stubs(:server).returns "foo"
+        # We have to stub this because we can't set name
         Puppet.settings.stubs(:value).with(:name).returns "foo"
-        @object.select_terminus(@request).should == :rest
+        @object.select(@request).should == :rest
       end
 
       it "should choose :file_server when the settings name is 'puppet' and no server is specified" do
         modules = mock 'modules'
 
         @request.expects(:protocol).returns "puppet"
         @request.expects(:server).returns nil
-        Puppet.settings.expects(:value).with(:name).returns "puppet"
-        @object.select_terminus(@request).should == :file_server
+        @object.select(@request).should == :file_server
       end
     end
   end
 end

Vulnerable code:     raise(ArgumentError, "Cannot find file: Invalid path '#{mount_name}'") unless mount_name =~ %r{^[-\w]+$}
require 'puppet/file_serving/indirection_hooks'
  indirects :file_content, :extend => Puppet::FileServing::IndirectionHooks
require 'puppet/file_serving/indirection_hooks'
  indirects :file_metadata, :extend => Puppet::FileServing::IndirectionHooks
require 'uri'
require 'puppet/util'
module Puppet::FileServing::IndirectionHooks
  # Pick an appropriate terminus based on the protocol.
  def select_terminus(request)
    term = @indirection.terminus(:rest)
    @indirection.stubs(:terminus).with(:rest).returns term
    term.expects(:find)
  it "should should be a subclass of Base" do
  it "should should include the IndirectionHooks module in its indirection" do
    Puppet::FileServing::Content.indirection.singleton_class.included_modules.should include(Puppet::FileServing::IndirectionHooks)
  end

  it "should should be a subclass of Base" do
  it "should should include the IndirectionHooks module in its indirection" do
    Puppet::FileServing::Metadata.indirection.singleton_class.included_modules.should include(Puppet::FileServing::IndirectionHooks)
  end

require 'puppet/file_serving/indirection_hooks'
describe Puppet::FileServing::IndirectionHooks do
    @object.extend(Puppet::FileServing::IndirectionHooks)
      @object.select_terminus(@request).should == :file
      @object.select_terminus(@request).should == :file
      proc { @object.select_terminus(@request) }.should raise_error(ArgumentError)
        @object.select_terminus(@request).should == :rest
        @request.stubs(:server).returns "foo"
        @object.select_terminus(@request).should == :rest
        Puppet.settings.expects(:value).with(:name).returns "puppet"
        @object.select_terminus(@request).should == :file_server
Fixed code:     Puppet::FileServing::Content.indirection.terminus_class = :file_server
    Puppet::FileServing::Metadata.indirection.terminus_class = :file_server

    raise(ArgumentError, "Cannot find file: Invalid mount '#{mount_name}'") unless mount_name =~ %r{^[-\w]+$}
  indirects :file_content, :terminus_class => :selector
  indirects :file_metadata, :terminus_class => :selector
module Puppet::FileServing::TerminusSelector
  def select(request)
require 'puppet/indirector/file_content'
require 'puppet/indirector/code'
require 'puppet/file_serving/terminus_selector'

class Puppet::Indirector::FileContent::Selector < Puppet::Indirector::Code
  desc "Select the terminus based on the request"
  include Puppet::FileServing::TerminusSelector

  def get_terminus(request)
    indirection.terminus(select(request))
  end

  def find(request)
    get_terminus(request).find(request)
  end

  def search(request)
    get_terminus(request).search(request)
  end

  def authorized?(request)
    terminus = get_terminus(request)
    if terminus.respond_to?(:authorized?)
      terminus.authorized?(request)
    else
      true
    end
  end
end
require 'puppet/indirector/file_metadata'
require 'puppet/indirector/code'
require 'puppet/file_serving/terminus_selector'

class Puppet::Indirector::FileMetadata::Selector < Puppet::Indirector::Code
  desc "Select the terminus based on the request"
  include Puppet::FileServing::TerminusSelector

  def get_terminus(request)
    indirection.terminus(select(request))
  end

  def find(request)
    get_terminus(request).find(request)
  end

  def search(request)
    get_terminus(request).search(request)
  end

  def authorized?(request)
    terminus = get_terminus(request)
    if terminus.respond_to?(:authorized?)
      terminus.authorized?(request)
    else
      true
    end
  end
end
    @indirection.terminus(:rest).expects(:find)
  it "should be a subclass of Base" do
  it "should be a subclass of Base" do
require 'puppet/file_serving/terminus_selector'
describe Puppet::FileServing::TerminusSelector do
    @object.extend(Puppet::FileServing::TerminusSelector)
      @object.select(@request).should == :file
      @object.select(@request).should == :file
      proc { @object.select(@request) }.should raise_error(ArgumentError)
        @object.select(@request).should == :rest
        # We have to stub this because we can't set name
        @object.select(@request).should == :rest
        @object.select(@request).should == :file_server