Fix incoming status creation date not being restricted to standard ISO8601 (#27655)

This commit is contained in:
Claire 2023-11-06 10:28:14 +01:00 committed by GitHub
parent 6712bf86cd
commit c0989b78f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 6 deletions

View File

@ -53,7 +53,8 @@ class ActivityPub::Parser::StatusParser
end end
def created_at def created_at
@object['published']&.to_datetime datetime = @object['published']&.to_datetime
datetime if datetime.present? && (0..9999).cover?(datetime.year)
rescue ArgumentError rescue ArgumentError
nil nil
end end

View File

@ -134,6 +134,46 @@ RSpec.describe ActivityPub::Activity::Create do
subject.perform subject.perform
end end
context 'when object publication date is below ISO8601 range' do
let(:object_json) do
{
id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
type: 'Note',
content: 'Lorem ipsum',
published: '-0977-11-03T08:31:22Z',
}
end
it 'creates status with a valid creation date', :aggregate_failures do
status = sender.statuses.first
expect(status).to_not be_nil
expect(status.text).to eq 'Lorem ipsum'
expect(status.created_at).to be_within(30).of(Time.now.utc)
end
end
context 'when object publication date is above ISO8601 range' do
let(:object_json) do
{
id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
type: 'Note',
content: 'Lorem ipsum',
published: '10000-11-03T08:31:22Z',
}
end
it 'creates status with a valid creation date', :aggregate_failures do
status = sender.statuses.first
expect(status).to_not be_nil
expect(status.text).to eq 'Lorem ipsum'
expect(status.created_at).to be_within(30).of(Time.now.utc)
end
end
context 'when object has been edited' do context 'when object has been edited' do
let(:object_json) do let(:object_json) do
{ {
@ -145,18 +185,16 @@ RSpec.describe ActivityPub::Activity::Create do
} }
end end
it 'creates status' do it 'creates status with appropriate creation and edition dates', :aggregate_failures do
status = sender.statuses.first status = sender.statuses.first
expect(status).to_not be_nil expect(status).to_not be_nil
expect(status.text).to eq 'Lorem ipsum' expect(status.text).to eq 'Lorem ipsum'
end
it 'marks status as edited' do expect(status.created_at).to eq '2022-01-22T15:00:00Z'.to_datetime
status = sender.statuses.first
expect(status).to_not be_nil
expect(status.edited?).to be true expect(status.edited?).to be true
expect(status.edited_at).to eq '2022-01-22T16:00:00Z'.to_datetime
end end
end end