Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(plugins): list only real storage keys in a package directory

Yudhi Armyndharisc75e9ebe2262
diff --git a/src/core/plugins/plugin-storage.service.spec.ts b/src/core/plugins/plugin-storage.service.spec.ts
index 4df5e071f..b9c0d65b5 100644
--- a/src/core/plugins/plugin-storage.service.spec.ts
+++ b/src/core/plugins/plugin-storage.service.spec.ts
@@ -130,6 +130,28 @@ describe('PluginStorageService sandboxed per-plugin storage containment', () =>
expect(JSON.parse(fs.readFileSync(packagePath, 'utf8'))).toEqual({ name: pluginId });
});
+ it('lists no package file as a key when storage shares the package directory', async () => {
+ // By default <dataDir>/plugins/<id> is both the installed package and ctx.storage, so a
+ // clear-all loop over list() must not reach the package's own root JSON files.
+ const pluginDataDir = path.join(dataDir, 'plugins', pluginId);
+ fs.writeFileSync(path.join(pluginDataDir, 'manifest.json'), JSON.stringify({ id: pluginId }));
+ for (const name of ['tsconfig.json', 'package-lock.json', 'defaults.json']) {
+ fs.writeFileSync(path.join(pluginDataDir, name), '{}');
+ }
+ await storage.set('real', 1);
+
+ expect(await storage.list()).toEqual(['real']);
+ for (const key of await storage.list()) await storage.delete(key);
+ expect(fs.readdirSync(pluginDataDir).sort()).toEqual([
+ 'defaults.json',
+ 'manifest.json',
+ 'package-lock.json',
+ 'tsconfig.json',
+ ]);
+ // A legacy key the plugin names explicitly stays readable.
+ expect(await storage.get('defaults')).toEqual({});
+ });
+
it('does not mangle a literal legacy filename that happens to start with "key-"', async () => {
const pluginDataDir = path.join(dataDir, 'plugins', pluginId);
// A pre-encoding plugin could have stored a key literally named "key-zzz" -> key-zzz.json.
diff --git a/src/core/plugins/plugin-storage.service.ts b/src/core/plugins/plugin-storage.service.ts
index 5a68cfbe8..edb5a3c6a 100644
--- a/src/core/plugins/plugin-storage.service.ts
+++ b/src/core/plugins/plugin-storage.service.ts
@@ -380,13 +380,17 @@ export class PluginStorageService {
try {
const files = fs.readdirSync(pluginDataDir);
// A legacy stem is listed only when get()/delete() would consult it, so the package's own
- // manifest.json/package.json never surface as keys that read back null.
+ // manifest.json/package.json never surface as keys that read back null. In a package directory
+ // (storage and the installed package share it by default) every other root JSON file is package
+ // content too, so only encoded keys are listed there; get()/delete() still honor a legacy key
+ // the plugin names explicitly.
+ const isPackageDir = files.includes('manifest.json');
let keys = Array.from(
new Set(
files
.filter(f => f.endsWith('.json'))
.map(f => f.slice(0, -'.json'.length))
- .map(stem => decodeStorageFileName(stem) ?? (resolveLegacyKeyPath(stem) ? stem : null))
+ .map(stem => decodeStorageFileName(stem) ?? (!isPackageDir && resolveLegacyKeyPath(stem) ? stem : null))
.filter((k): k is string => k !== null),
),
);