Add validation for remote JSON

This commit is contained in:
Vikram Rangnekar
2019-06-04 10:54:51 -04:00
parent 8b06473e58
commit 2f55131315
9 changed files with 487 additions and 51 deletions

View File

@ -1,5 +1,9 @@
package serv
import (
"github.com/gobuffalo/flect"
)
type config struct {
AppName string `mapstructure:"app_name"`
Env string
@ -88,7 +92,7 @@ func (c *config) getAliasMap() map[string]string {
if len(t.Table) == 0 {
continue
}
m[t.Name] = t.Table
m[flect.Pluralize(t.Name)] = t.Table
}
return m
}
@ -102,11 +106,12 @@ func (c *config) getFilterMap() map[string][]string {
if len(t.Filter) == 0 {
continue
}
name := flect.Pluralize(t.Name)
if t.Filter[0] == "none" {
m[t.Name] = []string{}
m[name] = []string{}
} else {
m[t.Name] = t.Filter
m[name] = t.Filter
}
}

View File

@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
@ -75,6 +76,10 @@ func (c *coreContext) handleReq(w io.Writer, req *http.Request) error {
return errors.New("something wrong no remote ids found in db response")
}
if err != nil {
return err
}
var ob bytes.Buffer
err = jsn.Replace(&ob, data, from, to)
@ -192,14 +197,14 @@ func (c *coreContext) resolveRemotes(
return nil, nil
}
go func(n int) {
go func(n int, s *qcode.Select) {
defer wg.Done()
st := time.Now()
b, err := r.Fn(req, id)
if err != nil {
cerr = err
cerr = fmt.Errorf("%s: %s", s.Table, err)
return
}
@ -216,7 +221,7 @@ func (c *coreContext) resolveRemotes(
if len(s.Cols) != 0 {
err = jsn.Filter(&ob, b, colsToList(s.Cols))
if err != nil {
cerr = err
cerr = fmt.Errorf("%s: %s", s.Table, err)
return
}
@ -225,7 +230,7 @@ func (c *coreContext) resolveRemotes(
}
to[n] = jsn.Field{[]byte(s.FieldName), ob.Bytes()}
}(i)
}(i, s)
}
wg.Wait()

View File

@ -7,6 +7,7 @@ import (
"strings"
"github.com/cespare/xxhash/v2"
"github.com/dosco/super-graph/jsn"
"github.com/dosco/super-graph/psql"
)
@ -112,6 +113,10 @@ func buildFn(r configRemote) func(*http.Request, []byte) ([]byte, error) {
return nil, err
}
if err := jsn.ValidateBytes(b); err != nil {
return nil, err
}
return b, nil
}