:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
chore: test ENCRYPTION_SALT_KEYS multi-key and two-step rotation (#61038)
Tom Piccirello4a4c3e56cd6b
diff --git a/nodejs/src/cdp/utils/encryption-utils.test.ts b/nodejs/src/cdp/utils/encryption-utils.test.tsindex cec5e65d43e1..cad1fa39e99b 100644--- a/nodejs/src/cdp/utils/encryption-utils.test.ts+++ b/nodejs/src/cdp/utils/encryption-utils.test.ts@@ -40,6 +40,56 @@ describe('Encrypted fields', () => {expect(encryptedFields.decrypt('NOT VALID', { ignoreDecryptionErrors: true })).toEqual('NOT VALID')})+ describe('two-step key rotation', () => {+ // Apps are not guaranteed to redeploy simultaneously, so rotation is done in two steps:+ // step 1: [OLD] -> [OLD, NEW] NEW added for decryption; OLD still encrypts+ // step 2: [OLD, NEW] -> [NEW, OLD] NEW now encrypts; OLD kept for decryption+ // Safety invariant: within each step's mixed-version window, every running app can+ // decrypt whatever any other running app writes.+ const OLD = 'o'.repeat(32)+ const NEW = 'n'.repeat(32)+ const PLAINTEXT = 'super-secret-value'++ const app = (keys: string[]): EncryptedFields => new EncryptedFields(keys.join(','))++ it.each([+ ['step 1', [[OLD], [OLD, NEW]]],+ [+ 'step 2',+ [+ [OLD, NEW],+ [NEW, OLD],+ ],+ ],+ ])('%s: coexisting apps decrypt each others writes', (_name, coexisting) => {+ for (const writerKeys of coexisting) {+ const token = app(writerKeys).encrypt(PLAINTEXT)+ for (const readerKeys of coexisting) {+ expect(app(readerKeys).decrypt(token)).toEqual(PLAINTEXT)+ }+ }+ })++ it.each([+ ['old only', [OLD]],+ ['old then new', [OLD, NEW]],+ ])('step 1 (%s) always encrypts with the old key', (_name, keys) => {+ const token = app(keys).encrypt(PLAINTEXT)+ expect(app([OLD]).decrypt(token)).toEqual(PLAINTEXT)+ expect(() => app([NEW]).decrypt(token)).toThrow()+ })++ it('step 2 apps encrypt with the new key', () => {+ const token = app([NEW, OLD]).encrypt(PLAINTEXT)+ expect(app([NEW]).decrypt(token)).toEqual(PLAINTEXT)+ })++ it('skipping step 1 would break un-upgraded apps', () => {+ const token = app([NEW, OLD]).encrypt(PLAINTEXT)+ expect(() => app([OLD]).decrypt(token)).toThrow()+ })+ })+describe('decrypting objects', () => {it('should decrypt an object', () => {const exampleObject = {diff --git a/posthog/helpers/tests/test_encrypted_fields.py b/posthog/helpers/tests/test_encrypted_fields.pyindex 08aeaa1ce215..7e9a50b3d889 100644--- a/posthog/helpers/tests/test_encrypted_fields.py+++ b/posthog/helpers/tests/test_encrypted_fields.py@@ -1,9 +1,41 @@+import base64+from freezegun import freeze_timefrom posthog.test.base import BaseTestfrom unittest.mock import patch+from django.test import SimpleTestCase, override_settings++from cryptography.fernet import Fernet, InvalidToken+from parameterized import parameterized+from posthog.helpers.encrypted_fields import EncryptedFieldMixin+KEY_A = "a" * 32+KEY_B = "b" * 32+KEY_C = "c" * 32++OLD = "o" * 32+NEW = "n" * 32+PLAINTEXT = "super-secret-value"+++def _fernet(raw_key: str) -> Fernet:+ # Build a Fernet from a single raw salt key, matching EncryptedFieldMixin.keys derivation+ return Fernet(base64.urlsafe_b64encode(raw_key.encode("utf-8")))+++def _encrypt_with(keys: list[str], plaintext: str = PLAINTEXT) -> str:+ # Simulates an app running with the given ENCRYPTION_SALT_KEYS encrypting a value+ with override_settings(ENCRYPTION_SALT_KEYS=keys, SALT_KEY=[]):+ return EncryptedFieldMixin().encrypt(plaintext)+++def _decrypt_with(keys: list[str], token: str) -> str:+ # Simulates an app running with the given ENCRYPTION_SALT_KEYS decrypting a value+ with override_settings(ENCRYPTION_SALT_KEYS=keys, SALT_KEY=[]):+ return EncryptedFieldMixin().decrypt(token)+class TestEncryptedFields(BaseTest):def test_simple_encryption_and_decryption(self):@@ -22,3 +54,105 @@ class TestEncryptedFields(BaseTest):decrypted = ef.f.decrypt(bytes(encrypted, "utf-8")).decode("utf-8")assert decrypted == "test-case"+++class TestEncryptedFieldsMultiKey(SimpleTestCase):+ # SALT_KEY=[] disables the legacy PBKDF2-derived keys so each test only exercises ENCRYPTION_SALT_KEYS++ @override_settings(ENCRYPTION_SALT_KEYS=[KEY_A, KEY_B], SALT_KEY=[])+ def test_encryption_uses_first_key(self):+ token = EncryptedFieldMixin().encrypt("secret")++ assert _fernet(KEY_A).decrypt(token.encode("utf-8")).decode("utf-8") == "secret"+ with self.assertRaises(InvalidToken):+ _fernet(KEY_B).decrypt(token.encode("utf-8"))++ def test_decryption_tries_every_key_in_the_list(self):+ with override_settings(ENCRYPTION_SALT_KEYS=[KEY_B], SALT_KEY=[]):+ token = EncryptedFieldMixin().encrypt("secret")++ with override_settings(ENCRYPTION_SALT_KEYS=[KEY_A, KEY_B], SALT_KEY=[]):+ assert EncryptedFieldMixin().decrypt(token) == "secret"++ def test_prepending_new_key_keeps_old_data_readable_and_writes_with_new(self):+ with override_settings(ENCRYPTION_SALT_KEYS=[KEY_B], SALT_KEY=[]):+ old_token = EncryptedFieldMixin().encrypt("secret")++ with override_settings(ENCRYPTION_SALT_KEYS=[KEY_A, KEY_B], SALT_KEY=[]):+ ef = EncryptedFieldMixin()+ assert ef.decrypt(old_token) == "secret"+ new_token = ef.encrypt("secret")++ assert _fernet(KEY_A).decrypt(new_token.encode("utf-8")).decode("utf-8") == "secret"++ def test_removing_old_key_makes_old_data_undecryptable(self):+ with override_settings(ENCRYPTION_SALT_KEYS=[KEY_B], SALT_KEY=[]):+ old_token = EncryptedFieldMixin().encrypt("secret")++ with override_settings(ENCRYPTION_SALT_KEYS=[KEY_A], SALT_KEY=[]):+ with self.assertRaises(InvalidToken):+ EncryptedFieldMixin().decrypt(old_token)++ @override_settings(ENCRYPTION_SALT_KEYS=[KEY_B, KEY_A], SALT_KEY=[])+ def test_key_order_determines_which_key_encrypts(self):+ token = EncryptedFieldMixin().encrypt("secret")++ assert _fernet(KEY_B).decrypt(token.encode("utf-8")).decode("utf-8") == "secret"+ with self.assertRaises(InvalidToken):+ _fernet(KEY_A).decrypt(token.encode("utf-8"))++ def test_three_keys_decrypt_data_from_any_of_them(self):+ tokens = {}+ for key in (KEY_A, KEY_B, KEY_C):+ with override_settings(ENCRYPTION_SALT_KEYS=[key], SALT_KEY=[]):+ tokens[key] = EncryptedFieldMixin().encrypt(f"secret-{key[0]}")++ with override_settings(ENCRYPTION_SALT_KEYS=[KEY_C, KEY_B, KEY_A], SALT_KEY=[]):+ ef = EncryptedFieldMixin()+ for key in (KEY_A, KEY_B, KEY_C):+ assert ef.decrypt(tokens[key]) == f"secret-{key[0]}"+++class TestEncryptionKeyRotationTwoStep(SimpleTestCase):+ # Two-step rollout, used because apps are not guaranteed to redeploy simultaneously:+ # step 1: [OLD] -> [OLD, NEW] NEW added for decryption; OLD still encrypts+ # step 2: [OLD, NEW] -> [NEW, OLD] NEW now encrypts; OLD kept for decryption+ # Safety invariant: within each step's mixed-version window, every running app can+ # decrypt whatever any other running app writes.++ @parameterized.expand(+ [+ ("step_1", [[OLD], [OLD, NEW]]),+ ("step_2", [[OLD, NEW], [NEW, OLD]]),+ ]+ )+ def test_coexisting_apps_decrypt_each_others_writes(self, _name, coexisting):+ for writer_keys in coexisting:+ token = _encrypt_with(writer_keys)+ for reader_keys in coexisting:+ assert _decrypt_with(reader_keys, token) == PLAINTEXT++ @parameterized.expand(+ [+ ("old_only", [OLD]),+ ("old_then_new", [OLD, NEW]),+ ]+ )+ def test_step_1_apps_always_encrypt_with_old_key(self, _name, keys):+ # While [OLD] and [OLD, NEW] apps coexist, OLD is always first, so no app emits+ # NEW-encrypted data that an un-upgraded [OLD]-only app could not read.+ token = _encrypt_with(keys)+ assert _fernet(OLD).decrypt(token.encode("utf-8")).decode("utf-8") == PLAINTEXT+ with self.assertRaises(InvalidToken):+ _fernet(NEW).decrypt(token.encode("utf-8"))++ def test_step_2_apps_encrypt_with_new_key(self):+ token = _encrypt_with([NEW, OLD])+ assert _fernet(NEW).decrypt(token.encode("utf-8")).decode("utf-8") == PLAINTEXT++ def test_skipping_step_1_would_break_un_upgraded_apps(self):+ # Justifies the two-step: a direct [OLD] -> [NEW, OLD] jump lets an upgraded app+ # encrypt with NEW while an un-upgraded [OLD]-only app still cannot decrypt it.+ token = _encrypt_with([NEW, OLD])+ with self.assertRaises(InvalidToken):+ _decrypt_with([OLD], token)