Skip to content

Commit

Permalink
Merge pull request #1647 from wpscanteam/db-backup
Browse files Browse the repository at this point in the history
Fixes #1642
  • Loading branch information
erwanlr committed May 18, 2021
2 parents 98a71d3 + c48be5e commit da180e1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Expand Up @@ -15,7 +15,7 @@ Lint/MissingSuper:
Lint/UriEscapeUnescape:
Enabled: false
Metrics/AbcSize:
Max: 25
Max: 27
Metrics/BlockLength:
Exclude:
- 'spec/**/*'
Expand Down
38 changes: 35 additions & 3 deletions app/finders/db_exports/known_locations.rb
Expand Up @@ -40,11 +40,24 @@ def full_request_params
# @return [ Hash ]
def potential_urls(opts = {})
urls = {}
index = 0

File.open(opts[:list]).each_with_index do |path, index|
path.gsub!('{domain_name}', domain_name)
File.open(opts[:list]).each do |path|
path.chomp!

urls[target.url(path.chomp)] = index
if path.include?('{domain_name}')
urls[target.url(path.gsub('{domain_name}', domain_name))] = index

if domain_name != domain_name_with_sub
urls[target.url(path.gsub('{domain_name}', domain_name_with_sub))] = index + 1

index += 1
end
else
urls[target.url(path)] = index
end

index += 1
end

urls
Expand All @@ -58,6 +71,25 @@ def domain_name
end
end

def domain_name_with_sub
@domain_name_with_sub ||=
if Resolv::AddressRegex.match?(target.uri.host)
target.uri.host
else
parsed = PublicSuffix.parse(target.uri.host)

if parsed.subdomain
parsed.subdomain.gsub(".#{parsed.tld}", '')
elsif parsed.domain
parsed.domain.gsub(".#{parsed.tld}", '')
else
target.uri.host
end
end
rescue PublicSuffix::DomainNotAllowed
@domain_name_with_sub = target.uri.host
end

def create_progress_bar(opts = {})
super(opts.merge(title: ' Checking DB Exports -'))
end
Expand Down
20 changes: 13 additions & 7 deletions spec/app/finders/db_exports/known_locations_spec.rb
Expand Up @@ -12,7 +12,7 @@
allow(target).to receive(:sub_dir).and_return(false)
end

it 'replaces {domain_name} by its value' do
it 'replaces {domain_name} by its values' do
expect(finder.potential_urls(opts).keys).to eql %w[
http://ex.lo/aa/ex.sql
http://ex.lo/aa/wordpress.sql
Expand All @@ -27,8 +27,8 @@
context "when #{sub_domain} sub-domain" do
let(:url) { "https://#{sub_domain}.domain.tld" }

it 'replaces {domain_name} by its correct value' do
expect(finder.potential_urls(opts).keys).to include "#{url}/domain.sql"
it 'replaces {domain_name} by its correct values' do
expect(finder.potential_urls(opts).keys).to include "#{url}/domain.sql", "#{url}/#{sub_domain}.domain.sql"
end
end
end
Expand All @@ -44,16 +44,22 @@
context 'when multi-level tlds and sub-domain' do
let(:url) { 'https://dev.something.com.tr' }

it 'replaces {domain_name} by its correct value' do
expect(finder.potential_urls(opts).keys).to include 'https://dev.something.com.tr/something.sql'
it 'replaces {domain_name} by its correct values' do
expect(finder.potential_urls(opts).keys).to include(
'https://dev.something.com.tr/something.sql',
'https://dev.something.com.tr/dev.something.sql'
)
end
end

context 'when some weird stuff' do
let(:url) { 'https://098f6bcd4621d373cade4e832627b4f6.aa-bb-ccc-dd.domain-test.com' }

it 'replaces {domain_name} by its correct value' do
expect(finder.potential_urls(opts).keys).to include "#{url}/domain-test.sql"
it 'replaces {domain_name} by its correct values' do
expect(finder.potential_urls(opts).keys).to include(
"#{url}/domain-test.sql",
"#{url}/098f6bcd4621d373cade4e832627b4f6.aa-bb-ccc-dd.domain-test.sql"
)
end
end

Expand Down

0 comments on commit da180e1

Please sign in to comment.