Skip to content

Commit 777d05d

Browse files
committed
Merge branch 'feature/api-key' into feature/rotate-api-key
2 parents 81ef9f4 + cd836f8 commit 777d05d

File tree

4 files changed

+9
-21
lines changed

4 files changed

+9
-21
lines changed

apikey.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,13 @@ func (k *ApiKey) DecryptData(ciphertext []byte) ([]byte, error) {
105105
return nil, err
106106
}
107107

108-
// plaintext will hold decrypted content, it must be at least as long
109-
// as ciphertext or decryption process will panic
110-
plaintext := make([]byte, len(ciphertext))
108+
// plaintext must be as long as ciphertext minus the length of the IV, which is the same as the AES block size
109+
plaintext := make([]byte, len(ciphertext)-aes.BlockSize)
111110

112-
// get iv from encrypted content
111+
// the IV (initialization vector) is the first BlockSize bytes in the encrypted content
113112
iv := ciphertext[:aes.BlockSize]
114113

115-
// use CTR to decrypt content
114+
// use CTR to decrypt content, which starts BlockSize bytes into the ciphertext
116115
stream := cipher.NewCTR(block, iv)
117116
stream.XORKeyStream(plaintext, ciphertext[aes.BlockSize:])
118117

apikey_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,12 @@ func TestApiKey_EncryptDecrypt(t *testing.T) {
9999
name string
100100
secret string
101101
plaintext []byte
102-
want []byte
103102
wantErr bool
104103
}{
105104
{
106105
name: "test encrypt/decrypt",
107106
secret: "ED86600E-3DBF-4C23-A0DA-9C55D448",
108107
plaintext: []byte("this is a plaintext string to be encrypted"),
109-
want: []byte("this is a plaintext string to be encrypted"),
110108
wantErr: false,
111109
},
112110
}
@@ -131,8 +129,8 @@ func TestApiKey_EncryptDecrypt(t *testing.T) {
131129
return
132130
}
133131

134-
if !bytes.Equal(tt.plaintext, tt.want) {
135-
t.Errorf("results from decypt do not match expected. Got: %s, wanted: %s", decrypted, tt.want)
132+
if !bytes.Equal(tt.plaintext, decrypted) {
133+
t.Errorf("results from decypt do not match expected. Got: %s, wanted: %s", decrypted, tt.plaintext)
136134
return
137135
}
138136
})

lambda/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"log"
99
"net/http"
10+
"net/url"
1011
"os"
1112
"strings"
1213

@@ -156,6 +157,7 @@ func httpRequestFromProxyRequest(ctx context.Context, req events.APIGatewayProxy
156157
for k, v := range req.Headers {
157158
headers.Set(k, v)
158159
}
160+
requestURL, _ := url.Parse(req.Path)
159161
r := &http.Request{
160162
Method: req.HTTPMethod,
161163
ProtoMinor: 0,
@@ -164,6 +166,7 @@ func httpRequestFromProxyRequest(ctx context.Context, req events.APIGatewayProxy
164166
ContentLength: int64(len(req.Body)),
165167
RemoteAddr: req.RequestContext.Identity.SourceIP,
166168
RequestURI: req.Path,
169+
URL: requestURL,
167170
}
168171
return r.WithContext(ctx)
169172
}

webauthnuser.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package mfa
22

33
import (
4-
"bytes"
54
"crypto/sha256"
65
"encoding/base64"
76
"encoding/json"
@@ -223,9 +222,6 @@ func (u *WebauthnUser) Load() error {
223222
return errors.Wrap(err, "failed to decrypt encrypted session data")
224223
}
225224

226-
// decryption process includes extra/invalid \x00 character, so trim it out
227-
plain = bytes.Trim(plain, "\x00")
228-
229225
// unmarshal decrypted session data into SessionData
230226
var sd webauthn.SessionData
231227
err = json.Unmarshal(plain, &sd)
@@ -243,9 +239,6 @@ func (u *WebauthnUser) Load() error {
243239
return errors.Wrap(err, "failed to decrypt encrypted credential data")
244240
}
245241

246-
// decryption process includes extra/invalid \x00 character, so trim it out
247-
dec = bytes.Trim(dec, "\x00")
248-
249242
// unmarshal decrypted session data into Credentials
250243
var creds []webauthn.Credential
251244
err = json.Unmarshal(dec, &creds)
@@ -429,11 +422,6 @@ func (u *WebauthnUser) WebAuthnCredentials() []webauthn.Credential {
429422
return nil
430423
}
431424

432-
// decryption process includes extra/invalid \x00 character, so trim it out
433-
// at some point early in dev this was needed, but in testing recently it doesn't
434-
// make a difference. Leaving commented out for now until we know 100% it's not needed
435-
// credId = bytes.Trim(credId, "\x00")
436-
437425
decodedCredId, err := base64.RawURLEncoding.DecodeString(string(credId))
438426
if err != nil {
439427
log.Println("error decoding credential id:", err)

0 commit comments

Comments
 (0)