logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

http_sig_test.exs (10033B)


      1 # Pleroma: A lightweight social networking server
      2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
      3 # SPDX-License-Identifier: AGPL-3.0-only
      4 
      5 # http signatures
      6 # Test data from https://tools.ietf.org/html/draft-cavage-http-signatures-08#appendix-C
      7 defmodule Pleroma.Web.HTTPSignaturesTest do
      8   use Pleroma.DataCase
      9   alias Pleroma.Web.HTTPSignatures
     10   import Pleroma.Factory
     11   import Tesla.Mock
     12 
     13   setup do
     14     mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
     15     :ok
     16   end
     17 
     18   @public_key hd(:public_key.pem_decode(File.read!("test/web/http_sigs/pub.key")))
     19               |> :public_key.pem_entry_decode()
     20 
     21   @headers %{
     22     "(request-target)" => "post /foo?param=value&pet=dog",
     23     "host" => "example.com",
     24     "date" => "Thu, 05 Jan 2014 21:31:40 GMT",
     25     "content-type" => "application/json",
     26     "digest" => "SHA-256=X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE=",
     27     "content-length" => "18"
     28   }
     29 
     30   @default_signature """
     31   keyId="Test",algorithm="rsa-sha256",signature="jKyvPcxB4JbmYY4mByyBY7cZfNl4OW9HpFQlG7N4YcJPteKTu4MWCLyk+gIr0wDgqtLWf9NLpMAMimdfsH7FSWGfbMFSrsVTHNTk0rK3usrfFnti1dxsM4jl0kYJCKTGI/UWkqiaxwNiKqGcdlEDrTcUhhsFsOIo8VhddmZTZ8w="
     32   """
     33 
     34   @basic_signature """
     35   keyId="Test",algorithm="rsa-sha256",headers="(request-target) host date",signature="HUxc9BS3P/kPhSmJo+0pQ4IsCo007vkv6bUm4Qehrx+B1Eo4Mq5/6KylET72ZpMUS80XvjlOPjKzxfeTQj4DiKbAzwJAb4HX3qX6obQTa00/qPDXlMepD2JtTw33yNnm/0xV7fQuvILN/ys+378Ysi082+4xBQFwvhNvSoVsGv4="
     36   """
     37 
     38   @all_headers_signature """
     39   keyId="Test",algorithm="rsa-sha256",headers="(request-target) host date content-type digest content-length",signature="Ef7MlxLXoBovhil3AlyjtBwAL9g4TN3tibLj7uuNB3CROat/9KaeQ4hW2NiJ+pZ6HQEOx9vYZAyi+7cmIkmJszJCut5kQLAwuX+Ms/mUFvpKlSo9StS2bMXDBNjOh4Auj774GFj4gwjS+3NhFeoqyr/MuN6HsEnkvn6zdgfE2i0="
     40   """
     41 
     42   test "split up a signature" do
     43     expected = %{
     44       "keyId" => "Test",
     45       "algorithm" => "rsa-sha256",
     46       "signature" =>
     47         "jKyvPcxB4JbmYY4mByyBY7cZfNl4OW9HpFQlG7N4YcJPteKTu4MWCLyk+gIr0wDgqtLWf9NLpMAMimdfsH7FSWGfbMFSrsVTHNTk0rK3usrfFnti1dxsM4jl0kYJCKTGI/UWkqiaxwNiKqGcdlEDrTcUhhsFsOIo8VhddmZTZ8w=",
     48       "headers" => ["date"]
     49     }
     50 
     51     assert HTTPSignatures.split_signature(@default_signature) == expected
     52   end
     53 
     54   test "validates the default case" do
     55     signature = HTTPSignatures.split_signature(@default_signature)
     56     assert HTTPSignatures.validate(@headers, signature, @public_key)
     57   end
     58 
     59   test "validates the basic case" do
     60     signature = HTTPSignatures.split_signature(@basic_signature)
     61     assert HTTPSignatures.validate(@headers, signature, @public_key)
     62   end
     63 
     64   test "validates the all-headers case" do
     65     signature = HTTPSignatures.split_signature(@all_headers_signature)
     66     assert HTTPSignatures.validate(@headers, signature, @public_key)
     67   end
     68 
     69   test "it contructs a signing string" do
     70     expected = "date: Thu, 05 Jan 2014 21:31:40 GMT\ncontent-length: 18"
     71     assert expected == HTTPSignatures.build_signing_string(@headers, ["date", "content-length"])
     72   end
     73 
     74   test "it validates a conn" do
     75     public_key_pem =
     76       "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnGb42rPZIapY4Hfhxrgn\nxKVJczBkfDviCrrYaYjfGxawSw93dWTUlenCVTymJo8meBlFgIQ70ar4rUbzl6GX\nMYvRdku072d1WpglNHXkjKPkXQgngFDrh2sGKtNB/cEtJcAPRO8OiCgPFqRtMiNM\nc8VdPfPdZuHEIZsJ/aUM38EnqHi9YnVDQik2xxDe3wPghOhqjxUM6eLC9jrjI+7i\naIaEygUdyst9qVg8e2FGQlwAeS2Eh8ygCxn+bBlT5OyV59jSzbYfbhtF2qnWHtZy\nkL7KOOwhIfGs7O9SoR2ZVpTEQ4HthNzainIe/6iCR5HGrao/T8dygweXFYRv+k5A\nPQIDAQAB\n-----END PUBLIC KEY-----\n"
     77 
     78     [public_key] = :public_key.pem_decode(public_key_pem)
     79 
     80     public_key =
     81       public_key
     82       |> :public_key.pem_entry_decode()
     83 
     84     conn = %{
     85       req_headers: [
     86         {"host", "localtesting.pleroma.lol"},
     87         {"connection", "close"},
     88         {"content-length", "2316"},
     89         {"user-agent", "http.rb/2.2.2 (Mastodon/2.1.0.rc3; +http://mastodon.example.org/)"},
     90         {"date", "Sun, 10 Dec 2017 14:23:49 GMT"},
     91         {"digest", "SHA-256=x/bHADMW8qRrq2NdPb5P9fl0lYpKXXpe5h5maCIL0nM="},
     92         {"content-type", "application/activity+json"},
     93         {"(request-target)", "post /users/demiurge/inbox"},
     94         {"signature",
     95          "keyId=\"http://mastodon.example.org/users/admin#main-key\",algorithm=\"rsa-sha256\",headers=\"(request-target) user-agent host date digest content-type\",signature=\"i0FQvr51sj9BoWAKydySUAO1RDxZmNY6g7M62IA7VesbRSdFZZj9/fZapLp6YSuvxUF0h80ZcBEq9GzUDY3Chi9lx6yjpUAS2eKb+Am/hY3aswhnAfYd6FmIdEHzsMrpdKIRqO+rpQ2tR05LwiGEHJPGS0p528NvyVxrxMT5H5yZS5RnxY5X2HmTKEgKYYcvujdv7JWvsfH88xeRS7Jlq5aDZkmXvqoR4wFyfgnwJMPLel8P/BUbn8BcXglH/cunR0LUP7sflTxEz+Rv5qg+9yB8zgBsB4C0233WpcJxjeD6Dkq0EcoJObBR56F8dcb7NQtUDu7x6xxzcgSd7dHm5w==\""}
     96       ]
     97     }
     98 
     99     assert HTTPSignatures.validate_conn(conn, public_key)
    100   end
    101 
    102   test "it validates a conn and fetches the key" do
    103     conn = %{
    104       params: %{"actor" => "http://mastodon.example.org/users/admin"},
    105       req_headers: [
    106         {"host", "localtesting.pleroma.lol"},
    107         {"x-forwarded-for", "127.0.0.1"},
    108         {"connection", "close"},
    109         {"content-length", "2307"},
    110         {"user-agent", "http.rb/2.2.2 (Mastodon/2.1.0.rc3; +http://mastodon.example.org/)"},
    111         {"date", "Sun, 11 Feb 2018 17:12:01 GMT"},
    112         {"digest", "SHA-256=UXsAnMtR9c7mi1FOf6HRMtPgGI1yi2e9nqB/j4rZ99I="},
    113         {"content-type", "application/activity+json"},
    114         {"signature",
    115          "keyId=\"http://mastodon.example.org/users/admin#main-key\",algorithm=\"rsa-sha256\",headers=\"(request-target) user-agent host date digest content-type\",signature=\"qXKqpQXUpC3d9bZi2ioEeAqP8nRMD021CzH1h6/w+LRk4Hj31ARJHDwQM+QwHltwaLDUepshMfz2WHSXAoLmzWtvv7xRwY+mRqe+NGk1GhxVZ/LSrO/Vp7rYfDpfdVtkn36LU7/Bzwxvvaa4ZWYltbFsRBL0oUrqsfmJFswNCQIG01BB52BAhGSCORHKtQyzo1IZHdxl8y80pzp/+FOK2SmHkqWkP9QbaU1qTZzckL01+7M5btMW48xs9zurEqC2sM5gdWMQSZyL6isTV5tmkTZrY8gUFPBJQZgihK44v3qgfWojYaOwM8ATpiv7NG8wKN/IX7clDLRMA8xqKRCOKw==\""},
    116         {"(request-target)", "post /users/demiurge/inbox"}
    117       ]
    118     }
    119 
    120     assert HTTPSignatures.validate_conn(conn)
    121   end
    122 
    123   test "validate this" do
    124     conn = %{
    125       params: %{"actor" => "https://niu.moe/users/rye"},
    126       req_headers: [
    127         {"x-forwarded-for", "149.202.73.191"},
    128         {"host", "testing.pleroma.lol"},
    129         {"x-cluster-client-ip", "149.202.73.191"},
    130         {"connection", "upgrade"},
    131         {"content-length", "2396"},
    132         {"user-agent", "http.rb/3.0.0 (Mastodon/2.2.0; +https://niu.moe/)"},
    133         {"date", "Sun, 18 Feb 2018 20:31:51 GMT"},
    134         {"digest", "SHA-256=dzH+vLyhxxALoe9RJdMl4hbEV9bGAZnSfddHQzeidTU="},
    135         {"content-type", "application/activity+json"},
    136         {"signature",
    137          "keyId=\"https://niu.moe/users/rye#main-key\",algorithm=\"rsa-sha256\",headers=\"(request-target) user-agent host date digest content-type\",signature=\"wtxDg4kIpW7nsnUcVJhBk6SgJeDZOocr8yjsnpDRqE52lR47SH6X7G16r7L1AUJdlnbfx7oqcvomoIJoHB3ghP6kRnZW6MyTMZ2jPoi3g0iC5RDqv6oAmDSO14iw6U+cqZbb3P/odS5LkbThF0UNXcfenVNfsKosIJycFjhNQc54IPCDXYq/7SArEKJp8XwEgzmiC2MdxlkVIUSTQYfjM4EG533cwlZocw1mw72e5mm/owTa80BUZAr0OOuhoWARJV9btMb02ZyAF6SCSoGPTA37wHyfM1Dk88NHf7Z0Aov/Fl65dpRM+XyoxdkpkrhDfH9qAx4iuV2VEWddQDiXHA==\""},
    138         {"(request-target)", "post /inbox"}
    139       ]
    140     }
    141 
    142     assert HTTPSignatures.validate_conn(conn)
    143   end
    144 
    145   test "validate this too" do
    146     conn = %{
    147       params: %{"actor" => "https://niu.moe/users/rye"},
    148       req_headers: [
    149         {"x-forwarded-for", "149.202.73.191"},
    150         {"host", "testing.pleroma.lol"},
    151         {"x-cluster-client-ip", "149.202.73.191"},
    152         {"connection", "upgrade"},
    153         {"content-length", "2342"},
    154         {"user-agent", "http.rb/3.0.0 (Mastodon/2.2.0; +https://niu.moe/)"},
    155         {"date", "Sun, 18 Feb 2018 21:44:46 GMT"},
    156         {"digest", "SHA-256=vS8uDOJlyAu78cF3k5EzrvaU9iilHCX3chP37gs5sS8="},
    157         {"content-type", "application/activity+json"},
    158         {"signature",
    159          "keyId=\"https://niu.moe/users/rye#main-key\",algorithm=\"rsa-sha256\",headers=\"(request-target) user-agent host date digest content-type\",signature=\"IN6fHD8pLiDEf35dOaRHzJKc1wBYh3/Yq0ItaNGxUSbJTd2xMjigZbcsVKzvgYYjglDDN+disGNeD+OBKwMqkXWaWe/lyMc9wHvCH5NMhpn/A7qGLY8yToSt4vh8ytSkZKO6B97yC+Nvy6Fz/yMbvKtFycIvSXCq417cMmY6f/aG+rtMUlTbKO5gXzC7SUgGJCtBPCh1xZzu5/w0pdqdjO46ePNeR6JyJSLLV4hfo3+p2n7SRraxM4ePVCUZqhwS9LPt3Zdhy3ut+IXCZgMVIZggQFM+zXLtcXY5HgFCsFQr5WQDu+YkhWciNWtKFnWfAsnsg5sC330lZ/0Z8Z91yA==\""},
    160         {"(request-target)", "post /inbox"}
    161       ]
    162     }
    163 
    164     assert HTTPSignatures.validate_conn(conn)
    165   end
    166 
    167   test "it generates a signature" do
    168     user = insert(:user)
    169     assert HTTPSignatures.sign(user, %{host: "mastodon.example.org"}) =~ "keyId=\""
    170   end
    171 
    172   test "this too" do
    173     conn = %{
    174       params: %{"actor" => "https://mst3k.interlinked.me/users/luciferMysticus"},
    175       req_headers: [
    176         {"host", "soc.canned-death.us"},
    177         {"user-agent", "http.rb/3.0.0 (Mastodon/2.2.0; +https://mst3k.interlinked.me/)"},
    178         {"date", "Sun, 11 Mar 2018 12:19:36 GMT"},
    179         {"digest", "SHA-256=V7Hl6qDK2m8WzNsjzNYSBISi9VoIXLFlyjF/a5o1SOc="},
    180         {"content-type", "application/activity+json"},
    181         {"signature",
    182          "keyId=\"https://mst3k.interlinked.me/users/luciferMysticus#main-key\",algorithm=\"rsa-sha256\",headers=\"(request-target) user-agent host date digest content-type\",signature=\"CTYdK5a6lYMxzmqjLOpvRRASoxo2Rqib2VrAvbR5HaTn80kiImj15pCpAyx8IZp53s0Fn/y8MjCTzp+absw8kxx0k2sQAXYs2iy6xhdDUe7iGzz+XLAEqLyZIZfecynaU2nb3Z2XnFDjhGjR1vj/JP7wiXpwp6o1dpDZj+KT2vxHtXuB9585V+sOHLwSB1cGDbAgTy0jx/2az2EGIKK2zkw1KJuAZm0DDMSZalp/30P8dl3qz7DV2EHdDNfaVtrs5BfbDOZ7t1hCcASllzAzgVGFl0BsrkzBfRMeUMRucr111ZG+c0BNOEtJYOHSyZsSSdNknElggCJekONYMYk5ZA==\""},
    183         {"x-forwarded-for", "2607:5300:203:2899::31:1337"},
    184         {"x-forwarded-host", "soc.canned-death.us"},
    185         {"x-forwarded-server", "soc.canned-death.us"},
    186         {"connection", "Keep-Alive"},
    187         {"content-length", "2006"},
    188         {"(request-target)", "post /inbox"}
    189       ]
    190     }
    191 
    192     assert HTTPSignatures.validate_conn(conn)
    193   end
    194 end