Skip to content

Commit

Permalink
fix: support most unix style file paths
Browse files Browse the repository at this point in the history
We allow most file paths now including spaces. We exclude `=` to allow the `path=whatever` format and `/` because `isomoric-git` doesn't handle it.

[ci skip]
  • Loading branch information
tegefaulkes committed Jun 28, 2024
1 parent f511693 commit da6974f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/utils/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import * as networkUtils from 'polykey/dist/network/utils';
import * as nodesUtils from 'polykey/dist/nodes/utils';

const secretPathRegex =
/^([\w-]+)(?::)([\w\-\\\/\.\$]+)(?:=)?([a-zA-Z_][\w]+)?$/;
/^([\w-]+)(?::)([^\0\\=]+)(?:=)?([a-zA-Z_][\w]+)?$/;
const secretPathEnvRegex =
/^([\w-]+)(?::)([\w\-\\\/\.\$]+)(?:=)?([a-zA-Z_]+[a-zA-Z0-9_]*)?$/;
/^([\w-]+)(?::)([^\0\\=]+)(?:=)?([a-zA-Z_]+[a-zA-Z0-9_]*)?$/;

/**
* Converts a validation parser to commander argument parser
Expand Down
40 changes: 40 additions & 0 deletions tests/secrets/create.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { VaultName } from 'polykey/dist/vaults/types';
import path from 'path';
import fs from 'fs';
import { test } from '@fast-check/jest';
import fc from 'fast-check';
import Logger, { LogLevel, StreamHandler } from '@matrixai/logger';
import PolykeyAgent from 'polykey/dist/PolykeyAgent';
import { vaultOps } from 'polykey/dist/vaults';
Expand Down Expand Up @@ -76,4 +78,42 @@ describe('commandCreateSecret', () => {
},
globalThis.defaultTimeout * 2,
);
const fileNameArb = fc.stringMatching(/^[^\0\\/]$/);
test.prop([fileNameArb, fileNameArb], { numRuns: 10 })(
'secrets handle unix style paths for secrets',
async (directoryName, secretName) => {
await polykeyAgent.vaultManager.stop();
await polykeyAgent.vaultManager.start({ fresh: true });
const vaultName = 'Vault1' as VaultName;
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);
const secretPath = path.join(dataDir, 'secret');
await fs.promises.writeFile(secretPath, 'this is a secret');
const vaultsSecretPath = path.join(directoryName, secretName);

command = [
'secrets',
'create',
'-np',
dataDir,
secretPath,
`${vaultName}:${vaultsSecretPath}`,
];

const result = await testUtils.pkStdio([...command], {
env: {
PK_PASSWORD: password,
},
cwd: dataDir,
});
expect(result.exitCode).toBe(0);

await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => {
const list = await vaultOps.listSecrets(vault);
expect(list.sort()).toStrictEqual([vaultsSecretPath]);
expect(
(await vaultOps.getSecret(vault, vaultsSecretPath)).toString(),
).toStrictEqual('this is a secret');
});
},
);
});

0 comments on commit da6974f

Please sign in to comment.