From 1131adae1c5c3642566566ff28d0bd07ac9a5306 Mon Sep 17 00:00:00 2001 From: Chaitanya Deshpande Date: Thu, 26 Nov 2026 17:05:11 +0530 Subject: [PATCH 1/2] fix(db): keep assets without the field in a DSL exclusion An asset DSL exclusion compiled to `col NOT ILIKE $n` for text fields and `col != $n` for numeric ones. Both are NULL when the asset has no value for the field, and WHERE treats NULL as false, so `page_title!=login` or `status_code!=404` also dropped every asset with no title or no status code, from both the page and its count. Compile `!=` to `(col IS NULL OR col NOT ILIKE $n)` for text fields and to `col IS DISTINCT FROM $n` for numeric fields. Array fields are NOT NULL and already behave. --- db/asset_dsl.go | 8 +++++- db/asset_dsl_not_null_test.go | 53 +++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 db/asset_dsl_not_null_test.go diff --git a/db/asset_dsl.go b/db/asset_dsl.go index 2f99466b..0ab77156 100644 --- a/db/asset_dsl.go +++ b/db/asset_dsl.go @@ -361,6 +361,10 @@ func (b *whereBuilder) buildLeaf(e Expr) (string, error) { if op != "=" && op != "!=" && op != ">" && op != ">=" && op != "<" && op != "<=" { return "", fmt.Errorf("field %s does not support operator %s", f, e.Op) } + if op == "!=" { + // An asset without the field does not have the excluded value. + op = "IS DISTINCT FROM" + } return fmt.Sprintf("%s %s %s", col, op, b.next(n)), nil } @@ -387,7 +391,9 @@ func (b *whereBuilder) buildLeaf(e Expr) (string, error) { case "==": return col + " = " + b.next(e.Value), nil case "!=": - return col + " NOT ILIKE " + b.next("%"+e.Value+"%"), nil + // NOT ILIKE on NULL is NULL, which would also drop the assets that + // have no value for the field; they do not match, so keep them. + return "(" + col + " IS NULL OR " + col + " NOT ILIKE " + b.next("%"+e.Value+"%") + ")", nil default: return "", fmt.Errorf("string field %s does not support operator %s", f, e.Op) } diff --git a/db/asset_dsl_not_null_test.go b/db/asset_dsl_not_null_test.go new file mode 100644 index 00000000..86aeb1ab --- /dev/null +++ b/db/asset_dsl_not_null_test.go @@ -0,0 +1,53 @@ +package db + +import ( + "fmt" + "testing" + "time" +) + +// An exclusion (field!=value) removes the assets whose field matches value. An +// asset that has no value for the field does not match, so it must stay in the +// result: SQL's NOT ILIKE / != against NULL yields NULL, which WHERE treats as +// false, so a bare comparison would drop it. +func TestQueryDSLExclusionKeepsAssetsWithoutTheField(t *testing.T) { + d, assets, _ := testSetup(t) + t.Cleanup(func() { d.Close() }) + + stamp := time.Now().UnixNano() + marker := fmt.Sprintf("excl%d.invalid", stamp) + + var titled, untitled int64 + if err := d.QueryRow(`INSERT INTO assets(type,url,service_type,domain,root_domain,page_title,status_code,last_seen) + VALUES ('service',$1,'http',$2,$2,'Login',404,now()) RETURNING id`, "http://a."+marker+"/", "a."+marker).Scan(&titled); err != nil { + t.Fatal(err) + } + if err := d.QueryRow(`INSERT INTO assets(type,url,service_type,domain,root_domain,last_seen) + VALUES ('service',$1,'http',$2,$2,now()) RETURNING id`, "http://b."+marker+"/", "b."+marker).Scan(&untitled); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { + cleanupExec(t, d, `DELETE FROM assets WHERE id=ANY($1::bigint[])`, []int64{titled, untitled}) + }) + + for _, dsl := range []string{ + "domain=" + marker + " AND page_title!=login", + "domain=" + marker + " AND status_code!=404", + } { + got, err := assets.QueryDSL(dsl, "service", 0, 50, 0) + if err != nil { + t.Fatalf("%s: %v", dsl, err) + } + n, err := assets.CountDSL(dsl, "service", 0) + if err != nil { + t.Fatalf("%s: %v", dsl, err) + } + ids := []int64{} + for _, a := range got { + ids = append(ids, a.ID) + } + if len(got) != 1 || got[0].ID != untitled || n != 1 { + t.Errorf("%s: got ids %v count %d, want only the asset without the field (%d) and count 1", dsl, ids, n, untitled) + } + } +} From c9116e393855b5d0b8724438a7951e2b6d767d16 Mon Sep 17 00:00:00 2001 From: Chaitanya Deshpande Date: Thu, 26 Nov 2026 17:13:12 +0530 Subject: [PATCH 2/2] fix(db): honour != on company_id and task_id in the asset DSL company_id and task_id ignored the operator, so company_id!=N and task_id!=N compiled to the equality and returned exactly the assets they were meant to exclude, and company_id>N silently meant =. != now excludes (an asset without a company is kept, as for the other fields), = and == stay equality, and any other operator is rejected like on the other fields. --- db/asset_dsl.go | 19 ++++++++++-- db/asset_dsl_not_null_test.go | 57 +++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/db/asset_dsl.go b/db/asset_dsl.go index 0ab77156..e8c5e4bd 100644 --- a/db/asset_dsl.go +++ b/db/asset_dsl.go @@ -336,7 +336,14 @@ func (b *whereBuilder) buildLeaf(e Expr) (string, error) { if err != nil { return "", fmt.Errorf("task_id requires an integer value: %s", e.Value) } - return b.next(n) + " = ANY(task_ids)", nil + switch e.Op { + case "=", "==": + return b.next(n) + " = ANY(task_ids)", nil + case "!=": + return "NOT (" + b.next(n) + " = ANY(task_ids))", nil + default: + return "", fmt.Errorf("field %s does not support operator %s", f, e.Op) + } } // company_id: exact integer @@ -345,7 +352,15 @@ func (b *whereBuilder) buildLeaf(e Expr) (string, error) { if err != nil { return "", fmt.Errorf("company_id requires an integer value: %s", e.Value) } - return "company_id = " + b.next(n), nil + switch e.Op { + case "=", "==": + return "company_id = " + b.next(n), nil + case "!=": + // An asset without a company is not in the excluded company. + return "company_id IS DISTINCT FROM " + b.next(n), nil + default: + return "", fmt.Errorf("field %s does not support operator %s", f, e.Op) + } } // numeric fields diff --git a/db/asset_dsl_not_null_test.go b/db/asset_dsl_not_null_test.go index 86aeb1ab..7fe1b9a9 100644 --- a/db/asset_dsl_not_null_test.go +++ b/db/asset_dsl_not_null_test.go @@ -51,3 +51,60 @@ func TestQueryDSLExclusionKeepsAssetsWithoutTheField(t *testing.T) { } } } + +// company_id!=N and task_id!=N exclude that company's or task's assets; they +// used to compile to the equality and return exactly the excluded assets. +func TestQueryDSLExclusionOnCompanyAndTask(t *testing.T) { + d, assets, _ := testSetup(t) + t.Cleanup(func() { d.Close() }) + + stamp := time.Now().UnixNano() + marker := fmt.Sprintf("exclid%d.invalid", stamp) + var companyID int64 + if err := d.QueryRow(`INSERT INTO companies(name,nkey) VALUES ($1,$1) RETURNING id`, marker).Scan(&companyID); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { cleanupExec(t, d, `DELETE FROM companies WHERE id=$1`, companyID) }) + taskID := stamp % 1_000_000_000 + + var owned, unowned int64 + if err := d.QueryRow(`INSERT INTO assets(type,domain,root_domain,company_id,task_ids) + VALUES ('subdomain',$1,$2,$3,ARRAY[$4]::bigint[]) RETURNING id`, "a."+marker, marker, companyID, taskID).Scan(&owned); err != nil { + t.Fatal(err) + } + if err := d.QueryRow(`INSERT INTO assets(type,domain,root_domain) + VALUES ('subdomain',$1,$2) RETURNING id`, "b."+marker, marker).Scan(&unowned); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { + cleanupExec(t, d, `DELETE FROM assets WHERE id=ANY($1::bigint[])`, []int64{owned, unowned}) + }) + + for _, c := range []struct { + dsl string + want int64 + }{ + {fmt.Sprintf("root_domain==%s AND company_id!=%d", marker, companyID), unowned}, + {fmt.Sprintf("root_domain==%s AND task_id!=%d", marker, taskID), unowned}, + {fmt.Sprintf("root_domain==%s AND company_id==%d", marker, companyID), owned}, + {fmt.Sprintf("root_domain==%s AND task_id=%d", marker, taskID), owned}, + } { + got, err := assets.QueryDSL(c.dsl, "subdomain", 0, 50, 0) + if err != nil { + t.Fatalf("%s: %v", c.dsl, err) + } + if len(got) != 1 || got[0].ID != c.want { + ids := []int64{} + for _, a := range got { + ids = append(ids, a.ID) + } + t.Errorf("%s: got ids %v, want [%d]", c.dsl, ids, c.want) + } + } + + for _, dsl := range []string{"company_id>1", "task_id<1"} { + if err := ValidateDSL(dsl); err == nil { + t.Errorf("%s: accepted, want an unsupported-operator error", dsl) + } + } +}