Skip to content

Commit

Permalink
Use a configuration object instead of a bare hash
Browse files Browse the repository at this point in the history
  • Loading branch information
maths22 committed Aug 21, 2018
1 parent c9e6f8f commit d547d5c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
13 changes: 3 additions & 10 deletions ext/nokogiri/xslt_stylesheet.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@

VALUE xslt;

enum xsltSecurityAction {
XSLT_SEC_FORBID = 1,
XSLT_SEC_ALLOW = 2
};

int vasprintf (char **strp, const char *fmt, va_list ap);
void vasprintf_free (void *p);

Expand Down Expand Up @@ -257,13 +252,11 @@ static VALUE registr(VALUE self, VALUE uri, VALUE obj)

int add_sec_pref(VALUE key, VALUE val, VALUE in)
{
Check_Type(key, T_FIXNUM);
Check_Type(val, T_FIXNUM);
xsltSecurityPrefsPtr xsltPrefs = (xsltSecurityPrefsPtr) in;
if(NUM2INT(val) == XSLT_SEC_FORBID) {
xsltSetSecurityPrefs(xsltPrefs, NUM2INT(key), xsltSecurityForbid);
} else if(NUM2INT(val) == XSLT_SEC_ALLOW) {
if(val == Qtrue) {
xsltSetSecurityPrefs(xsltPrefs, NUM2INT(key), xsltSecurityAllow);
} else if(val == Qfalse) {
xsltSetSecurityPrefs(xsltPrefs, NUM2INT(key), xsltSecurityForbid);
}

return ST_CONTINUE;
Expand Down
5 changes: 4 additions & 1 deletion lib/nokogiri/xslt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ def parse string, modules = {}
end
end

###
# Set the default security options used by libxslt
# +prefs+ should be an object of type Nokogiri::XSLT::Security::Config
def set_default_security_prefs prefs
Stylesheet.set_default_security_prefs(prefs.map{|k,v| { Security.keys[k] => v}}.reduce(:merge))
Stylesheet.set_default_security_prefs(Security.keys.map{|k,v| { v => prefs.send(k) }}.reduce(:merge))
end

###
Expand Down
29 changes: 21 additions & 8 deletions lib/nokogiri/xslt/security.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
module Nokogiri
module XSLT
module Security
class Config
attr_accessor :allow_read_file
attr_accessor :allow_write_file
attr_accessor :allow_create_directory
attr_accessor :allow_read_network
attr_accessor :allow_write_network

def initialize
@allow_read_file = false
@allow_write_file = false
@allow_create_directory = false
@allow_read_network = false
@allow_write_network = false
end
end

def self.keys
{
READ_FILE: 1,
WRITE_FILE: 2,
CREATE_DIRECTORY: 3,
READ_NETWORK: 4,
WRITE_NETWORK: 5
allow_read_file: 1,
allow_write_file: 2,
allow_create_directory: 3,
allow_read_network: 4,
allow_write_network: 5
}
end

FORBID = 1
ALLOW = 2
end
end
end
12 changes: 9 additions & 3 deletions test/test_xslt_transforms.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require "helper"

class TestXsltTransforms < Nokogiri::TestCase
include Nokogiri::XSLT::Security

def setup
@doc = Nokogiri::XML(File.open(XML_FILE))
Expand Down Expand Up @@ -197,11 +196,18 @@ def test_quote_params

if Nokogiri.uses_libxml?
def test_set_default_security_prefs
Nokogiri::XSLT.set_default_security_prefs({ READ_FILE: FORBID})
# Default should be secure
sec_prefs = Nokogiri::XSLT::Security::Config.new
Nokogiri::XSLT.set_default_security_prefs(sec_prefs)
assert_raises(RuntimeError) { Nokogiri::XSLT(File.open(XSLT_INCLUDING_FILE)) }

Nokogiri::XSLT.set_default_security_prefs({ READ_FILE: ALLOW})
sec_prefs.allow_read_file = true
Nokogiri::XSLT.set_default_security_prefs(sec_prefs)
assert doc = Nokogiri::XSLT(File.open(XSLT_INCLUDING_FILE))

sec_prefs.allow_read_file = false
Nokogiri::XSLT.set_default_security_prefs(sec_prefs)
assert_raises(RuntimeError) { Nokogiri::XSLT(File.open(XSLT_INCLUDING_FILE)) }
end
end

Expand Down

0 comments on commit d547d5c

Please sign in to comment.