Merge pull request #1019 from wpscanteam/hash
remove scripts before calculating hashes
This commit is contained in:
@@ -91,15 +91,18 @@ class WebSite
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Compute the MD5 of the page
|
# Compute the MD5 of the page
|
||||||
# Comments are deleted from the page to avoid cache generation details
|
# Comments and scripts are deleted from the page to avoid cache generation details
|
||||||
#
|
#
|
||||||
# @param [ String, Typhoeus::Response ] page The url of the response of the page
|
# @param [ String, Typhoeus::Response ] page The url of the response of the page
|
||||||
#
|
#
|
||||||
# @return [ String ] The MD5 hash of the page
|
# @return [ String ] The MD5 hash of the page
|
||||||
def self.page_hash(page)
|
def self.page_hash(page)
|
||||||
page = Browser.get(page, { followlocation: true, cache_ttl: 0 }) unless page.is_a?(Typhoeus::Response)
|
page = Browser.get(page, { followlocation: true, cache_ttl: 0 }) unless page.is_a?(Typhoeus::Response)
|
||||||
|
# remove comments
|
||||||
Digest::MD5.hexdigest(page.body.gsub(/<!--.*?-->/m, ''))
|
page = page.body.gsub(/<!--.*?-->/m, '')
|
||||||
|
# remove javascript stuff
|
||||||
|
page = page.gsub(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/m, '')
|
||||||
|
Digest::MD5.hexdigest(page)
|
||||||
end
|
end
|
||||||
|
|
||||||
def homepage_hash
|
def homepage_hash
|
||||||
|
|||||||
@@ -176,6 +176,17 @@ describe 'WebSite' do
|
|||||||
@expected = "yolo\n\n\nworld!"
|
@expected = "yolo\n\n\nworld!"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'when there are scripts' do
|
||||||
|
let(:page) {
|
||||||
|
body = "yolo\n\n<script type=\"text/javascript\">alert('Hi');</script>\nworld!"
|
||||||
|
Typhoeus::Response.new(body: body)
|
||||||
|
}
|
||||||
|
|
||||||
|
it 'removes them' do
|
||||||
|
@expected = "yolo\n\n\nworld!"
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#homepage_hash' do
|
describe '#homepage_hash' do
|
||||||
|
|||||||
Reference in New Issue
Block a user