Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(plugins): list only storage keys the plugin can use

Yudhi Armyndharisb61d192594b6
diff --git a/src/core/plugins/plugin-storage.service.spec.ts b/src/core/plugins/plugin-storage.service.spec.ts
index f6811a26e..08c406517 100644
--- a/src/core/plugins/plugin-storage.service.spec.ts
+++ b/src/core/plugins/plugin-storage.service.spec.ts
@@ -117,6 +117,15 @@ describe('PluginStorageService sandboxed per-plugin storage containment', () =>
expect(await storage.list()).toEqual(['foo']);
});
+ it('does not list a canonical encoded filename whose key get and set would refuse', async () => {
+ const pluginDataDir = path.join(dataDir, 'plugins', pluginId);
+ await storage.set('real', { ok: true });
+ const encoded = (key: string): string => `key-${Buffer.from(key).toString('base64url')}.json`;
+ for (const key of ['', '../escape']) fs.writeFileSync(path.join(pluginDataDir, encoded(key)), '{}');
+
+ expect(await storage.list()).toEqual(['real']);
+ });
+
it('deletes a legacy plain-key file', async () => {
const pluginDataDir = path.join(dataDir, 'plugins', pluginId);
fs.writeFileSync(path.join(pluginDataDir, 'legacy.json'), JSON.stringify({ old: true }));
diff --git a/src/core/plugins/plugin-storage.service.ts b/src/core/plugins/plugin-storage.service.ts
index da8a97c78..2172369af 100644
--- a/src/core/plugins/plugin-storage.service.ts
+++ b/src/core/plugins/plugin-storage.service.ts
@@ -447,7 +447,13 @@ export class PluginStorageService {
files
.filter(f => f.endsWith('.json'))
.map(f => f.slice(0, -'.json'.length))
- .map(stem => decodeStorageFileName(stem) ?? (!isPackageDir && resolveLegacyKeyPath(stem) ? stem : null))
+ .map(stem => {
+ // A listed key must be one get/set/delete accept, so a canonical name that decodes to an
+ // unsafe or empty key is not listed (and is not a legacy key either, see resolveLegacyKeyPath).
+ const decoded = decodeStorageFileName(stem);
+ if (decoded !== null) return isSafeStorageKey(decoded) ? decoded : null;
+ return !isPackageDir && resolveLegacyKeyPath(stem) ? stem : null;
+ })
.filter((k): k is string => k !== null),
),
);