From a8cd859ef930d968bebeb2afb373ed002e8824f7 Mon Sep 17 00:00:00 2001
From: darkkirb <lotte@chir.rs>
Date: Mon, 9 Jan 2023 22:12:28 +0000
Subject: [PATCH] Use actual ISO8601 timestamps for masto API (#425)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Some users post posts with spoofed timestamp, and some clients will have issues with certain dates. Tusky for example crashes if the date is any sooner than 1 BCE (“year zero” in the representation).

I limited the range of what is considered a valid date to be somewhere between the years 1583 and 9999 (inclusive).

The numbers have been chosen because:

- ISO 8601 only allows years before 1583 with “mutual agreement”
- Years after 9999 could cause issues with certain clients as well

Co-authored-by: Charlotte 🦝 Delenk <lotte@chir.rs>
Reviewed-on: https://akkoma.dev/AkkomaGang/akkoma/pulls/425
Co-authored-by: darkkirb <lotte@chir.rs>
Co-committed-by: darkkirb <lotte@chir.rs>
---
 lib/pleroma/web/common_api/utils.ex        | 17 ++++++++++++-----
 test/pleroma/web/common_api/utils_test.exs | 12 ++++++++++--
 2 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex
index aee19a840..345c5d10d 100644
--- a/lib/pleroma/web/common_api/utils.ex
+++ b/lib/pleroma/web/common_api/utils.ex
@@ -328,20 +328,27 @@ defmodule Pleroma.Web.CommonAPI.Utils do
   end
 
   def to_masto_date(%NaiveDateTime{} = date) do
-    date
-    |> NaiveDateTime.to_iso8601()
-    |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
+    # NOTE: Elixir’s ISO 8601 format is a superset of the real standard
+    # It supports negative years for example.
+    # ISO8601 only supports years before 1583 with mutual agreement
+    if date.year < 1583 do
+      "1970-01-01T00:00:00Z"
+    else
+      date
+      |> NaiveDateTime.to_iso8601()
+      |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
+    end
   end
 
   def to_masto_date(date) when is_binary(date) do
     with {:ok, date} <- NaiveDateTime.from_iso8601(date) do
       to_masto_date(date)
     else
-      _ -> ""
+      _ -> "1970-01-01T00:00:00Z"
     end
   end
 
-  def to_masto_date(_), do: ""
+  def to_masto_date(_), do: "1970-01-01T00:00:00Z"
 
   defp shortname(name) do
     with max_length when max_length > 0 <-
diff --git a/test/pleroma/web/common_api/utils_test.exs b/test/pleroma/web/common_api/utils_test.exs
index a88d7681a..f56d21c70 100644
--- a/test/pleroma/web/common_api/utils_test.exs
+++ b/test/pleroma/web/common_api/utils_test.exs
@@ -495,8 +495,16 @@ defmodule Pleroma.Web.CommonAPI.UtilsTest do
       assert Utils.to_masto_date("2015-01-23T23:50:07.123Z") == "2015-01-23T23:50:07.000Z"
     end
 
-    test "returns empty string when date invalid" do
-      assert Utils.to_masto_date("2015-01?23T23:50:07.123Z") == ""
+    test "returns unix epoch when date invalid" do
+      assert Utils.to_masto_date("2015-01?23T23:50:07.123Z") == "1970-01-01T00:00:00Z"
+    end
+
+    test "returns unix epoch when date is before the introduction of the Gregorian Calendar" do
+      assert Utils.to_masto_date("0621-01-01T00:00:00Z") == "1970-01-01T00:00:00Z"
+    end
+
+    test "returns unix epoch when date is BCE" do
+      assert Utils.to_masto_date("-0420-01-01T00:00:00Z") == "1970-01-01T00:00:00Z"
     end
   end