diff options
author | Claire <claire.github-309c@sitedethib.com> | 2023-11-06 10:28:14 +0100 |
---|---|---|
committer | Claire <claire.github-309c@sitedethib.com> | 2023-12-04 15:27:44 +0100 |
commit | 49693fe42f4528db4b41a2c346391016e9fe954a (patch) | |
tree | 184910dd88f647cf8321a5f5c59e692ee88efab1 | |
parent | 16262f815d7d120bd73ffb71740ffaea3771683b (diff) |
Fix incoming status creation date not being restricted to standard ISO8601 (#27655)
-rw-r--r-- | app/lib/activitypub/parser/status_parser.rb | 3 | ||||
-rw-r--r-- | spec/lib/activitypub/activity/create_spec.rb | 50 |
2 files changed, 46 insertions, 7 deletions
diff --git a/app/lib/activitypub/parser/status_parser.rb b/app/lib/activitypub/parser/status_parser.rb index 3ba154d0155..45f5fc5bf2d 100644 --- a/app/lib/activitypub/parser/status_parser.rb +++ b/app/lib/activitypub/parser/status_parser.rb @@ -53,7 +53,8 @@ class ActivityPub::Parser::StatusParser end def created_at - @object['published']&.to_datetime + datetime = @object['published']&.to_datetime + datetime if datetime.present? && (0..9999).cover?(datetime.year) rescue ArgumentError nil end diff --git a/spec/lib/activitypub/activity/create_spec.rb b/spec/lib/activitypub/activity/create_spec.rb index 1a25395fad3..378ba0cd1b5 100644 --- a/spec/lib/activitypub/activity/create_spec.rb +++ b/spec/lib/activitypub/activity/create_spec.rb @@ -29,29 +29,67 @@ RSpec.describe ActivityPub::Activity::Create do subject.perform end - context 'object has been edited' do + 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: '2022-01-22T15:00:00Z', - updated: '2022-01-22T16:00:00Z', + published: '-0977-11-03T08:31:22Z', } end - it 'creates status' do + 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 - it 'marks status as edited' do + context 'when object has been edited' do + let(:object_json) do + { + id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join, + type: 'Note', + content: 'Lorem ipsum', + published: '2022-01-22T15:00:00Z', + updated: '2022-01-22T16:00:00Z', + } + end + + it 'creates status with appropriate creation and edition dates', :aggregate_failures do status = sender.statuses.first expect(status).to_not be_nil - expect(status.edited?).to eq true + expect(status.text).to eq 'Lorem ipsum' + + expect(status.created_at).to eq '2022-01-22T15:00:00Z'.to_datetime + + expect(status.edited?).to be true + expect(status.edited_at).to eq '2022-01-22T16:00:00Z'.to_datetime end end |