|
4 | 4 | package integration |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "encoding/base32" |
7 | 8 | "io" |
8 | 9 | "net" |
9 | 10 | "net/smtp" |
@@ -75,6 +76,51 @@ func TestIncomingEmail(t *testing.T) { |
75 | 76 | assert.Equal(t, payload, p) |
76 | 77 | }) |
77 | 78 |
|
| 79 | + tokenEncoding := base32.StdEncoding.WithPadding(base32.NoPadding) |
| 80 | + t.Run("Deprecated token version", func(t *testing.T) { |
| 81 | + defer tests.PrintCurrentTest(t)() |
| 82 | + |
| 83 | + payload := []byte{1, 2, 3, 4, 5} |
| 84 | + |
| 85 | + token, err := token_service.CreateToken(token_service.ReplyHandlerType, user, payload) |
| 86 | + require.NoError(t, err) |
| 87 | + assert.NotEmpty(t, token) |
| 88 | + |
| 89 | + // Set the token to version 1. |
| 90 | + unencodedToken, err := tokenEncoding.DecodeString(token) |
| 91 | + require.NoError(t, err) |
| 92 | + unencodedToken[0] = 1 |
| 93 | + token = tokenEncoding.EncodeToString(unencodedToken) |
| 94 | + |
| 95 | + ht, u, p, err := token_service.ExtractToken(db.DefaultContext, token) |
| 96 | + require.ErrorContains(t, err, "unsupported token version: 1") |
| 97 | + assert.Equal(t, token_service.UnknownHandlerType, ht) |
| 98 | + assert.Nil(t, u) |
| 99 | + assert.Nil(t, p) |
| 100 | + }) |
| 101 | + |
| 102 | + t.Run("MAC check", func(t *testing.T) { |
| 103 | + defer tests.PrintCurrentTest(t)() |
| 104 | + |
| 105 | + payload := []byte{1, 2, 3, 4, 5} |
| 106 | + |
| 107 | + token, err := token_service.CreateToken(token_service.ReplyHandlerType, user, payload) |
| 108 | + require.NoError(t, err) |
| 109 | + assert.NotEmpty(t, token) |
| 110 | + |
| 111 | + // Modify the MAC. |
| 112 | + unencodedToken, err := tokenEncoding.DecodeString(token) |
| 113 | + require.NoError(t, err) |
| 114 | + unencodedToken[len(unencodedToken)-1] ^= 0x01 |
| 115 | + token = tokenEncoding.EncodeToString(unencodedToken) |
| 116 | + |
| 117 | + ht, u, p, err := token_service.ExtractToken(db.DefaultContext, token) |
| 118 | + require.ErrorContains(t, err, "verification failed") |
| 119 | + assert.Equal(t, token_service.UnknownHandlerType, ht) |
| 120 | + assert.Nil(t, u) |
| 121 | + assert.Nil(t, p) |
| 122 | + }) |
| 123 | + |
78 | 124 | t.Run("Handler", func(t *testing.T) { |
79 | 125 | t.Run("Reply", func(t *testing.T) { |
80 | 126 | checkReply := func(t *testing.T, payload []byte, issue *issues_model.Issue, commentType issues_model.CommentType) { |
|
0 commit comments