CVE-2022-23857: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection') (CWE-89)
value: model/criteria/criteria.go in Navidrome before 0.47.5 is vulnerable to SQL injection attacks when processing crafted Smart Playlists. An authenticated user could abuse this to extract arbitrary data from the database, including the user table (which contains sensitive information such as the users' encrypted passwords).
CWE description: The product constructs all or part of an SQL command using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the intended SQL command when it is sent to a downstream component. Without sufficient removal or quoting of SQL syntax in user-controllable inputs, the generated SQL query can cause those inputs to be interpreted as SQL instead of ordinary user data.
CVSS v3 base score: 6.5
Published: 2022-01-24T02:15Z
Repository: https://github.com/navidrome/navidrome
Language: Go
Files changed: model/criteria/criteria.go, scanner/tag_scanner.go
Commit date: 2022-01-18 21:36:29 -0500
Commit message: Fix potential SQL injection in Smart Playlists

Diff: diff --git a/model/criteria/criteria.go b/model/criteria/criteria.go
index 0d3c7295..c59c392a 100644
--- a/model/criteria/criteria.go
+++ b/model/criteria/criteria.go
@@ -26,21 +26,25 @@ func (c Criteria) OrderBy() string {
 		c.Sort = "title"
 	}
 	f := fieldMap[strings.ToLower(c.Sort)]
 	var mapped string
 	if f == nil {
-		log.Error("Invalid field in 'sort' field", "field", c.Sort)
-		mapped = c.Sort
+		log.Error("Invalid field in 'sort' field. Using 'title'", "sort", c.Sort)
+		mapped = fieldMap["title"].field
 	} else {
 		if f.order == "" {
 			mapped = f.field
 		} else {
 			mapped = f.order
 		}
 	}
 	if c.Order != "" {
-		mapped = mapped + " " + c.Order
+		if strings.EqualFold(c.Order, "asc") || strings.EqualFold(c.Order, "desc") {
+			mapped = mapped + " " + c.Order
+		} else {
+			log.Error("Invalid value in 'order' field. Valid values: 'asc', 'desc'", "order", c.Order)
+		}
 	}
 	return mapped
 }
 
 func (c Criteria) ToSql() (sql string, args []interface{}, err error) {
diff --git a/scanner/tag_scanner.go b/scanner/tag_scanner.go
index 0e0ed7f8..8f022d70 100644
--- a/scanner/tag_scanner.go
+++ b/scanner/tag_scanner.go
@@ -70,11 +70,11 @@ const (
 // Delete all empty albums, delete all empty artists, clean-up playlists
 func (s *TagScanner) Scan(ctx context.Context, lastModifiedSince time.Time, progress chan uint32) (int64, error) {
 	ctx = s.withAdminUser(ctx)
 	start := time.Now()
 
-	// Special case: if lastModifiedSInce is zero, re-import all files
+	// Special case: if lastModifiedSince is zero, re-import all files
 	fullScan := lastModifiedSince.IsZero()
 
 	allDBDirs, err := s.getDBDirTree(ctx)
 	if err != nil {
 		return 0, err

Vulnerable code: 		log.Error("Invalid field in 'sort' field", "field", c.Sort)
		mapped = c.Sort
		mapped = mapped + " " + c.Order
	// Special case: if lastModifiedSInce is zero, re-import all files
Fixed code: 		log.Error("Invalid field in 'sort' field. Using 'title'", "sort", c.Sort)
		mapped = fieldMap["title"].field
		if strings.EqualFold(c.Order, "asc") || strings.EqualFold(c.Order, "desc") {
			mapped = mapped + " " + c.Order
		} else {
			log.Error("Invalid value in 'order' field. Valid values: 'asc', 'desc'", "order", c.Order)
		}
	// Special case: if lastModifiedSince is zero, re-import all files