Skip to content

Commit

Permalink
Merge pull request #55 from suborbital/connor/static-file-improvements
Browse files Browse the repository at this point in the history
Improvements to static file access
  • Loading branch information
cohix committed Feb 13, 2021
2 parents a6edd20 + a6af798 commit 3129001
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
18 changes: 18 additions & 0 deletions bundle/bundle.go
Expand Up @@ -33,6 +33,9 @@ type WasmModuleRef struct {

// StaticFile returns a static file from the bundle, if it exists
func (b *Bundle) StaticFile(filePath string) ([]byte, error) {
// normalize in case the caller added `/` or `./` to the filename
filePath = NormalizeStaticFilename(filePath)

if _, exists := b.staticFiles[filePath]; !exists {
return nil, os.ErrNotExist
}
Expand All @@ -42,6 +45,9 @@ func (b *Bundle) StaticFile(filePath string) ([]byte, error) {
return nil, errors.Wrap(err, "failed to open bundle")
}

defer r.Close()

// re-add the static/ prefix to ensure sandboxing to the static directory
staticFilePath := ensurePrefix(filePath, "static/")

var contents []byte
Expand All @@ -53,6 +59,8 @@ func (b *Bundle) StaticFile(filePath string) ([]byte, error) {
return nil, errors.Wrap(err, "failed to Open static file")
}

defer file.Close()

contents, err = ioutil.ReadAll(file)
if err != nil {
return nil, errors.Wrap(err, "failed to ReadAll static file")
Expand Down Expand Up @@ -265,3 +273,13 @@ func ensurePrefix(val, prefix string) string {

return fmt.Sprintf("%s%s", prefix, val)
}

// NormalizeStaticFilename will take various variations of a filename and
// normalize it to what is listed in the staticFile name cache on the Bundle struct
func NormalizeStaticFilename(fileName string) string {
withoutStatic := strings.TrimPrefix(fileName, "static/")
withoutLeadingSlash := strings.TrimPrefix(withoutStatic, "/")
withoutDotSlash := strings.TrimPrefix(withoutLeadingSlash, "./")

return withoutDotSlash
}
18 changes: 9 additions & 9 deletions directive/directive.go
Expand Up @@ -147,19 +147,19 @@ func (d *Directive) Validate() error {

for i, h := range d.Handlers {
if h.Input.Type == "" {
problems.add(fmt.Errorf("handler at position %d missing type", i))
problems.add(fmt.Errorf("handler for resource %s missing type", h.Input.Resource))
}

if h.Input.Resource == "" {
problems.add(fmt.Errorf("handler at position %d missing resource", i))
problems.add(fmt.Errorf("handler for resource %s missing resource", h.Input.Resource))
}

if h.Input.Type == InputTypeRequest && h.Input.Method == "" {
problems.add(fmt.Errorf("handler at position %d is of type request, but does not specify a method", i))
problems.add(fmt.Errorf("handler for resource %s is of type request, but does not specify a method", h.Input.Resource))
}

if len(h.Steps) == 0 {
problems.add(fmt.Errorf("handler at position %d missing steps", i))
problems.add(fmt.Errorf("handler for resource %s missing steps", h.Input.Resource))
continue
}

Expand All @@ -170,21 +170,21 @@ func (d *Directive) Validate() error {
fnsToAdd := []string{}

if !s.IsFn() && !s.IsGroup() {
problems.add(fmt.Errorf("step at position %d for handler handler at position %d has neither Fn or Group", j, i))
problems.add(fmt.Errorf("step at position %d for resource %s has neither Fn or Group", j, h.Input.Resource))
}

validateFn := func(fn CallableFn) {
if _, exists := fns[fn.Fn]; !exists {
problems.add(fmt.Errorf("handler at positiion %d lists fn at step %d that does not exist: %s (did you forget a namespace?)", i, j, s.Fn))
problems.add(fmt.Errorf("handler for resource %s lists fn at step %d that does not exist: %s (did you forget a namespace?)", h.Input.Resource, j, fn.Fn))
}

if _, err := fn.ParseWith(); err != nil {
problems.add(fmt.Errorf("handler at position %d has invalid 'with' value at step %d: %s", i, j, err.Error()))
problems.add(fmt.Errorf("handler for resource %s has invalid 'with' value at step %d: %s", h.Input.Resource, j, err.Error()))
}

for _, d := range fn.DesiredState {
if _, exists := fullState[d.Key]; !exists {
problems.add(fmt.Errorf("handler at position %d has 'with' value at step %d referencing a key that is not yet available in the handler's state: %s", i, j, d.Key))
problems.add(fmt.Errorf("handler for resource %s has 'with' value at step %d referencing a key that is not yet available in the handler's state: %s", h.Input.Resource, j, d.Key))
}
}

Expand All @@ -211,7 +211,7 @@ func (d *Directive) Validate() error {

lastStep := h.Steps[len(h.Steps)-1]
if h.Response == "" && lastStep.IsGroup() {
problems.add(fmt.Errorf("handler at position %d has group as last step but does not include 'response' field", i))
problems.add(fmt.Errorf("handler for resource %s has group as last step but does not include 'response' field", h.Input.Resource))
} else if h.Response != "" {
if _, exists := fullState[h.Response]; !exists {
problems.add(fmt.Errorf("handler at positiion %d lists response state key that does not exist: %s", i, h.Response))
Expand Down
10 changes: 3 additions & 7 deletions rwasm/ffi_environment.go
Expand Up @@ -2,7 +2,6 @@ package rwasm

import (
"crypto/rand"
"fmt"
"math"
"math/big"
"sync"
Expand Down Expand Up @@ -151,16 +150,16 @@ func (w *wasmEnvironment) useInstance(req *request.CoordinatedRequest, ctx *rt.C
inst.lock.Lock()
defer inst.lock.Unlock()

inst.rtCtx = ctx
inst.request = req

// generate a random identifier as a reference to the instance in use to
// easily allow the Wasm module to reference itself when calling back over the FFI
ident, err := setupNewIdentifier(w.UUID, instIndex)
if err != nil {
return errors.Wrap(err, "failed to setupNewIdentifier")
}

inst.rtCtx = ctx
inst.request = req

instFunc(inst, ident)

removeIdentifier(ident)
Expand Down Expand Up @@ -288,7 +287,6 @@ func randomIdentifier() (int32, error) {
func (w *wasmInstance) readMemory(pointer int32, size int32) []byte {
memory, err := w.wasmerInst.Exports.GetMemory("memory")
if err != nil || memory == nil {
fmt.Println("MEMORY BAD")
// we failed
return []byte{}
}
Expand Down Expand Up @@ -327,7 +325,6 @@ func (w *wasmInstance) writeMemory(data []byte) (int32, error) {
func (w *wasmInstance) writeMemoryAtLocation(pointer int32, data []byte) {
memory, err := w.wasmerInst.Exports.GetMemory("memory")
if err != nil || memory == nil {
fmt.Println("MEMORY BAD")
// we failed
return
}
Expand All @@ -340,7 +337,6 @@ func (w *wasmInstance) writeMemoryAtLocation(pointer int32, data []byte) {
func (w *wasmInstance) deallocate(pointer int32, length int) {
dealloc, err := w.wasmerInst.Exports.GetFunction("deallocate")
if err != nil || dealloc == nil {
fmt.Println("DEALLOC BAD")
// we failed
return
}
Expand Down

0 comments on commit 3129001

Please sign in to comment.