Skip to content

🎧 Your Personal Streaming Service

fix(smartplaylist): reject NSP mixing top-level 'any' and 'all' (#5759)

Deluan Quintãoe91687e760a8
diff --git a/core/playlists/parse_nsp_test.go b/core/playlists/parse_nsp_test.go
index 516a5355d..d6d69866f 100644
--- a/core/playlists/parse_nsp_test.go
+++ b/core/playlists/parse_nsp_test.go
@@ -113,6 +113,20 @@ var _ = Describe("parseNSP", func() {
Expect(err.Error()).To(ContainSubstring("SmartPlaylist"))
})
+ It("rejects a NSP that mixes top-level 'any' and 'all' instead of silently dropping a group", func() {
+ nsp := `{
+ "name": "Overplayed Favorites",
+ "any": [{"inPlaylist": {"path": "most-played-favorites.nsp"}}],
+ "all": [{"notInPlaylist": {"path": "favorites-not-played-in-4-yrs.nsp"}}],
+ "sort": "playCount, lastPlayed"
+ }`
+ pls := &model.Playlist{}
+ err := s.parseNSP(ctx, pls, strings.NewReader(nsp))
+ Expect(err).To(HaveOccurred())
+ Expect(err.Error()).To(ContainSubstring("SmartPlaylist"))
+ Expect(err.Error()).To(And(ContainSubstring("all"), ContainSubstring("any")))
+ })
+
It("gracefully handles non-string name field", func() {
nsp := `{"name": 123, "all": [{"is": {"loved": true}}]}`
pls := &model.Playlist{Name: "Original"}
diff --git a/model/criteria/criteria.go b/model/criteria/criteria.go
index 31d208d08..8c3d183a9 100644
--- a/model/criteria/criteria.go
+++ b/model/criteria/criteria.go
@@ -103,21 +103,26 @@ func (c Criteria) MarshalJSON() ([]byte, error) {
func (c *Criteria) UnmarshalJSON(data []byte) error {
var aux struct {
- All unmarshalConjunctionType `json:"all"`
- Any unmarshalConjunctionType `json:"any"`
- Sort string `json:"sort"`
- Order string `json:"order"`
- Limit int `json:"limit"`
- LimitPercent int `json:"limitPercent"`
- Offset int `json:"offset"`
+ All optionalConjunction `json:"all"`
+ Any optionalConjunction `json:"any"`
+ Sort string `json:"sort"`
+ Order string `json:"order"`
+ Limit int `json:"limit"`
+ LimitPercent int `json:"limitPercent"`
+ Offset int `json:"offset"`
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
- if len(aux.Any) > 0 {
- c.Expression = Any(aux.Any)
- } else if len(aux.All) > 0 {
- c.Expression = All(aux.All)
+ // A Criteria has a single top-level group. Reject files that provide both keys
+ // (even when one is [] or null) rather than silently dropping one of them.
+ if aux.All.present && aux.Any.present {
+ return errors.New("invalid criteria json: 'all' and 'any' cannot both be used at the top level; nest one inside the other instead")
+ }
+ if len(aux.Any.rules) > 0 {
+ c.Expression = Any(aux.Any.rules)
+ } else if len(aux.All.rules) > 0 {
+ c.Expression = All(aux.All.rules)
} else {
return errors.New("invalid criteria json. missing rules (key 'all' or 'any')")
}
diff --git a/model/criteria/criteria_test.go b/model/criteria/criteria_test.go
index 092cfd36a..7f214e703 100644
--- a/model/criteria/criteria_test.go
+++ b/model/criteria/criteria_test.go
@@ -80,6 +80,28 @@ var _ = Describe("Criteria", func() {
})
})
+ Context("with both top-level 'all' and 'any'", func() {
+ It("returns an error instead of silently dropping one of the groups", func() {
+ jsonStr := `{"any":[{"inPlaylist":{"path":"a.nsp"}}],"all":[{"notInPlaylist":{"path":"b.nsp"}}]}`
+ var c Criteria
+ err := json.Unmarshal([]byte(jsonStr), &c)
+ gomega.Expect(err).To(gomega.HaveOccurred())
+ gomega.Expect(err.Error()).To(gomega.And(gomega.ContainSubstring("all"), gomega.ContainSubstring("any")))
+ })
+
+ DescribeTable("rejects both keys even when one group is present but empty",
+ func(jsonStr string) {
+ var c Criteria
+ err := json.Unmarshal([]byte(jsonStr), &c)
+ gomega.Expect(err).To(gomega.HaveOccurred())
+ gomega.Expect(err.Error()).To(gomega.And(gomega.ContainSubstring("all"), gomega.ContainSubstring("any")))
+ },
+ Entry("empty any", `{"any":[],"all":[{"is":{"loved":true}}]}`),
+ Entry("empty all", `{"all":[],"any":[{"is":{"loved":true}}]}`),
+ Entry("null any", `{"any":null,"all":[{"is":{"loved":true}}]}`),
+ )
+ })
+
Describe("LimitPercent", func() {
Describe("JSON round-trip", func() {
It("marshals and unmarshals limitPercent", func() {
diff --git a/model/criteria/json.go b/model/criteria/json.go
index beded9d1f..d0f453524 100644
--- a/model/criteria/json.go
+++ b/model/criteria/json.go
@@ -33,6 +33,20 @@ func (uc *unmarshalConjunctionType) UnmarshalJSON(data []byte) error {
return nil
}
+// optionalConjunction is a top-level "all"/"any" value that remembers whether its
+// key was present at all, so a Criteria providing both can be rejected. encoding/json
+// calls UnmarshalJSON even for a JSON null, so present is set whenever the key appears
+// — including as [] or null — while an absent key leaves it false.
+type optionalConjunction struct {
+ present bool
+ rules unmarshalConjunctionType
+}
+
+func (o *optionalConjunction) UnmarshalJSON(data []byte) error {
+ o.present = true
+ return json.Unmarshal(data, &o.rules)
+}
+
func unmarshalExpression(opName string, rawValue json.RawMessage) Expression {
m := make(map[string]any)
err := json.Unmarshal(rawValue, &m)