GitHub Security Advisory: GHSA-p3x5-mvmp-5f35
Package: github.com/canonical/lxd (GO)
Severity: MODERATE (CVSS 0.0)
CWE: CWE-200: Exposure of Sensitive Information to an Unauthorized Actor
Vulnerable versions: >= 4.0, < 5.21.4
Patched in: 5.21.4

Summary: Canonical LXD Project Existence Determination Through Error Handling in Image Export Function

Details:
### Impact
In LXD's images export API (`/1.0/images/{fingerprint}/export`), implementation differences in error handling allow determining project existence without authentication.

Specifically, in the following code, errors when multiple images match are directly returned to users as API responses:

https://github.com/canonical/lxd/blob/43d5189564d27f6161b430ed258c8b56603c2759/lxd/db/images.go#L239-L246

While fingerprints generally don't duplicate, this functionality uses fingerprints with LIKE clauses, allowing prefix specification. Therefore, using LIKE wildcards such as % will match multiple images if multiple images exist in the project.

https://github.com/canonical/lxd/blob/43d5189564d27f6161b430ed258c8b56603c2759/lxd/db/images.go#L277-L286

In the above implementation, multiple matches result in a 500 error, but if the project itself doesn't exist, there are 0 matches and a 404 is returned.

1. When project exists and multiple images match: HTTP 500 error "More than one image matches"
2. When project doesn't exist: HTTP 404 error "not found" 
 
This behavioural difference allows attackers to confirm project existence without authentication.

### Reproduction Steps
1. Send a request with a pattern matching multiple entries to an existing project (default):

```
curl -k 'https://lxd-host:8443/1.0/images/%25/export?project=default&secret=x'
```

Response:

```json
{"type":"error","status":"","status_code":0,"operation":"","error_code":500,"error":"More than one image matches","metadata":null}
```

2. Send a same request to a non-existent project (not-exist):

```
curl -k 'https://lxd-host:8443/1.0/images/%25/export?project=not-exist&secret=x'
```

Response:

```json
{"type":"error","status":"","status_code":0,"operation":"","error_code":404,"error":"not found","metadata":null}
```

This difference allows enumerating existing projects in the system by brute-forcing(dictionary attack) project names.

Note that `%25` is the URL encoding of `%`, which works as a wildcard matching all characters in SQL LIKE clauses. 
This is used to intentionally create requests matching multiple images to trigger a 500 error.

Additionally, the secret parameter is added to include non-public images in the search, increasing the possibility of multiple matches.

https://github.com/canonical/lxd/blob/43d5189564d27f6161b430ed258c8b56603c2759/lxd/images.go#L4211-L4230

### Risk
The attack requires only network access to the LXD API endpoint, with no authentication needed.

The attack allows confirming the existence of projects within the LXD system by exploiting differences in HTTP status codes. This could potentially increase the exploitability of other vulnerabilities. Additionally, since project IDs often use meaningful names set by users, this could lead to leakage of unpublished product information. 

However, resource information within projects cannot be obtained, limiting the impact to existence confirmation only.

### Countermeasures
It is recommended to