Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to encrypt custom field / config arg data at rest #2648

Open
michaelbromley opened this issue Jan 25, 2024 · 2 comments
Open

Ability to encrypt custom field / config arg data at rest #2648

michaelbromley opened this issue Jan 25, 2024 · 2 comments
Labels
design 📐 This issue deals with high-level design of a feature @vendure/core
Milestone

Comments

@michaelbromley
Copy link
Member

Is your feature request related to a problem? Please describe.
Some data which is stored as config args or custom fields might be sensitive and would benefit from being stored encrypted in the DB.
For instance, the API keys of a payment plugin.

Describe the solution you'd like
I will investigate the feasibility of a new property on custom fields and configurable operation args, encrypt: true which uses some kind of symmetric encryption using a secret key (provided by an env var) to encrypt/decrypt these values on save/load.

Describe alternatives you've considered
Storing the values as env vars in the first place. This limits the utility of certain functions, e.g. we cannot then have channel-aware payment methods using different api keys for the same payment service.

@michaelbromley michaelbromley added @vendure/core design 📐 This issue deals with high-level design of a feature labels Jan 25, 2024
@michaelbromley michaelbromley moved this to 📋 Backlog in Vendure OS Roadmap Jan 25, 2024
michaelbromley added a commit that referenced this issue Feb 20, 2024
Exploring some ideas for #2648
@michaelbromley michaelbromley moved this from 📋 Backlog to 🤔 Under consideration in Vendure OS Roadmap Mar 19, 2024
@dlhck dlhck removed the next minor label Sep 24, 2024
@dlhck dlhck added this to the v3.1 milestone Sep 24, 2024
@dlhck dlhck moved this from 👀 Under consideration to 📅 Planned in Vendure OS Roadmap Sep 24, 2024
@dlhck dlhck modified the milestones: v3.1, v3.2 Sep 27, 2024
@oroce
Copy link

oroce commented Jan 9, 2025

hey, i wanted to chime in, we did something similar (not for custom fields for on our entities where we had to store passwords for an external invoicing integration).

We took leverage of the AfterLoad, BeforeInsert + BeforeUpdate. I hope it helps.

import Cryptr from 'cryptr';
import { AfterLoad, BeforeInsert, BeforeUpdate, Column, Entity } from 'typeorm';

const { ENCRYPTION_KEY } = process.env;

@Entity()
export class ExampleEntity extends VendureEntity {
  private readonly cryptr: Cryptr | null = null;
  constructor(input?: DeepPartial<ExampleEntity>) {
    super(input);
    if (ENCRYPTION_KEY != null) {
        this.cryptr = new Cryptr(ENCRYPTION_KEY);
    }
  }

  @Column({ type: 'varchar', nullable: true })
  password?: string | null;

  @BeforeInsert()
  @BeforeUpdate()
  encryptPassword() {
    if (!this.cryptr) {
      this.logger.verbose({
        message: 'Encryption key is not set in the config, password will not be encrypted',
      });
      return;
    }

    if (this.password != null && this.password !== '') {
      this.password = this.cryptr.encrypt(this.password);
    }
  }

  @AfterLoad()
  decryptPassword() {
    if (this.password == null || this.password === '') {
      return;
    }

    if (!this.cryptr) {
      this.logger.verbose({
        message: 'Encryption key is not set in the config, password will not be encrypted',
      });
      return;
    }

    try {
      this.password = this.cryptr.decrypt(this.password);
    } catch (ex) {
      this.logger.warn({
        message:
          'Failed to decrypt password, did you change the encryption key?'
        error: ex,
      });
    }
  }
}

@michaelbromley
Copy link
Member Author

@oroce thank you, that's a very useful example!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design 📐 This issue deals with high-level design of a feature @vendure/core
Projects
Status: 📅 Planned
Development

No branches or pull requests

3 participants