security fix for parameter store list

This commit is contained in:
Arwid Thornström
2026-03-10 14:51:50 +01:00
parent 11bd5ca3a3
commit 4f4294453d
3 changed files with 52 additions and 20 deletions
+7 -10
View File
@@ -1,6 +1,10 @@
import { GluegunMenuToolbox } from '@lenne.tech/gluegun-menu'; import { GluegunMenuToolbox } from '@lenne.tech/gluegun-menu';
import chalk = require('chalk'); import chalk = require('chalk');
import { defaultMenuSettings, sleep } from '../../../../globals'; import { defaultMenuSettings, sleep } from '../../../../globals';
import {
readParameterNameCache,
writeParameterNameCache,
} from '../../../../common/parameterstore-cache';
const { AutoComplete } = require('enquirer'); const { AutoComplete } = require('enquirer');
module.exports = { module.exports = {
@@ -9,12 +13,9 @@ module.exports = {
description: 'List all parameters in parameter store (a ps l)', description: 'List all parameters in parameter store (a ps l)',
hidden: false, hidden: false,
run: async (toolbox: GluegunMenuToolbox) => { run: async (toolbox: GluegunMenuToolbox) => {
const { system, strings, print, filesystem } = toolbox; const { system, strings, print } = toolbox;
// Check for cache in /tmp folder const valueNames = [...readParameterNameCache(toolbox)];
const cache = filesystem.read('/tmp/.pwcli_parametercache');
const cacheValues = !cache ? [] : JSON.parse(cache);
const valueNames = [...cacheValues];
let nextToken = ''; let nextToken = '';
if (valueNames.length === 0) { if (valueNames.length === 0) {
@@ -45,11 +46,7 @@ module.exports = {
} }
spinner.text = 'Saving parameter names to /tmp/.pwcli_parametercache'; spinner.text = 'Saving parameter names to /tmp/.pwcli_parametercache';
writeParameterNameCache(toolbox, valueNames);
filesystem.file('/tmp/.pwcli_parametercache', {
mode: '600',
content: JSON.stringify(valueNames),
});
await sleep(2000); await sleep(2000);
spinner.stop(); spinner.stop();
@@ -1,7 +1,10 @@
import { GluegunMenuToolbox } from '@lenne.tech/gluegun-menu'; import { GluegunMenuToolbox } from '@lenne.tech/gluegun-menu';
import chalk = require('chalk'); import chalk = require('chalk');
import { defaultMenuSettings, sleep } from '../../../../globals'; import { defaultMenuSettings, sleep } from '../../../../globals';
import * as fs from 'fs'; import {
parameterNameCacheFilePath,
readParameterNameCache,
} from '../../../../common/parameterstore-cache';
const { AutoComplete } = require('enquirer'); const { AutoComplete } = require('enquirer');
module.exports = { module.exports = {
@@ -10,18 +13,13 @@ module.exports = {
description: 'Update an existing item in parameter store (a ps u)', description: 'Update an existing item in parameter store (a ps u)',
hidden: false, hidden: false,
run: async (toolbox: GluegunMenuToolbox) => { run: async (toolbox: GluegunMenuToolbox) => {
const { print, prompt, system, filesystem } = toolbox; const { print, prompt, system } = toolbox;
// Read the .pwcli_parametercache file const parameters = readParameterNameCache(toolbox);
const cacheFilePath = '/tmp/.pwcli_parametercache'; if (parameters.length === 0) {
let parameters = [];
if (fs.existsSync(cacheFilePath)) {
const cacheContent = await filesystem.read(cacheFilePath);
parameters = JSON.parse(cacheContent);
} else {
print.error( print.error(
chalk.red( chalk.red(
'Cache file not found. Please run "a ps l" first to populate the cache.', `Parameter name cache is missing or invalid at "${parameterNameCacheFilePath}". Run "a ps l" to populate a name-only cache.`,
), ),
); );
return; return;
+37
View File
@@ -0,0 +1,37 @@
import { GluegunMenuToolbox } from '@lenne.tech/gluegun-menu';
export const parameterNameCacheFilePath = '/tmp/.pwcli_parametercache';
const isParameterNameList = (value: unknown): value is string[] => {
return (
Array.isArray(value) && value.every((item) => typeof item === 'string')
);
};
export const readParameterNameCache = (
toolbox: GluegunMenuToolbox,
): string[] => {
const { filesystem } = toolbox;
const cache = filesystem.read(parameterNameCacheFilePath);
if (!cache) {
return [];
}
try {
const parsedCache = JSON.parse(cache);
return isParameterNameList(parsedCache) ? parsedCache : [];
} catch {
return [];
}
};
export const writeParameterNameCache = (
toolbox: GluegunMenuToolbox,
parameterNames: string[],
) => {
toolbox.filesystem.file(parameterNameCacheFilePath, {
mode: '600',
content: JSON.stringify(parameterNames),
});
};