diff --git a/lib/wpscan/helper.rb b/lib/wpscan/helper.rb index 0804d68d..2ff8b3c6 100644 --- a/lib/wpscan/helper.rb +++ b/lib/wpscan/helper.rb @@ -6,12 +6,14 @@ rescue StandardError => e raise "JSON parsing error in #{file} #{e}" end -# @return [ Symbol ] +# Sanitize and classify a slug # @note As a class can not start with a digit or underscore, a D_ is -# put as a prefix in such case. Ugly but well :x -# Not only used to classify slugs though, but Dynamic Finder names as well +# put as a prefix in such case. Ugly but well :x +# Not only used to classify slugs though, but Dynamic Finder names as well +# +# @return [ Symbol ] def classify_slug(slug) - classified = slug.to_s.tr('-', '_').camelize.to_s + classified = slug.to_s.gsub(/[^a-z\d\-]/i, '-').gsub(/\-{1,}/, '_').camelize.to_s classified = "D_#{classified}" if classified[0] =~ /\d/ classified.to_sym diff --git a/spec/lib/db/dynamic_finders/plugin_spec.rb b/spec/lib/db/dynamic_finders/plugin_spec.rb index aabba1f4..5e481501 100644 --- a/spec/lib/db/dynamic_finders/plugin_spec.rb +++ b/spec/lib/db/dynamic_finders/plugin_spec.rb @@ -47,7 +47,7 @@ describe WPScan::DB::DynamicFinders::Plugin do describe '.create_versions_finders' do # handled and tested in spec/lib/finders/dynamic_finders/plugin_version_spec - describe 'Try to create the finders twice' do + context 'When trying to create the finders twice' do # let's just test one slug, no need to test them all let(:slug) { '12-step-meeting-list' } @@ -57,6 +57,14 @@ describe WPScan::DB::DynamicFinders::Plugin do expect { WPScan::DB::DynamicFinders::Plugin.create_versions_finders(slug) }.to_not raise_error end end + + context 'when the slug contains non alpha-numeric chars' do + let(:slug) { 'test.something' } + + it 'sanitize it and does not raise an error' do + expect { WPScan::DB::DynamicFinders::Plugin.create_versions_finders(slug) }.to_not raise_error + end + end end describe '.version_finder_super_class' do diff --git a/spec/lib/helper_spec.rb b/spec/lib/helper_spec.rb new file mode 100644 index 00000000..a1982664 --- /dev/null +++ b/spec/lib/helper_spec.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +describe '#classify_slug' do + { + 'slug' => :Slug, + 'slug-usual' => :SlugUsual, + '12-slug' => :D_12Slug, + 'slug.s' => :SlugS, + 'slug yolo $' => :SlugYolo, + 'slug $ ab.cd/12' => :SlugAbCd12 + }.each do |slug, expected_symbol| + context "when #{slug}" do + it "returns #{expected_symbol}" do + expect(classify_slug(slug)).to eql expected_symbol + end + end + end +end