zitadel/internal/zerrors
Tim Möhlmann 4e3fd305ab
fix(crypto): reject decrypted strings with non-UTF8 characters. (#8374)
# Which Problems Are Solved

We noticed logging where 500: Internal Server errors were returned from
the token endpoint, mostly for the `refresh_token` grant. The error was
thrown by the database as it received non-UTF8 strings for token IDs

Zitadel uses symmetric encryption for opaque tokens, including refresh
tokens. Encrypted values are base64 encoded. It appeared to be possible
to send garbage base64 to the token endpoint, which will pass decryption
and string-splitting. In those cases the resulting ID is not a valid
UTF-8 string.

Invalid non-UTF8 strings are now rejected during token decryption.

# How the Problems Are Solved

- `AESCrypto.DecryptString()` checks if the decrypted bytes only contain
valid UTF-8 characters before converting them into a string.
- `AESCrypto.Decrypt()` is unmodified and still allows decryption on
non-UTF8 byte strings.
- `FromRefreshToken` now uses `DecryptString` instead of `Decrypt`

# Additional Changes

- Unit tests added for `FromRefreshToken` and
`AESCrypto.DecryptString()`.
- Fuzz tests added for `FromRefreshToken` and
`AESCrypto.DecryptString()`. This was to pinpoint the problem
- Testdata with values that resulted in invalid strings are committed.
In the pipeline this results in the Fuzz tests to execute as regular
unit-test cases. As we don't use the `-fuzz` flag in the pipeline no
further fuzzing is performed.

# Additional Context

- Closes #7765
- https://go.dev/doc/tutorial/fuzz
2024-08-02 08:38:37 +00:00
..
generate refactor: rename package errors to zerrors (#7039) 2023-12-08 15:30:55 +01:00
already_exists_test.go refactor: rename package errors to zerrors (#7039) 2023-12-08 15:30:55 +01:00
already_exists.go refactor: rename package errors to zerrors (#7039) 2023-12-08 15:30:55 +01:00
deadline_exceeded_test.go refactor: rename package errors to zerrors (#7039) 2023-12-08 15:30:55 +01:00
deadline_exceeded.go refactor: rename package errors to zerrors (#7039) 2023-12-08 15:30:55 +01:00
error_test.go refactor: rename package errors to zerrors (#7039) 2023-12-08 15:30:55 +01:00
error.go refactor: rename package errors to zerrors (#7039) 2023-12-08 15:30:55 +01:00
internal_test.go refactor: rename package errors to zerrors (#7039) 2023-12-08 15:30:55 +01:00
internal.go refactor: rename package errors to zerrors (#7039) 2023-12-08 15:30:55 +01:00
invalid_argument_test.go refactor: rename package errors to zerrors (#7039) 2023-12-08 15:30:55 +01:00
invalid_argument.go fix(crypto): reject decrypted strings with non-UTF8 characters. (#8374) 2024-08-02 08:38:37 +00:00
not_found_test.go refactor: rename package errors to zerrors (#7039) 2023-12-08 15:30:55 +01:00
not_found.go refactor: rename package errors to zerrors (#7039) 2023-12-08 15:30:55 +01:00
permission_denied_test.go refactor: rename package errors to zerrors (#7039) 2023-12-08 15:30:55 +01:00
permission_denied.go refactor: rename package errors to zerrors (#7039) 2023-12-08 15:30:55 +01:00
precondition_failed_test.go refactor: rename package errors to zerrors (#7039) 2023-12-08 15:30:55 +01:00
precondition_failed.go refactor: rename package errors to zerrors (#7039) 2023-12-08 15:30:55 +01:00
resource_exhausted_test.go refactor: rename package errors to zerrors (#7039) 2023-12-08 15:30:55 +01:00
resource_exhausted.go refactor: rename package errors to zerrors (#7039) 2023-12-08 15:30:55 +01:00
unauthenticated_test.go refactor: rename package errors to zerrors (#7039) 2023-12-08 15:30:55 +01:00
unauthenticated.go refactor: rename package errors to zerrors (#7039) 2023-12-08 15:30:55 +01:00
unavailable_test.go refactor: rename package errors to zerrors (#7039) 2023-12-08 15:30:55 +01:00
unavailable.go refactor: rename package errors to zerrors (#7039) 2023-12-08 15:30:55 +01:00
unimplemented_test.go refactor: rename package errors to zerrors (#7039) 2023-12-08 15:30:55 +01:00
unimplemented.go refactor: rename package errors to zerrors (#7039) 2023-12-08 15:30:55 +01:00
unknown_test.go refactor: rename package errors to zerrors (#7039) 2023-12-08 15:30:55 +01:00
unknown.go refactor: rename package errors to zerrors (#7039) 2023-12-08 15:30:55 +01:00
zerror_test.go refactor(fmt): run gci on complete project (#7557) 2024-04-03 10:43:43 +00:00
zerror.go refactor: rename package errors to zerrors (#7039) 2023-12-08 15:30:55 +01:00