-
Notifications
You must be signed in to change notification settings - Fork 1
/
jeeves.go
280 lines (226 loc) · 7.07 KB
/
jeeves.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package jeeves
import (
"crypto"
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"bytes"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"time"
"github.com/codykrieger/jeeves/ask"
)
var certURLStringToBytesMap map[string][]byte
func init() {
certURLStringToBytesMap = make(map[string][]byte)
}
type Skill struct {
Name string
Endpoint string
ApplicationID string
Handler func(*Skill, *ask.Request) *ask.Response
internalHandler func(*requestContext, *ask.Request)
}
type requestContext struct {
writer http.ResponseWriter
req *http.Request
reqBody *[]byte
skill *Skill
}
func RegisterSkill(skill *Skill) http.Handler {
skill.internalHandler = func(ctx *requestContext, req *ask.Request) {
resp := skill.Handler(skill, req)
bytes, _ := resp.Bytes()
// FIXME: Handle errors.
ctx.writer.Header().Set("Content-Type", "application/json; charset=utf-8")
ctx.writer.Write(bytes)
}
return skill
}
func (skill *Skill) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := &requestContext{
writer: w,
req: r,
skill: skill,
}
ctx.process()
}
func (ctx *requestContext) err(code int, theError error) {
log.Printf("Error: %v; returning HTTP status code %v\n", theError, code)
http.Error(ctx.writer, http.StatusText(code), code)
}
func (ctx *requestContext) process() {
body, err := ioutil.ReadAll(ctx.req.Body)
if err != nil {
ctx.err(400, err)
return
}
ctx.reqBody = &body
ctx.req.Body = ioutil.NopCloser(bytes.NewReader(*ctx.reqBody))
req, err := ask.NewRequestFromJSON(bytes.NewReader(*ctx.reqBody))
if err != nil {
ctx.err(400, err)
return
}
if !ctx.validateRequestSignature(req) ||
!ctx.validateTimestamp(req) ||
!ctx.validateApplicationID(req) ||
!ctx.validateRequestType(req) {
return
}
ctx.skill.internalHandler(ctx, req)
}
func (ctx *requestContext) validateRequestSignature(req *ask.Request) bool {
certChainURL := ctx.req.Header.Get("SignatureCertChainUrl")
if !ctx.validateCertChainURL(certChainURL) {
return false
}
certBytes, err := readCertAtURL(certChainURL)
if err != nil {
ctx.err(400, fmt.Errorf("Unable to fetch/parse certificate at given URL (%v): %v", certChainURL, err))
return false
}
// FIXME: We need to check the additional blocks in the PEM structure to
// ensure the Amazon signing certificate (the first certificate in the PEM
// structure) has a valid chain of trust up to a trusted root CA (see
// https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/developing-an-alexa-skill-as-a-web-service#Checking%20the%20Signature%20of%20the%20Request).
// This will at least be sufficient for development, though.
var certs []*x509.Certificate
for {
var pemBlock *pem.Block
pemBlock, certBytes = pem.Decode(certBytes)
if pemBlock == nil {
break
}
cert, err := x509.ParseCertificate(pemBlock.Bytes)
if err != nil {
ctx.err(400, fmt.Errorf("Failed to parse x509 certificate: %v", err))
return false
}
now := time.Now().Unix()
if now < cert.NotBefore.Unix() || now > cert.NotAfter.Unix() {
ctx.err(400, fmt.Errorf("Given certificate is expired or not yet valid"))
// FIXME: Maybe blow away the url => cert data cache at this point to
// avoid having to restart the server in the case where the cert has
// been updated after expiry? Probably in some other failure cases, too.
return false
}
certs = append(certs, cert)
}
numberOfCerts := len(certs)
if numberOfCerts < 2 {
ctx.err(400, fmt.Errorf("Unexpected number of certificates (%v)", numberOfCerts))
return false
}
cert := certs[0]
validSAN := false
for _, san := range cert.Subject.Names {
if san.Value == "echo-api.amazon.com" {
validSAN = true
break
}
}
if !validSAN {
ctx.err(400, fmt.Errorf("No valid Subject Alternate Names included in given certificate"))
return false
}
intermediateCerts := x509.NewCertPool()
for i := 1; i < numberOfCerts; i++ {
intermediateCerts.AddCert(certs[i])
}
verifyOpts := x509.VerifyOptions{
Intermediates: intermediateCerts,
}
if _, err := cert.Verify(verifyOpts); err != nil {
ctx.err(400, fmt.Errorf("Failed to verify certificate chain"))
return false
}
publicKey := cert.PublicKey
encodedSignature := ctx.req.Header.Get("Signature")
decodedSignature, err := base64.StdEncoding.DecodeString(encodedSignature)
if err != nil {
ctx.err(400, fmt.Errorf("Couldn't decode base64 Signature header (%v)", encodedSignature))
return false
}
hash := sha1.New()
hash.Write(*ctx.reqBody)
err = rsa.VerifyPKCS1v15(publicKey.(*rsa.PublicKey), crypto.SHA1, hash.Sum(nil), decodedSignature)
if err != nil {
ctx.err(400, fmt.Errorf("Signature verification failed: %v", err))
return false
}
return true
}
func (ctx *requestContext) validateCertChainURL(urlString string) bool {
url, err := url.Parse(urlString)
if err != nil {
ctx.err(400, fmt.Errorf("Invalid SignatureCertChainUrl (%v)", urlString))
return false
}
// FIXME: This should technically be a case-insensitive match.
if url.Scheme != "https" {
ctx.err(400, fmt.Errorf("Invalid SignatureCertChainUrl scheme (%v)", urlString))
return false
}
// FIXME: This should technically be a case-insensitive match.
if url.Host != "s3.amazonaws.com" && url.Host != "s3.amazonaws.com:443" {
ctx.err(400, fmt.Errorf("Invalid SignatureCertChainUrl hostname (%v)", urlString))
return false
}
// This, on the other hand, *is* supposed to be a case-sensitive match.
if !strings.HasPrefix(url.Path, "/echo.api/") {
ctx.err(400, fmt.Errorf("Invalid SignatureCertChainUrl path (%v)", urlString))
return false
}
return true
}
func (ctx *requestContext) validateApplicationID(req *ask.Request) bool {
if req.Session.Application.ApplicationID != ctx.skill.ApplicationID {
ctx.err(400, fmt.Errorf("Expected application ID %v, got %v",
ctx.skill.ApplicationID, req.Session.Application.ApplicationID))
return false
}
return true
}
func (ctx *requestContext) validateTimestamp(req *ask.Request) bool {
ts, err := time.Parse(time.RFC3339Nano, req.Body.Timestamp)
if err != nil {
ctx.err(400, fmt.Errorf("Bad request timestamp %v", req.Body.Timestamp))
return false
}
if time.Since(ts) > time.Duration(30)*time.Second {
ctx.err(400, fmt.Errorf("Timestamp is too old"))
return false
}
return true
}
func (ctx *requestContext) validateRequestType(req *ask.Request) bool {
if !req.IsLaunchRequest() && !req.IsIntentRequest() && !req.IsSessionEndedRequest() {
ctx.err(400, fmt.Errorf("Request type '%v' invalid", req.Body.Type))
return false
}
return true
}
func readCertAtURL(urlString string) ([]byte, error) {
// FIXME: Re-enable caching after performance impact can be gauged.
// if bytes, ok := certURLStringToBytesMap[urlString]; ok {
// return bytes, nil
// }
resp, err := http.Get(urlString)
if err != nil {
return nil, fmt.Errorf("Couldn't fetch Amazon cert file: %v", err)
}
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("Couldn't read Amazon cert file: %v", err)
}
// certURLStringToBytesMap[urlString] = bytes
return bytes, nil
}