Commit 39318708 authored by Sean McGivern's avatar Sean McGivern

Normalise license MD5 values

License files uploaded in the UI always have newlines as separators, with a
single trailing newline.

Licenses pasted in the text box will have newlines replaced by \r\n by the
browser, and may have zero trailing newlines, or many.

Normalising these to a canonical form lets us match against the license app more
efficiently.
parent ee4b5fc6
......@@ -131,6 +131,12 @@ class License < ActiveRecord::Base
self.data = file.read
end
def md5
normalized_data = self.data.gsub("\r\n", "\n").gsub(/\n+$/, '') + "\n"
Digest::MD5.hexdigest(normalized_data)
end
def license
return nil unless self.data
......
......@@ -82,7 +82,7 @@ module Gitlab
if license
usage_data[:edition] = license_edition(license.plan)
usage_data[:license_md5] = Digest::MD5.hexdigest(license.data)
usage_data[:license_md5] = license.md5
usage_data[:historical_max_users] = ::HistoricalData.max_historical_user_count
usage_data[:licensee] = license.licensee
usage_data[:license_user_count] = license.restricted_user_count
......
......@@ -315,6 +315,26 @@ describe License do
end
end
describe "#md5" do
it "returns the same MD5 for licenses with carriage returns and those without" do
other_license = build(:license, data: license.data.gsub("\n", "\r\n"))
expect(other_license.md5).to eq(license.md5)
end
it "returns the same MD5 for licenses with trailing newlines and those without" do
other_license = build(:license, data: license.data.chomp)
expect(other_license.md5).to eq(license.md5)
end
it "returns the same MD5 for licenses with multiple trailing newlines and those with a single trailing newline" do
other_license = build(:license, data: "#{license.data}\n\n\n")
expect(other_license.md5).to eq(license.md5)
end
end
describe "#license" do
context "when no data is provided" do
before do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment