feat(安全): 店铺凭据解密支持历史密钥,为轮换留出双密钥窗口

换 shop-credential-key 会让 435 行历史密文全部解不开,而调用方拿到解不开的串
是当明文口令往外发(紫鸟账号、1688 店铺、第三方 api_key 都这样用)。
现在新增 aiimage.security.shop-credential-key-previous(逗号分隔),解密按
当前→历史的顺序回试,加密只用当前密钥;全部不中时 WARN 写明"这串密文会被
当成明文口令发出去",不再静默回退。与当前密钥相同的历史项自动丢弃。
previous 留空时行为与改动前逐字节一致(6 例单测盯着这一条)。
轮换设计与取证见 docs/ops/credential-rotation-design-2026-10-02.md(只含长度与
sha8 指纹,无明文值);轮换本身与存量重加密脚本仍未做。
This commit is contained in:
2026-10-02 11:01:18 +08:00
parent 5f7f37c788
commit 8812a3b64a
4 changed files with 270 additions and 16 deletions
@@ -0,0 +1,96 @@
package com.nanri.aiimage.common.security;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Field;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* 店铺凭据/用户密钥加密器的轮换语义测试。
* 关键契约:轮换后旧密文必须还能解(按 previous 回试),且全部解不开时是「回退原文」而不是抛错——
* 这条回退会把密文当明文口令发出去,所以轮换必须配合重加密,见 crawler-plugin/docs/ops/credential-rotation-design-2026-10-02.md。
*/
class ShopCredentialCryptoServiceTest {
private static final String OLD_KEY = "old-key-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
private static final String NEW_KEY = "new-key-bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
private static final String THIRD_KEY = "third-key-cccccccccccccccccccccccccccccccccccccccccccccccccccccccccc";
private static ShopCredentialCryptoService newService(String rawKey, String previousKeys) throws Exception {
ShopCredentialCryptoService service = new ShopCredentialCryptoService();
set(service, "rawKey", rawKey);
set(service, "previousKeys", previousKeys);
service.init();
return service;
}
private static void set(Object target, String name, String value) throws Exception {
Field field = target.getClass().getDeclaredField(name);
field.setAccessible(true);
field.set(target, value);
}
@SuppressWarnings("unchecked")
private static List<Object> previousOf(ShopCredentialCryptoService service) throws Exception {
Field field = service.getClass().getDeclaredField("previousKeySpecs");
field.setAccessible(true);
return (List<Object>) field.get(service);
}
@Test
void rotates_key_but_still_decrypts_legacy_ciphertext() throws Exception {
ShopCredentialCryptoService old = newService(OLD_KEY, "");
ShopCredentialCryptoService rotated = newService(NEW_KEY, OLD_KEY);
String legacy = old.encrypt("P@ssw0rd-shopA");
assertEquals("P@ssw0rd-shopA", rotated.decrypt(legacy));
assertNotEquals(old.encrypt("same-input"), rotated.encrypt("same-input"), "加密只用当前密钥");
assertEquals("same-input", rotated.decrypt(rotated.encrypt("same-input")));
}
@Test
void falls_back_to_the_input_when_no_key_matches() throws Exception {
String legacy = newService(OLD_KEY, "").encrypt("P@ssw0rd-shopA");
ShopCredentialCryptoService wrong = newService(NEW_KEY, THIRD_KEY);
assertEquals(legacy, wrong.decrypt(legacy), "回退原文是既有兼容行为,不能借轮换悄悄改成抛错");
}
@Test
void legacy_plaintext_values_pass_through_unchanged() throws Exception {
ShopCredentialCryptoService service = newService(NEW_KEY, "");
assertEquals("not-a-ciphertext!!", service.decrypt("not-a-ciphertext!!"));
assertEquals("", service.decrypt(null));
assertEquals("", service.decrypt(" "));
}
@Test
void behaves_exactly_as_before_when_previous_keys_is_empty() throws Exception {
ShopCredentialCryptoService service = newService(NEW_KEY, "");
String cipher = service.encrypt("v");
assertEquals("v", service.decrypt(cipher));
assertTrue(previousOf(service).isEmpty());
}
@Test
void drops_previous_keys_that_equal_the_current_key() throws Exception {
ShopCredentialCryptoService service = newService(NEW_KEY, NEW_KEY + " , ," + NEW_KEY);
assertTrue(previousOf(service).isEmpty(), "重复密钥白跑一遍解密,还会掩盖 previous 其实没配");
}
@Test
void tries_several_previous_keys_in_order() throws Exception {
String fromThird = newService(THIRD_KEY, "").encrypt("v");
ShopCredentialCryptoService service = newService(NEW_KEY, OLD_KEY + "," + THIRD_KEY);
assertEquals("v", service.decrypt(fromThird));
assertEquals(2, previousOf(service).size());
}
}