Skip to content

Commit dc61566

Browse files
committed
fix(ci): flag constraints added to open maps/arrays
childSchemas now recurses into items/additionalProperties whenever head defines a schema, diffing against an empty base when base had none. This catches constraining a previously-unconstrained array element or map value (base absent/true -> schema), while still treating additionalProperties:false -> schema and schema removal as safe relaxations. Also tighten the first-party group check to cozystack.io and its dot-delimited subdomains so a lookalike like fakecozystack.io is not gated. Both covered by tests. Assisted-By: Claude <[email protected]> Signed-off-by: Timofei Larkin <[email protected]>
1 parent f3e55d2 commit dc61566

4 files changed

Lines changed: 88 additions & 27 deletions

File tree

‎internal/apigate/parse_test.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,24 @@ func TestLoadSnapshotEmptyErrors(t *testing.T) {
194194
}
195195
}
196196

197+
// TestIsFirstPartyGroup covers the subdomain check: only cozystack.io and its
198+
// dot-delimited subdomains are first-party; a lookalike suffix is not.
199+
func TestIsFirstPartyGroup(t *testing.T) {
200+
cases := map[string]bool{
201+
"cozystack.io": true,
202+
"apps.cozystack.io": true,
203+
"strategy.backups.cozystack.io": true,
204+
"fakecozystack.io": false,
205+
"cozystack.io.evil.com": false,
206+
"cert-manager.io": false,
207+
}
208+
for group, want := range cases {
209+
if got := isFirstPartyGroup(group); got != want {
210+
t.Errorf("isFirstPartyGroup(%q) = %v, want %v", group, got, want)
211+
}
212+
}
213+
}
214+
197215
// TestLoadSnapshotEndToEnd builds two minimal checkouts on disk and verifies
198216
// the loader + classifier wire together across all three sources.
199217
func TestLoadSnapshotEndToEnd(t *testing.T) {

‎internal/apigate/schemadiff.go‎

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -148,21 +148,29 @@ func diffNode(path string, base, head Schema, out *[]string) {
148148
diffNode(child, baseProps[name], hp, out)
149149
}
150150

151-
// additionalProperties: restricting it to false rejects previously-allowed
152-
// undeclared fields — a breaking change that a boolean value carries and a
153-
// schema recursion would miss. Otherwise recurse only when BOTH sides carry
154-
// a map schema: a child schema present only in base is a relaxation (its
155-
// removal loosens the contract), and diffing it against a nil head would
156-
// misreport that relaxation as a break.
157-
if base["additionalProperties"] != false && head["additionalProperties"] == false {
151+
// additionalProperties governs undeclared fields; its permissiveness runs
152+
// false (none) < schema (matching) < true/absent (any).
153+
// - open -> false: breaking (fields once accepted now rejected)
154+
// - open -> schema: recurse; adding a schema to a previously-open
155+
// map is itself a restriction, caught by diffing
156+
// the new schema against an empty (unconstrained)
157+
// base.
158+
// - base false -> *: relaxation (base accepted nothing), so safe.
159+
// - open -> true/absent: relaxation, safe.
160+
baseOpenAP := base["additionalProperties"] != false
161+
if baseOpenAP && head["additionalProperties"] == false {
158162
*out = append(*out, fmt.Sprintf("%s: additionalProperties restricted to false (undeclared fields no longer accepted)", path))
159-
} else if b, h, ok := bothMapSchemas(base, head, "additionalProperties"); ok {
160-
diffNode(path+"{}", b, h, out)
163+
} else if baseOpenAP {
164+
if b, h, ok := childSchemas(base, head, "additionalProperties"); ok {
165+
diffNode(path+"{}", b, h, out)
166+
}
161167
}
162168

163-
// items: recurse only when both sides define an item schema; removing items
164-
// loosens an array's contract and is safe.
165-
if b, h, ok := bothMapSchemas(base, head, "items"); ok {
169+
// items: recurse whenever head defines an item schema. When base has none,
170+
// the head schema is diffed against an empty base so that constraining a
171+
// previously-unconstrained array element is caught; a base schema with no
172+
// head counterpart is a relaxation and is skipped.
173+
if b, h, ok := childSchemas(base, head, "items"); ok {
166174
diffNode(path+"[]", b, h, out)
167175
}
168176
}
@@ -267,18 +275,25 @@ func schemaProps(s Schema) map[string]Schema {
267275
return out
268276
}
269277

270-
// bothMapSchemas extracts a named child schema (e.g. items, additionalProperties)
271-
// from both nodes, reporting ok only when BOTH sides carry a schema object.
272-
// Requiring both sides is deliberate: a child schema present only in base was
273-
// removed on head, which loosens the contract (safe), and recursing into it
274-
// against a nil head would misreport that relaxation as a break. A boolean
275-
// additionalProperties (true/false) is handled separately by the caller.
276-
func bothMapSchemas(base, head Schema, key string) (Schema, Schema, bool) {
277-
b, bOK := base[key].(map[string]any)
278-
h, hOK := head[key].(map[string]any)
279-
if !bOK || !hOK {
278+
// childSchemas extracts a named child schema (items or additionalProperties)
279+
// for recursion. It reports ok only when HEAD carries a schema object; base's
280+
// counterpart is returned when present and an empty schema otherwise. The
281+
// asymmetry is deliberate:
282+
// - head schema, base schema -> diff them (nested changes).
283+
// - head schema, base none -> diff head against an empty base, so that
284+
// constraining a previously-unconstrained array element / map value
285+
// (base absent or true) surfaces as breaking.
286+
// - head none, base schema -> ok=false: removing a child schema loosens
287+
// the contract (relaxation), so it is skipped.
288+
//
289+
// A boolean additionalProperties (true/false) is not a schema object and is
290+
// handled separately by the caller.
291+
func childSchemas(base, head Schema, key string) (Schema, Schema, bool) {
292+
h, ok := head[key].(map[string]any)
293+
if !ok {
280294
return nil, nil, false
281295
}
296+
b, _ := base[key].(map[string]any) // nil -> empty (unconstrained) base schema
282297
return Schema(b), Schema(h), true
283298
}
284299

‎internal/apigate/schemadiff_test.go‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,27 @@ func TestDiffSchema(t *testing.T) {
204204
base: `{"type":"object","properties":{"m":{"type":"object","additionalProperties":false}}}`,
205205
head: `{"type":"object","properties":{"m":{"type":"object","additionalProperties":true}}}`,
206206
},
207+
{
208+
name: "additionalProperties false to schema is safe (relaxation)",
209+
base: `{"type":"object","properties":{"m":{"type":"object","additionalProperties":false}}}`,
210+
head: `{"type":"object","properties":{"m":{"type":"object","additionalProperties":{"type":"string"}}}}`,
211+
},
212+
// Adding a schema where the map/array element was previously
213+
// unconstrained is a restriction and must be flagged.
214+
{
215+
name: "adding additionalProperties schema to open map is breaking",
216+
base: `{"type":"object","properties":{"m":{"type":"object"}}}`,
217+
head: `{"type":"object","properties":{"m":{"type":"object","additionalProperties":{"type":"string"}}}}`,
218+
breaking: true,
219+
wantSub: "type constraint added",
220+
},
221+
{
222+
name: "adding items schema to unconstrained array is breaking",
223+
base: `{"type":"object","properties":{"list":{"type":"array"}}}`,
224+
head: `{"type":"object","properties":{"list":{"type":"array","items":{"type":"object","properties":{"foo":{"type":"string"}}}}}}`,
225+
breaking: true,
226+
wantSub: "type constraint added",
227+
},
207228
// B7(b): CEL validation rules — additions break, removals are safe.
208229
{
209230
name: "added CEL validation rule is breaking",

‎internal/apigate/snapshot.go‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,17 @@ var crdDirNames = map[string]struct{}{
4949
// apiserverGoFile is the source of the static Go-backed aggregated registrations.
5050
const apiserverGoFile = "pkg/apiserver/apiserver.go"
5151

52-
// crdGroupSuffix restricts discovered CRDs to first-party API groups, so
53-
// vendored upstream CRDs (cert-manager, flux, …) that happen to sit in a crds/
54-
// directory are not gated.
55-
const crdGroupSuffix = "cozystack.io"
52+
// crdGroupBase is the first-party API group family. A discovered CRD is gated
53+
// only when its group is exactly this or a dot-delimited subdomain of it, so
54+
// vendored upstream CRDs (cert-manager, flux, …) sitting in a crds/ directory
55+
// are not gated — and a lookalike such as "fakecozystack.io" is not mistaken
56+
// for first-party.
57+
const crdGroupBase = "cozystack.io"
58+
59+
// isFirstPartyGroup reports whether group belongs to the cozystack.io family.
60+
func isFirstPartyGroup(group string) bool {
61+
return group == crdGroupBase || strings.HasSuffix(group, "."+crdGroupBase)
62+
}
5663

5764
// LoadSnapshot walks a repository checkout rooted at dir and builds the full
5865
// API Snapshot from every source of truth: cozyrds (apps API), CRD manifests
@@ -102,7 +109,7 @@ func LoadSnapshot(dir string) (Snapshot, error) {
102109
continue
103110
}
104111
for _, res := range resources {
105-
if strings.HasSuffix(res.Group, crdGroupSuffix) {
112+
if isFirstPartyGroup(res.Group) {
106113
add(res)
107114
}
108115
}

0 commit comments

Comments
 (0)