Skip to content

Commit

Permalink
brought back handling of base64 values from api gateway stage variables
Browse files Browse the repository at this point in the history
  • Loading branch information
tmaiaroto committed Mar 3, 2019
1 parent 45a8710 commit 8cc3980
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
25 changes: 16 additions & 9 deletions framework/aegis.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"strconv"
"strings"
"time"
"unicode"

"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
Expand Down Expand Up @@ -168,20 +169,26 @@ func (a *Aegis) setAegisVariables(ctx context.Context, evt map[string]interface{
// Try to base64 decode it, because API Gateway stage variables may be encoded because
// they do not support certain special characters, which is a big problem for sensitive
// credentials that often include special characters.
// TODO: Find another solution, if possible. May need to pre/suffix variables because
// it's actually possible to have a value that's a valid base64 string that isn't
// intended to be decoded.
// It may ultimately be up to the user to decode. They set the value after all.
//
// sDec, err := base64.StdEncoding.DecodeString(v)
// if err == nil {
// a.Services.Variables[k] = string(sDec)
// }
sDec, err := base64.StdEncoding.DecodeString(v)
if err == nil && isASCII(string(sDec)) {
a.Services.Variables[k] = string(sDec)
}
}
}
}
}

// isASCII is a simple check to help determine if a base64 decode was necessary when reading values
// from API Gateway stage variables
func isASCII(s string) bool {
for _, c := range s {
if c > unicode.MaxASCII {
return false
}
}
return true
}

// aegisHandler configures services and determines how to handle the Lambda event
func (a *Aegis) aegisHandler(ctx context.Context, evt map[string]interface{}) (interface{}, error) {
if a.TraceContext == nil {
Expand Down
11 changes: 9 additions & 2 deletions framework/aegis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ func TestAegis(t *testing.T) {
"httpMethod": "GET",
"path": "/",
"stageVariables": map[string]string{
"foo": "bar",
"b64": "aGVsbG8gd29ybGQ=",
"foo": "bar",
"b64": "aGVsbG8gd29ybGQ=",
"b64FalsePositive": "aegistest",
},
"requestContext": map[string]interface{}{
"stage": "dev",
Expand Down Expand Up @@ -79,6 +80,12 @@ func TestAegis(t *testing.T) {
a.setAegisVariables(ctx, evt)
Convey("Should set variables from API Gateway stage variables", func() {
So(a.Variables, ShouldContainKey, "foo")
So(a.Variables, ShouldContainKey, "b64FalsePositive")
So(a.Variables["b64FalsePositive"], ShouldEqual, "aegistest")
})
Convey("Should handle base64 encoded values from API Gateway stage variables", func() {
So(a.Variables, ShouldContainKey, "b64")
So(a.Variables["b64"], ShouldEqual, "hello world")
})
})

Expand Down
4 changes: 4 additions & 0 deletions version/HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ a lightweight set of helpers or framework to help build things faster. It's to b
and flexible. 1.x will focus on adding more event router/handlers and helper functions.
Not every possible service will likely ever covered, the focus will be on the common.

## 1.16.1

- Brought back the base64 decoding of API Gateway stage variables

## 1.16.0

- Fixed an issue where convenience was causing problems with AWS SDK rate limiting, using Cognito
Expand Down
2 changes: 1 addition & 1 deletion version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
package version

// Semantic defines a semver string for aegis
const Semantic = "1.16.0"
const Semantic = "1.16.1"

// Current will return the current version
func Current() string {
Expand Down

0 comments on commit 8cc3980

Please sign in to comment.