From 1a3d74e1cef5f9d6253effc1165107e64c77a4cb Mon Sep 17 00:00:00 2001 From: Vikram Rangnekar Date: Sun, 2 Feb 2020 01:43:09 -0500 Subject: [PATCH] Fix issues surfaced by the fuzzer --- .gitignore | 4 +- docs/guide.md | 170 +++++++++++++++++++++++++++++++------------- go.mod | 1 + go.sum | 2 + psql/fuzz.go | 54 ++++++++++++++ psql/insert.go | 5 +- psql/mutate.go | 5 +- psql/psql_test.go | 93 +----------------------- psql/query.go | 8 +-- psql/test_schema.go | 102 ++++++++++++++++++++++++++ psql/update.go | 5 +- qcode/fuzz.go | 6 +- 12 files changed, 304 insertions(+), 151 deletions(-) create mode 100644 psql/fuzz.go create mode 100644 psql/test_schema.go diff --git a/.gitignore b/.gitignore index 8bef108..6855eb8 100644 --- a/.gitignore +++ b/.gitignore @@ -34,4 +34,6 @@ supergraph *-fuzz.zip crashers suppressions -release \ No newline at end of file +release +.gofuzz +*-fuzz.zip diff --git a/docs/guide.md b/docs/guide.md index 92e8788..6299a1f 100644 --- a/docs/guide.md +++ b/docs/guide.md @@ -4,9 +4,9 @@ sidebar: auto # Guide to Super Graph -Super Graph is a micro-service that instantly and without code gives you a high performance and secure GraphQL API. Your GraphQL queries are auto translated into a single fast SQL query. No more writing API code as you develop your web frontend just make the query you need and Super Graph will do the rest. +Super Graph is a service that instantly and without code gives you a high performance and secure GraphQL API. Your GraphQL queries are auto translated into a single fast SQL query. No more spending weeks or months writing backend API code. Just make the query you need and Super Graph will do the rest. -Super Graph has a rich feature set like integrating with your existing Ruby on Rails apps, joining your DB with data from remote APIs, Role and Attribute based access control, Supoport for JWT tokens, DB migrations, seeding and a lot more. +Super Graph has a rich feature set like integrating with your existing Ruby on Rails apps, joining your DB with data from remote APIs, Role and Attribute based access control, Support for JWT tokens, DB migrations, seeding and a lot more. ## Features @@ -47,14 +47,14 @@ open http://localhost:3000 open http://localhost:8080 ``` -::: warning DEMO REQUIREMENTS +::: tip DEMO REQUIREMENTS This demo requires `docker` you can either install it using `brew` or from the docker website [https://docs.docker.com/docker-for-mac/install/](https://docs.docker.com/docker-for-mac/install/) ::: #### Trying out GraphQL -We currently fully support queries and mutations. Support for `subscriptions` is work in progress. For example the below GraphQL query would fetch two products that belong to the current user where the price is greater than 10. +We fully support queries and mutations. For example the below GraphQL query would fetch two products that belong to the current user where the price is greater than 10. #### GQL Query @@ -76,32 +76,6 @@ query { } ``` -In another example the below GraphQL mutation would insert a product into the database. The first part of the below example is the variable data and the second half is the GraphQL mutation. For mutations data has to always ben passed as a variable. - -```json -{ - "data": { - "name": "Art of Computer Programming", - "description": "The Art of Computer Programming (TAOCP) is a comprehensive monograph written by computer scientist Donald Knuth", - "price": 30.5 - } -} -``` - -```graphql -mutation { - product(insert: $data) { - id - name - } -} -``` - -The above GraphQL query returns the JSON result below. It handles all -kinds of complexity without you having to writing a line of code. - -For example there is a while greater than `gt` and a limit clause on a child field. And the `avatar` field is renamed to `picture`. The `password` field is blocked and not returned. Finally the relationship between the `users` table and the `products` table is auto discovered and used. - #### JSON Result ```json @@ -128,19 +102,107 @@ For example there is a while greater than `gt` and a limit clause on a child fie } ``` -#### Try with an authenticated user +::: tip Testing with a user +In development mode you can use the `X-User-ID: 4` header to set a user id so you don't have to worries about cookies etc. This can be set using the *HTTP Headers* tab at the bottom of the web UI. +::: -In development mode you can use the `X-User-ID: 4` header to set a user id so you don't have to worries about cookies etc. This can be set using the *HTTP Headers* tab at the bottom of the web UI you'll see when you visit the above link. You can also directly run queries from the commandline like below. +In another example the below GraphQL mutation would insert a product into the database. The first part of the below example is the variable data and the second half is the GraphQL mutation. For mutations data has to always ben passed as a variable. -#### Querying the GQL endpoint +```json +{ + "data": { + "name": "Art of Computer Programming", + "description": "The Art of Computer Programming (TAOCP) is a comprehensive monograph written by computer scientist Donald Knuth", + "price": 30.5 + } +} +``` -```bash +```graphql +mutation { + product(insert: $data) { + id + name + } +} +``` -# fetch the response json directly from the endpoint using user id 5 -curl 'http://localhost:8080/api/v1/graphql' \ - -H 'content-type: application/json' \ - -H 'X-User-ID: 5' \ - --data-binary '{"query":"{ products { name price users { email }}}"}' +## Why Super Graph + +Let's take a simple example say you want to fetch 5 products priced over 12 dollars along with the photos of the products and the users that owns them. Additionally also fetch the last 10 of your own purchases along with the name and ID of the product you purchased. This is a common type of query to render a view in say an ecommerce app. Lets be honest it's not very exciting write and maintain. Keep in mind the data needed will only continue to grow and change as your app evolves. Developers might find that most ORMs will not be able to do all of this in a single SQL query and will require n+1 queries to fetch all the data and assembly it into the right JSON response. + +What if I told you Super Graph will fetch all this data with a single SQL query and without you having to write a single line of code. Also as your app evolves feel free to evolve the query as you like. In our experience Super Graph saves us hundreds or thousands of man hours that we can put towards the more exciting parts of our app. + +#### GraphQL Query + +```graphql +query { + products(limit 5, where: { price: { gt: 12 } }) { + id + name + description + price + photos { + url + } + user { + id + email + picture : avatar + full_name + } + } + purchases( + limit 10, + order_by: { created_at: desc } , + where: { user_id: { eq: $user_id } } + ) { + id + created_at + product { + id + name + } + } +} +``` + +#### JSON Result + +```json + + "data": { + "products": [ + { + "id": 1, + "name": "Oaked Arrogant Bastard Ale", + "description": "Coors lite, European Amber Lager, Perle, 1272 - American Ale II, 38 IBU, 6.4%, 9.7°Blg", + "price": 20, + "photos: [{ + "url": "https://www.scienceworld.ca/wp-content/uploads/science-world-beer-flavours.jpg" + }], + "user": { + "id": 1, + "email": "user0@demo.com", + "picture": "https://robohash.org/sitaliquamquaerat.png?size=300x300&set=set1", + "full_name": "Mrs. Wilhemina Hilpert" + } + }, + ... + ] + }, + "purchases": [ + { + "id": 5, + "created_at": "2020-01-24T05:34:39.880599", + "product": { + "id": 45, + "name": "Brooklyn Black", + } + }, + ... + ] +} ``` ## Get Started @@ -1169,18 +1231,20 @@ end ## API Security -One of the the most common questions I get asked if what happens if a user out on the internet issues queries -that we don't want issued. For example how do we stop him from fetching all users or the emails of users. Our answer to this is that it is not an issue as this cannot happen, let me explain. +One of the the most common questions I get asked is what happens if a user out on the internet sends queries +that we don't want run. For example how do we stop him from fetching all users or the emails of users. Our answer to this is that it is not an issue as this cannot happen, let me explain. -Super Graph runs in one of two modes `development` or `production`, this is controlled via the config value `production: false` when it's false it's running in development mode and when true, production. In development mode all the **named** quries (including mutations) you run are saved into the allow list (`./config/allow.list`). I production mode when Super Graph starts only the queries from this allow list file are registered with the database as (prepared statements)[https://stackoverflow.com/questions/8263371/how-can-prepared-statements-protect-from-sql-injection-attacks]. Prepared statements are designed by databases to be fast and secure. They protect against all kinds of sql injection attacks and since they are pre-processed and pre-planned they are much faster to run then raw sql queries. Also there's no GraphQL to SQL compiling happening in production mode which makes your queries lighting fast as they directly goto the database with almost no overhead. +Super Graph runs in one of two modes `development` or `production`, this is controlled via the config value `production: false` when it's false it's running in development mode and when true, production. In development mode all the **named** queries (including mutations) are saved to the allow list `./config/allow.list`. While in production mode when Super Graph starts only the queries from this allow list file are registered with the database as [prepared statements](https://stackoverflow.com/questions/8263371/how-can-prepared-statements-protect-from-sql-injection-attacks). -In short in production only queries listed in the allow list file (`./config/allow.list`) can be used all other queries will be blocked. +Prepared statements are designed by databases to be fast and secure. They protect against all kinds of sql injection attacks and since they are pre-processed and pre-planned they are much faster to run then raw sql queries. Also there's no GraphQL to SQL compiling happening in production mode which makes your queries lighting fast as they are directly sent to the database with almost no overhead. + +In short in production only queries listed in the allow list file `./config/allow.list` can be used, all other queries will be blocked. ::: tip How to think about the allow list? -The allow list file is essentially a list of all your exposed API calls and the data thats passes within them in plain text. It's very easy to build tooling to do things like parsing this file within your tests to ensure fields like `credit_card_no` are not accidently leaked. It's a great way to build compliance tooling and ensure your user data is always safe. +The allow list file is essentially a list of all your exposed API calls and the data that passes within them. It's very easy to build tooling to do things like parsing this file within your tests to ensure fields like `credit_card_no` are not accidently leaked. It's a great way to build compliance tooling and ensure your user data is always safe. ::: -This is an example of a named query `getUserWithProducts` is the name you've given to this query it can be anything you like but should be unique across all you're queries. Only named queries are saved in the allow list in development mode the allow list is not modified in production mode. +This is an example of a named query, `getUserWithProducts` is the name you've given to this query it can be anything you like but should be unique across all you're queries. Only named queries are saved in the allow list in development mode. ```graphql @@ -1201,7 +1265,7 @@ query getUserWithProducts { ## Authentication -You can only have one type of auth enabled. You can either pick Rails or JWT. +You can only have one type of auth enabled either Rails or JWT. ### Ruby on Rails @@ -1269,14 +1333,22 @@ auth: public_key_type: ecdsa #rsa ``` -For JWT tokens we currently support tokens from a provider like Auth0 -or if you have a custom solution then we look for the `user_id` in the -`subject` claim of of the `id token`. If you pick Auth0 then we derive two variables from the token `user_id` and `user_id_provider` for to use in your filters. +For JWT tokens we currently support tokens from a provider like Auth0 or if you have a custom solution then we look for the `user_id` in the `subject` claim of of the `id token`. If you pick Auth0 then we derive two variables from the token `user_id` and `user_id_provider` for to use in your filters. We can get the JWT token either from the `authorization` header where we expect it to be a `bearer` token or if `cookie` is specified then we look there. For validation a `secret` or a public key (ecdsa or rsa) is required. When using public keys they have to be in a PEM format file. +#### Using CURL to test a query + +```bash +# fetch the response json directly from the endpoint using user id 5 +curl 'http://localhost:8080/api/v1/graphql' \ + -H 'content-type: application/json' \ + -H 'X-User-ID: 5' \ + --data-binary '{"query":"{ products { name price users { email }}}"}' +``` + ## Access Control It's common for APIs to control what information they return or insert based on the role of the user. In Super Graph we have two primary roles `user` and `anon` the first for users where a `user_id` is available the latter for users where it's not. diff --git a/go.mod b/go.mod index 266a351..e1342d3 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/dlclark/regexp2 v1.2.0 // indirect github.com/dop251/goja v0.0.0-20190912223329-aa89e6a4c733 + github.com/dvyukov/go-fuzz v0.0.0-20191206100749-a378175e205c // indirect github.com/fsnotify/fsnotify v1.4.7 github.com/garyburd/redigo v1.6.0 github.com/go-sourcemap/sourcemap v2.1.2+incompatible // indirect diff --git a/go.sum b/go.sum index 9449553..fd69149 100644 --- a/go.sum +++ b/go.sum @@ -54,6 +54,8 @@ github.com/dlclark/regexp2 v1.2.0 h1:8sAhBGEM0dRWogWqWyQeIJnxjWO6oIjl8FKqREDsGfk github.com/dlclark/regexp2 v1.2.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= github.com/dop251/goja v0.0.0-20190912223329-aa89e6a4c733 h1:cyNc40Dx5YNEO94idePU8rhVd3dn+sd04Arh0kDBAaw= github.com/dop251/goja v0.0.0-20190912223329-aa89e6a4c733/go.mod h1:Mw6PkjjMXWbTj+nnj4s3QPXq1jaT0s5pC0iFD4+BOAA= +github.com/dvyukov/go-fuzz v0.0.0-20191206100749-a378175e205c h1:/bXaeEuNG6V0HeyEGw11DYLW5BGsOPlcVRIXbHNUWSo= +github.com/dvyukov/go-fuzz v0.0.0-20191206100749-a378175e205c/go.mod h1:11Gm+ccJnvAhCNLlf5+cS9KjtbaD5I5zaZpFMsTHWTw= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/garyburd/redigo v1.6.0 h1:0VruCpn7yAIIu7pWVClQC8wxCJEcG3nyzpMSHKi1PQc= diff --git a/psql/fuzz.go b/psql/fuzz.go new file mode 100644 index 0000000..6b9d8bb --- /dev/null +++ b/psql/fuzz.go @@ -0,0 +1,54 @@ +// +build gofuzz + +package psql + +import ( + "encoding/json" + "github.com/dosco/super-graph/qcode" +) + +var ( + qcompileTest, _ = qcode.NewCompiler(qcode.Config{}) + + schema = getTestSchema() + + vars = NewVariables(map[string]string{ + "admin_account_id": "5", + }) + + pcompileTest = NewCompiler(Config{ + Schema: schema, + Vars: vars, + }) +) + +// FuzzerEntrypoint for Fuzzbuzz +func Fuzz(data []byte) int { + gql := `mutation { + product(insert: $data) { + id + name + user { + id + full_name + email + } + } + }` + + qc, err := qcompileTest.Compile([]byte(gql), "user") + if err != nil { + panic("qcompile can't fail") + } + + vars := map[string]json.RawMessage{ + "data": json.RawMessage(data), + } + + _, _, err = pcompileTest.CompileEx(qc, vars) + if err != nil { + return 0 + } + + return 1 +} diff --git a/psql/insert.go b/psql/insert.go index 60b311c..6e19e14 100644 --- a/psql/insert.go +++ b/psql/insert.go @@ -15,7 +15,10 @@ func (c *compilerContext) renderInsert(qc *qcode.QCode, w io.Writer, insert, ok := vars[qc.ActionVar] if !ok { - return 0, fmt.Errorf("Variable '%s' not !defined", qc.ActionVar) + return 0, fmt.Errorf("variable '%s' not defined", qc.ActionVar) + } + if len(insert) == 0 { + return 0, fmt.Errorf("variable '%s' is empty", qc.ActionVar) } io.WriteString(c.w, `WITH "_sg_input" AS (SELECT '{{`) diff --git a/psql/mutate.go b/psql/mutate.go index d7ef176..eab9b5c 100644 --- a/psql/mutate.go +++ b/psql/mutate.go @@ -446,7 +446,10 @@ func (c *compilerContext) renderUpsert(qc *qcode.QCode, w io.Writer, upsert, ok := vars[qc.ActionVar] if !ok { - return 0, fmt.Errorf("Variable '%s' not defined", qc.ActionVar) + return 0, fmt.Errorf("variable '%s' not defined", qc.ActionVar) + } + if len(upsert) == 0 { + return 0, fmt.Errorf("variable '%s' is empty", qc.ActionVar) } if ti.PrimaryCol == nil { diff --git a/psql/psql_test.go b/psql/psql_test.go index 2146d46..be03255 100644 --- a/psql/psql_test.go +++ b/psql/psql_test.go @@ -3,7 +3,6 @@ package psql import ( "log" "os" - "strings" "testing" "github.com/dosco/super-graph/qcode" @@ -128,97 +127,7 @@ func TestMain(m *testing.M) { log.Fatal(err) } - tables := []DBTable{ - DBTable{Name: "customers", Type: "table"}, - DBTable{Name: "users", Type: "table"}, - DBTable{Name: "products", Type: "table"}, - DBTable{Name: "purchases", Type: "table"}, - DBTable{Name: "tags", Type: "table"}, - DBTable{Name: "tag_count", Type: "json"}, - } - - columns := [][]DBColumn{ - []DBColumn{ - DBColumn{ID: 1, Name: "id", Type: "bigint", NotNull: true, PrimaryKey: true, UniqueKey: true}, - DBColumn{ID: 2, Name: "full_name", Type: "character varying", NotNull: true, PrimaryKey: false, UniqueKey: false}, - DBColumn{ID: 3, Name: "phone", Type: "character varying", NotNull: false, PrimaryKey: false, UniqueKey: false}, - DBColumn{ID: 4, Name: "email", Type: "character varying", NotNull: true, PrimaryKey: false, UniqueKey: false}, - DBColumn{ID: 5, Name: "encrypted_password", Type: "character varying", NotNull: true, PrimaryKey: false, UniqueKey: false}, - DBColumn{ID: 6, Name: "reset_password_token", Type: "character varying", NotNull: false, PrimaryKey: false, UniqueKey: false}, - DBColumn{ID: 7, Name: "reset_password_sent_at", Type: "timestamp without time zone", NotNull: false, PrimaryKey: false, UniqueKey: false}, - DBColumn{ID: 8, Name: "remember_created_at", Type: "timestamp without time zone", NotNull: false, PrimaryKey: false, UniqueKey: false}, - DBColumn{ID: 9, Name: "created_at", Type: "timestamp without time zone", NotNull: true, PrimaryKey: false, UniqueKey: false}, - DBColumn{ID: 10, Name: "updated_at", Type: "timestamp without time zone", NotNull: true, PrimaryKey: false, UniqueKey: false}}, - []DBColumn{ - DBColumn{ID: 1, Name: "id", Type: "bigint", NotNull: true, PrimaryKey: true, UniqueKey: true}, - DBColumn{ID: 2, Name: "full_name", Type: "character varying", NotNull: true, PrimaryKey: false, UniqueKey: false}, - DBColumn{ID: 3, Name: "phone", Type: "character varying", NotNull: false, PrimaryKey: false, UniqueKey: false}, - DBColumn{ID: 4, Name: "avatar", Type: "character varying", NotNull: false, PrimaryKey: false, UniqueKey: false}, - DBColumn{ID: 5, Name: "email", Type: "character varying", NotNull: true, PrimaryKey: false, UniqueKey: false}, - DBColumn{ID: 6, Name: "encrypted_password", Type: "character varying", NotNull: true, PrimaryKey: false, UniqueKey: false}, - DBColumn{ID: 7, Name: "reset_password_token", Type: "character varying", NotNull: false, PrimaryKey: false, UniqueKey: false}, - DBColumn{ID: 8, Name: "reset_password_sent_at", Type: "timestamp without time zone", NotNull: false, PrimaryKey: false, UniqueKey: false}, - DBColumn{ID: 9, Name: "remember_created_at", Type: "timestamp without time zone", NotNull: false, PrimaryKey: false, UniqueKey: false}, - DBColumn{ID: 10, Name: "created_at", Type: "timestamp without time zone", NotNull: true, PrimaryKey: false, UniqueKey: false}, - DBColumn{ID: 11, Name: "updated_at", Type: "timestamp without time zone", NotNull: true, PrimaryKey: false, UniqueKey: false}}, - []DBColumn{ - DBColumn{ID: 1, Name: "id", Type: "bigint", NotNull: true, PrimaryKey: true, UniqueKey: true}, - DBColumn{ID: 2, Name: "name", Type: "character varying", NotNull: false, PrimaryKey: false, UniqueKey: false}, - DBColumn{ID: 3, Name: "description", Type: "text", NotNull: false, PrimaryKey: false, UniqueKey: false}, - DBColumn{ID: 4, Name: "price", Type: "numeric(7,2)", NotNull: false, PrimaryKey: false, UniqueKey: false}, - DBColumn{ID: 5, Name: "user_id", Type: "bigint", NotNull: false, PrimaryKey: false, UniqueKey: false, FKeyTable: "users", FKeyColID: []int16{1}}, - DBColumn{ID: 6, Name: "created_at", Type: "timestamp without time zone", NotNull: true, PrimaryKey: false, UniqueKey: false}, - DBColumn{ID: 7, Name: "updated_at", Type: "timestamp without time zone", NotNull: true, PrimaryKey: false, UniqueKey: false}, - DBColumn{ID: 8, Name: "tsv", Type: "tsvector", NotNull: false, PrimaryKey: false, UniqueKey: false}, - DBColumn{ID: 9, Name: "tags", Type: "text[]", NotNull: false, PrimaryKey: false, UniqueKey: false, FKeyTable: "tags", FKeyColID: []int16{3}, Array: true}, - DBColumn{ID: 9, Name: "tag_count", Type: "json", NotNull: false, PrimaryKey: false, UniqueKey: false, FKeyTable: "tag_count", FKeyColID: []int16{}}}, - []DBColumn{ - DBColumn{ID: 1, Name: "id", Type: "bigint", NotNull: true, PrimaryKey: true, UniqueKey: true}, - DBColumn{ID: 2, Name: "customer_id", Type: "bigint", NotNull: false, PrimaryKey: false, UniqueKey: false, FKeyTable: "customers", FKeyColID: []int16{1}}, - DBColumn{ID: 3, Name: "product_id", Type: "bigint", NotNull: false, PrimaryKey: false, UniqueKey: false, FKeyTable: "products", FKeyColID: []int16{1}}, - DBColumn{ID: 4, Name: "sale_type", Type: "character varying", NotNull: false, PrimaryKey: false, UniqueKey: false}, - DBColumn{ID: 5, Name: "quantity", Type: "integer", NotNull: false, PrimaryKey: false, UniqueKey: false}, - DBColumn{ID: 6, Name: "due_date", Type: "timestamp without time zone", NotNull: false, PrimaryKey: false, UniqueKey: false}, - DBColumn{ID: 7, Name: "returned", Type: "timestamp without time zone", NotNull: false, PrimaryKey: false, UniqueKey: false}}, - []DBColumn{ - DBColumn{ID: 1, Name: "id", Type: "bigint", NotNull: true, PrimaryKey: true, UniqueKey: true}, - DBColumn{ID: 2, Name: "name", Type: "text", NotNull: false, PrimaryKey: false, UniqueKey: false}, - DBColumn{ID: 3, Name: "slug", Type: "text", NotNull: false, PrimaryKey: false, UniqueKey: false}}, - []DBColumn{ - DBColumn{ID: 1, Name: "tag_id", Type: "bigint", NotNull: false, PrimaryKey: false, UniqueKey: false, FKeyTable: "tags", FKeyColID: []int16{1}}, - DBColumn{ID: 2, Name: "count", Type: "int", NotNull: false, PrimaryKey: false, UniqueKey: false}}, - } - - for i := range tables { - tables[i].Key = strings.ToLower(tables[i].Name) - for n := range columns[i] { - columns[i][n].Key = strings.ToLower(columns[i][n].Name) - } - } - - schema := &DBSchema{ - ver: 110000, - t: make(map[string]*DBTableInfo), - rm: make(map[string]map[string]*DBRel), - } - - aliases := map[string][]string{ - "users": []string{"mes"}, - } - - for i, t := range tables { - err := schema.addTable(t, columns[i], aliases) - if err != nil { - log.Fatal(err) - } - } - - for i, t := range tables { - err := schema.updateRelationships(t, columns[i]) - if err != nil { - log.Fatal(err) - } - } + schema := getTestSchema() vars := NewVariables(map[string]string{ "admin_account_id": "5", diff --git a/psql/query.go b/psql/query.go index c76f02e..73dd9d0 100644 --- a/psql/query.go +++ b/psql/query.go @@ -825,13 +825,11 @@ func (c *compilerContext) renderFrom(sel *qcode.Select, ti *DBTableInfo, rel *DB } func (c *compilerContext) renderOrderByColumns(sel *qcode.Select, ti *DBTableInfo) { - colsRendered := len(sel.Cols) != 0 + //colsRendered := len(sel.Cols) != 0 for i := range sel.OrderBy { - if colsRendered { - //io.WriteString(w, ", ") - io.WriteString(c.w, `, `) - } + //io.WriteString(w, ", ") + io.WriteString(c.w, `, `) col := sel.OrderBy[i].Col //fmt.Fprintf(w, `"%s_%d"."%s" AS "%s_%d_%s_ob"`, diff --git a/psql/test_schema.go b/psql/test_schema.go new file mode 100644 index 0000000..235e8ca --- /dev/null +++ b/psql/test_schema.go @@ -0,0 +1,102 @@ +package psql + +import ( + "log" + "strings" +) + +func getTestSchema() *DBSchema { + tables := []DBTable{ + DBTable{Name: "customers", Type: "table"}, + DBTable{Name: "users", Type: "table"}, + DBTable{Name: "products", Type: "table"}, + DBTable{Name: "purchases", Type: "table"}, + DBTable{Name: "tags", Type: "table"}, + DBTable{Name: "tag_count", Type: "json"}, + } + + columns := [][]DBColumn{ + []DBColumn{ + DBColumn{ID: 1, Name: "id", Type: "bigint", NotNull: true, PrimaryKey: true, UniqueKey: true}, + DBColumn{ID: 2, Name: "full_name", Type: "character varying", NotNull: true, PrimaryKey: false, UniqueKey: false}, + DBColumn{ID: 3, Name: "phone", Type: "character varying", NotNull: false, PrimaryKey: false, UniqueKey: false}, + DBColumn{ID: 4, Name: "email", Type: "character varying", NotNull: true, PrimaryKey: false, UniqueKey: false}, + DBColumn{ID: 5, Name: "encrypted_password", Type: "character varying", NotNull: true, PrimaryKey: false, UniqueKey: false}, + DBColumn{ID: 6, Name: "reset_password_token", Type: "character varying", NotNull: false, PrimaryKey: false, UniqueKey: false}, + DBColumn{ID: 7, Name: "reset_password_sent_at", Type: "timestamp without time zone", NotNull: false, PrimaryKey: false, UniqueKey: false}, + DBColumn{ID: 8, Name: "remember_created_at", Type: "timestamp without time zone", NotNull: false, PrimaryKey: false, UniqueKey: false}, + DBColumn{ID: 9, Name: "created_at", Type: "timestamp without time zone", NotNull: true, PrimaryKey: false, UniqueKey: false}, + DBColumn{ID: 10, Name: "updated_at", Type: "timestamp without time zone", NotNull: true, PrimaryKey: false, UniqueKey: false}}, + []DBColumn{ + DBColumn{ID: 1, Name: "id", Type: "bigint", NotNull: true, PrimaryKey: true, UniqueKey: true}, + DBColumn{ID: 2, Name: "full_name", Type: "character varying", NotNull: true, PrimaryKey: false, UniqueKey: false}, + DBColumn{ID: 3, Name: "phone", Type: "character varying", NotNull: false, PrimaryKey: false, UniqueKey: false}, + DBColumn{ID: 4, Name: "avatar", Type: "character varying", NotNull: false, PrimaryKey: false, UniqueKey: false}, + DBColumn{ID: 5, Name: "email", Type: "character varying", NotNull: true, PrimaryKey: false, UniqueKey: false}, + DBColumn{ID: 6, Name: "encrypted_password", Type: "character varying", NotNull: true, PrimaryKey: false, UniqueKey: false}, + DBColumn{ID: 7, Name: "reset_password_token", Type: "character varying", NotNull: false, PrimaryKey: false, UniqueKey: false}, + DBColumn{ID: 8, Name: "reset_password_sent_at", Type: "timestamp without time zone", NotNull: false, PrimaryKey: false, UniqueKey: false}, + DBColumn{ID: 9, Name: "remember_created_at", Type: "timestamp without time zone", NotNull: false, PrimaryKey: false, UniqueKey: false}, + DBColumn{ID: 10, Name: "created_at", Type: "timestamp without time zone", NotNull: true, PrimaryKey: false, UniqueKey: false}, + DBColumn{ID: 11, Name: "updated_at", Type: "timestamp without time zone", NotNull: true, PrimaryKey: false, UniqueKey: false}}, + []DBColumn{ + DBColumn{ID: 1, Name: "id", Type: "bigint", NotNull: true, PrimaryKey: true, UniqueKey: true}, + DBColumn{ID: 2, Name: "name", Type: "character varying", NotNull: false, PrimaryKey: false, UniqueKey: false}, + DBColumn{ID: 3, Name: "description", Type: "text", NotNull: false, PrimaryKey: false, UniqueKey: false}, + DBColumn{ID: 4, Name: "price", Type: "numeric(7,2)", NotNull: false, PrimaryKey: false, UniqueKey: false}, + DBColumn{ID: 5, Name: "user_id", Type: "bigint", NotNull: false, PrimaryKey: false, UniqueKey: false, FKeyTable: "users", FKeyColID: []int16{1}}, + DBColumn{ID: 6, Name: "created_at", Type: "timestamp without time zone", NotNull: true, PrimaryKey: false, UniqueKey: false}, + DBColumn{ID: 7, Name: "updated_at", Type: "timestamp without time zone", NotNull: true, PrimaryKey: false, UniqueKey: false}, + DBColumn{ID: 8, Name: "tsv", Type: "tsvector", NotNull: false, PrimaryKey: false, UniqueKey: false}, + DBColumn{ID: 9, Name: "tags", Type: "text[]", NotNull: false, PrimaryKey: false, UniqueKey: false, FKeyTable: "tags", FKeyColID: []int16{3}, Array: true}, + DBColumn{ID: 9, Name: "tag_count", Type: "json", NotNull: false, PrimaryKey: false, UniqueKey: false, FKeyTable: "tag_count", FKeyColID: []int16{}}}, + []DBColumn{ + DBColumn{ID: 1, Name: "id", Type: "bigint", NotNull: true, PrimaryKey: true, UniqueKey: true}, + DBColumn{ID: 2, Name: "customer_id", Type: "bigint", NotNull: false, PrimaryKey: false, UniqueKey: false, FKeyTable: "customers", FKeyColID: []int16{1}}, + DBColumn{ID: 3, Name: "product_id", Type: "bigint", NotNull: false, PrimaryKey: false, UniqueKey: false, FKeyTable: "products", FKeyColID: []int16{1}}, + DBColumn{ID: 4, Name: "sale_type", Type: "character varying", NotNull: false, PrimaryKey: false, UniqueKey: false}, + DBColumn{ID: 5, Name: "quantity", Type: "integer", NotNull: false, PrimaryKey: false, UniqueKey: false}, + DBColumn{ID: 6, Name: "due_date", Type: "timestamp without time zone", NotNull: false, PrimaryKey: false, UniqueKey: false}, + DBColumn{ID: 7, Name: "returned", Type: "timestamp without time zone", NotNull: false, PrimaryKey: false, UniqueKey: false}}, + []DBColumn{ + DBColumn{ID: 1, Name: "id", Type: "bigint", NotNull: true, PrimaryKey: true, UniqueKey: true}, + DBColumn{ID: 2, Name: "name", Type: "text", NotNull: false, PrimaryKey: false, UniqueKey: false}, + DBColumn{ID: 3, Name: "slug", Type: "text", NotNull: false, PrimaryKey: false, UniqueKey: false}}, + []DBColumn{ + DBColumn{ID: 1, Name: "tag_id", Type: "bigint", NotNull: false, PrimaryKey: false, UniqueKey: false, FKeyTable: "tags", FKeyColID: []int16{1}}, + DBColumn{ID: 2, Name: "count", Type: "int", NotNull: false, PrimaryKey: false, UniqueKey: false}}, + } + + for i := range tables { + tables[i].Key = strings.ToLower(tables[i].Name) + for n := range columns[i] { + columns[i][n].Key = strings.ToLower(columns[i][n].Name) + } + } + + schema := &DBSchema{ + ver: 110000, + t: make(map[string]*DBTableInfo), + rm: make(map[string]map[string]*DBRel), + } + + aliases := map[string][]string{ + "users": []string{"mes"}, + } + + for i, t := range tables { + err := schema.addTable(t, columns[i], aliases) + if err != nil { + log.Fatal(err) + } + } + + for i, t := range tables { + err := schema.updateRelationships(t, columns[i]) + if err != nil { + log.Fatal(err) + } + } + + return schema +} diff --git a/psql/update.go b/psql/update.go index 9b915ca..b4ddaac 100644 --- a/psql/update.go +++ b/psql/update.go @@ -15,7 +15,10 @@ func (c *compilerContext) renderUpdate(qc *qcode.QCode, w io.Writer, update, ok := vars[qc.ActionVar] if !ok { - return 0, fmt.Errorf("Variable '%s' not !defined", qc.ActionVar) + return 0, fmt.Errorf("variable '%s' not !defined", qc.ActionVar) + } + if len(update) == 0 { + return 0, fmt.Errorf("variable '%s' is empty", qc.ActionVar) } io.WriteString(c.w, `WITH "_sg_input" AS (SELECT '{{`) diff --git a/qcode/fuzz.go b/qcode/fuzz.go index 630bba6..3ea0fe3 100644 --- a/qcode/fuzz.go +++ b/qcode/fuzz.go @@ -4,7 +4,11 @@ package qcode // FuzzerEntrypoint for Fuzzbuzz func Fuzz(data []byte) int { - GetQType(string(data)) + qt := GetQType(string(data)) + + if qt > QTUpsert { + panic("qt > QTUpsert") + } qcompile, _ := NewCompiler(Config{}) _, err := qcompile.Compile(data, "user")