mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 06:49:14 +08:00
FIX: Ensure that extract_upload_ids works with all short URLs (#17070)
We do not zero-pad our base62 short URLs, so there is no guarantee that the length is 27. Instead, let's greedily match all consecutive base62 characters and look for a matching upload. This revertsbd32656157
and36f5d5eada
.
This commit is contained in:
parent
f0c6dd5682
commit
6650218e3d
|
@ -515,7 +515,7 @@ class Upload < ActiveRecord::Base
|
||||||
sha1s << match[0]
|
sha1s << match[0]
|
||||||
end
|
end
|
||||||
|
|
||||||
raw.scan(/\/([a-zA-Z0-9]{27})/).each do |match|
|
raw.scan(/\/([a-zA-Z0-9]+)/).each do |match|
|
||||||
sha1s << Upload.sha1_from_base62_encoded(match[0])
|
sha1s << Upload.sha1_from_base62_encoded(match[0])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -520,9 +520,7 @@ describe Upload do
|
||||||
describe '.extract_upload_ids' do
|
describe '.extract_upload_ids' do
|
||||||
let(:upload) { Fabricate(:upload) }
|
let(:upload) { Fabricate(:upload) }
|
||||||
|
|
||||||
# This spec fails when upload has a sha1 of "035839d28676a96fda268562e6aa93c57b11113c".
|
it 'works with short URLs' do
|
||||||
# When the sha1 is converted to hex, the leading 0 is ignored so the base62 encoding will be 26 chars long.
|
|
||||||
skip 'works with short URLs' do
|
|
||||||
ids = Upload.extract_upload_ids("This URL #{upload.short_url} is an upload")
|
ids = Upload.extract_upload_ids("This URL #{upload.short_url} is an upload")
|
||||||
expect(ids).to contain_exactly(upload.id)
|
expect(ids).to contain_exactly(upload.id)
|
||||||
end
|
end
|
||||||
|
@ -532,12 +530,17 @@ describe Upload do
|
||||||
expect(ids).to contain_exactly(upload.id)
|
expect(ids).to contain_exactly(upload.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
# This spec fails when upload has a sha1 of "035839d28676a96fda268562e6aa93c57b11113c".
|
it 'works with Base62 hashes' do
|
||||||
# When the sha1 is converted to hex, the leading 0 is ignored so the base62 encoding will be 26 chars long.
|
|
||||||
skip 'works with Base62 hashes' do
|
|
||||||
ids = Upload.extract_upload_ids("This URL /#{Upload.base62_sha1(upload.sha1)} is an upload")
|
ids = Upload.extract_upload_ids("This URL /#{Upload.base62_sha1(upload.sha1)} is an upload")
|
||||||
expect(ids).to contain_exactly(upload.id)
|
expect(ids).to contain_exactly(upload.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'works with shorter base62 hashes (when sha1 has leading 0s)' do
|
||||||
|
upload.update(sha1: "0000c513e1da04f7b4e99230851ea2aafeb8cc4e")
|
||||||
|
base62 = Upload.base62_sha1(upload.sha1).delete_prefix("0")
|
||||||
|
ids = Upload.extract_upload_ids("This URL /#{base62} is an upload")
|
||||||
|
expect(ids).to contain_exactly(upload.id)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def enable_secure_media
|
def enable_secure_media
|
||||||
|
|
Loading…
Reference in New Issue
Block a user