Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix: harden secret encryption guard and normalize error handling

Tin9634b592b7b0
diff --git a/apps/api/src/notification-preferences/secrets.ts b/apps/api/src/notification-preferences/secrets.ts
index 04cb88c83..389a5b633 100644
--- a/apps/api/src/notification-preferences/secrets.ts
+++ b/apps/api/src/notification-preferences/secrets.ts
@@ -4,6 +4,7 @@ import {
createHash,
randomBytes,
} from "node:crypto";
+import { HTTPException } from "hono/http-exception";
const SECRET_PREFIX = "enc:v1:";
const SECRET_ALGORITHM = "aes-256-gcm";
@@ -21,9 +22,10 @@ function getSecretEncryptionKey() {
function requireSecretEncryptionKey() {
const key = getSecretEncryptionKey();
if (!key) {
- throw new Error(
- "NOTIFICATION_SECRET_ENCRYPTION_KEY is required to store encrypted notification secrets",
- );
+ throw new HTTPException(500, {
+ message:
+ "NOTIFICATION_SECRET_ENCRYPTION_KEY is required to store encrypted notification secrets",
+ });
}
return key;
@@ -41,6 +43,15 @@ export function isEncryptedSecret(value: string | null | undefined): boolean {
return typeof value === "string" && value.startsWith(SECRET_PREFIX);
}
+function isValidEncryptedSecret(value: string): boolean {
+ try {
+ decryptSecret(value);
+ return true;
+ } catch {
+ return false;
+ }
+}
+
export function encryptSecret(
value: string | null | undefined,
): string | null | undefined {
@@ -48,7 +59,7 @@ export function encryptSecret(
return value;
}
- if (isEncryptedSecret(value)) {
+ if (isEncryptedSecret(value) && isValidEncryptedSecret(value)) {
return value;
}
@@ -78,18 +89,29 @@ export function decryptSecret(
const [iv, authTag, encrypted] = payload.split(".");
if (!iv || !authTag || !encrypted) {
- throw new Error("Invalid encrypted notification secret payload");
+ throw new HTTPException(500, {
+ message: "Invalid encrypted notification secret payload",
+ });
}
- const decipher = createDecipheriv(
- SECRET_ALGORITHM,
- requireSecretEncryptionKey(),
- decodePart(iv),
- );
- decipher.setAuthTag(decodePart(authTag));
-
- return Buffer.concat([
- decipher.update(decodePart(encrypted)),
- decipher.final(),
- ]).toString("utf8");
+ try {
+ const decipher = createDecipheriv(
+ SECRET_ALGORITHM,
+ requireSecretEncryptionKey(),
+ decodePart(iv),
+ );
+ decipher.setAuthTag(decodePart(authTag));
+
+ return Buffer.concat([
+ decipher.update(decodePart(encrypted)),
+ decipher.final(),
+ ]).toString("utf8");
+ } catch (error) {
+ if (error instanceof HTTPException) {
+ throw error;
+ }
+ throw new HTTPException(500, {
+ message: "Failed to decrypt notification secret",
+ });
+ }
}
diff --git a/apps/docs/core/functional/account-notifications.mdx b/apps/docs/core/functional/account-notifications.mdx
index 40e278187..63669bd53 100644
--- a/apps/docs/core/functional/account-notifications.mdx
+++ b/apps/docs/core/functional/account-notifications.mdx
@@ -130,7 +130,7 @@ This applies to:
- `Gotify` server URLs
- custom webhook URLs
-If you are using a self-hosted ntfy instance or webhook receiver, it must be reachable from the API with an accepted public or routable hostname.
+If you are using a self-hosted ntfy instance, Gotify instance, or webhook receiver, it must be reachable from the API with an accepted public or routable hostname.
## Troubleshooting
@@ -152,7 +152,7 @@ Check that:
- the signed-in user has an email address
- the server can reach the SMTP provider
-### ntfy or webhook save fails
+### ntfy, Gotify, or webhook save fails
Check that: