Files
outline/server/models/decorators/Encrypted.ts
T
Tom Moor 0139b91b5d chore: Replace lodash with es-toolkit (#12281)
* chore: Replace lodash with es-toolkit

Migrate all direct lodash imports to es-toolkit/compat for a smaller,
faster, lodash-compatible utility library. Transitive lodash usage from
other packages remains unchanged.

* fix: Restore isPlainObject semantics in CanCan policy

The lodash migration aliased `isObject` to `lodash/isPlainObject` and
the codemod incorrectly mapped the local name to es-toolkit's `isObject`,
which also returns true for arrays and functions. This caused condition
objects in policy definitions to be skipped, breaking authorization
checks across the codebase.

* fix: Restore unicode-aware length counting in validators

es-toolkit/compat's size() returns string.length, while lodash's _.size()
counts unicode code points. Switch to [...value].length to preserve the
previous behavior so multi-byte characters like emoji count as one.
2026-05-06 21:03:47 -04:00

76 lines
2.8 KiB
TypeScript

import { isEmpty, isNil } from "es-toolkit/compat";
import { getAttributes } from "sequelize-typescript";
import Logger from "@server/logging/Logger";
import vaults from "@server/storage/vaults";
const key = "sequelize:vault";
/**
* A decorator that stores the encrypted vault for a particular database column
* so that it can be used by getters and setters. Must be accompanied by a
* @Column(DataType.BLOB) annotation.
*/
export default function Encrypted(target: object, propertyKey: string) {
// Ensure that the Encrypted decorator is the first decorator applied to the property, we can check
// this by looking at the attributes of the target and checking if the propertyKey is already defined.
if (getAttributes(target)[propertyKey]) {
Logger.fatal(
`The Encrypted decorator must be the first decorator applied to the property ${propertyKey} in ${target.constructor.name}`,
new Error()
);
}
Reflect.defineMetadata(key, vaults().vault(propertyKey), target, propertyKey);
return {
get() {
const attributeOptions = getAttributes(target);
const defaultValue = attributeOptions[propertyKey].allowNull ? null : "";
if (!this.getDataValue(propertyKey)) {
return defaultValue;
}
try {
const value = Reflect.getMetadata(key, this, propertyKey).get.call(
this
);
// `value` equals to `{}` instead of `null` if column value in db is `null`. Possibly explained by:
// https://github.com/defunctzombie/sequelize-encrypted/blob/c3854e76ae4b80318c8f10f94e6c898c67659ca6/index.js#L30-L33
return isEmpty(value) && typeof value === "object"
? defaultValue
: value;
} catch (err) {
if (err.message.includes("Unexpected end of JSON input")) {
return defaultValue;
}
if (err.message.includes("bad decrypt")) {
Logger.fatal(
`Failed to decrypt database column (${propertyKey}). The SECRET_KEY environment variable may have changed since installation.`,
err
);
}
throw err;
}
},
set(value: string | null) {
try {
if (isNil(value)) {
this.setDataValue(propertyKey, value);
} else {
Reflect.getMetadata(key, this, propertyKey).set.call(this, value);
}
} catch (err) {
if (err.message.includes("Invalid key length")) {
Logger.fatal(
`Failed to encrypt database column (${propertyKey}). The SECRET_KEY environment variable is not the correct length.`,
err
);
}
throw err;
}
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- TS rejects PropertyDescriptor return for legacy decorator; descriptor is consumed by Sequelize at runtime.
} as any;
}