mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-24 09:16:29 +00:00
276 lines
11 KiB
TypeScript
276 lines
11 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import {
|
|
buildPluginImporterSafePreview,
|
|
mergePluginImporterDrafts,
|
|
normalizePluginImporterRecords,
|
|
} from './pluginImporter.ts';
|
|
|
|
test('plugin importer drafts receive host-owned identities and reject malformed records', () => {
|
|
const result = normalizePluginImporterRecords([
|
|
{ type: 'draft', draft: { kind: 'host', value: { id: 'plugin-id', label: 'Prod', hostname: 'prod.test', username: 'root', tags: [], os: 'linux' } } },
|
|
{ type: 'draft', draft: { kind: 'identity', value: { id: 'plugin-id', label: 'Deploy', username: 'deploy', authMethod: 'password', password: 'secret' } } },
|
|
{ type: 'draft', draft: { kind: 'group', value: { path: 'Imported/Prod' } } },
|
|
{ type: 'draft', draft: { kind: 'snippet', value: { label: 'Broken' } } },
|
|
{ type: 'warning', message: 'Provider warning' },
|
|
]);
|
|
assert.equal(result.hosts.length, 1);
|
|
assert.notEqual(result.hosts[0].id, 'plugin-id');
|
|
assert.equal(result.identities.length, 1);
|
|
assert.notEqual(result.identities[0].id, 'plugin-id');
|
|
assert.deepEqual(result.groups, ['Imported/Prod']);
|
|
assert.deepEqual(result.warnings, ['Provider warning']);
|
|
assert.deepEqual(result.errors, ['Importer returned an invalid snippet draft.']);
|
|
});
|
|
|
|
test('plugin importer host drafts preserve unavailable namespaced configuration', () => {
|
|
const result = normalizePluginImporterRecords([{ type: 'draft', draft: {
|
|
kind: 'host',
|
|
value: {
|
|
label: 'Custom transport',
|
|
hostname: 'opaque-target',
|
|
username: '',
|
|
tags: [],
|
|
os: 'linux',
|
|
protocol: 'plugin:com.example.transport.connection',
|
|
pluginConnection: {
|
|
providerId: 'com.example.transport.connection',
|
|
configuration: { endpoint: 'opaque-target' },
|
|
},
|
|
},
|
|
} }]);
|
|
assert.equal(result.hosts[0].protocol, 'plugin:com.example.transport.connection');
|
|
assert.deepEqual(result.hosts[0].pluginConnection?.configuration, { endpoint: 'opaque-target' });
|
|
});
|
|
|
|
test('plugin importer host drafts can use opaque provider configuration without an SSH hostname', () => {
|
|
const result = normalizePluginImporterRecords([{ type: 'draft', draft: {
|
|
kind: 'host',
|
|
value: {
|
|
label: 'Opaque service',
|
|
protocol: 'plugin:com.example.transport.connection',
|
|
pluginConnection: {
|
|
providerId: 'com.example.transport.connection',
|
|
configuration: { account: 'production' },
|
|
},
|
|
},
|
|
} }]);
|
|
assert.equal(result.hosts.length, 1);
|
|
assert.equal(result.hosts[0].hostname, 'com.example.transport.connection');
|
|
assert.deepEqual(result.hosts[0].pluginConnection?.configuration, { account: 'production' });
|
|
});
|
|
|
|
test('plugin importer rejects plugin host drafts whose protocol does not match the provider ID', () => {
|
|
const result = normalizePluginImporterRecords([{ type: 'draft', draft: {
|
|
kind: 'host',
|
|
value: {
|
|
label: 'Mismatched service',
|
|
protocol: 'plugin:com.example.other.connection',
|
|
pluginConnection: {
|
|
providerId: 'com.example.transport.connection',
|
|
configuration: { account: 'production' },
|
|
},
|
|
},
|
|
} }]);
|
|
assert.deepEqual(result.hosts, []);
|
|
assert.deepEqual(result.errors, ['Importer returned an invalid host draft.']);
|
|
});
|
|
|
|
test('plugin importer rejects unknown host protocols instead of persisting an unsafe cast', () => {
|
|
const result = normalizePluginImporterRecords([{ type: 'draft', draft: {
|
|
kind: 'host',
|
|
value: {
|
|
label: 'Unsupported transport',
|
|
hostname: 'host.test',
|
|
protocol: 'rdp',
|
|
},
|
|
} }]);
|
|
assert.deepEqual(result.hosts, []);
|
|
assert.deepEqual(result.errors, ['Importer returned an invalid host draft.']);
|
|
});
|
|
|
|
test('plugin importer rejects overlong host protocols before draft spreading', () => {
|
|
const result = normalizePluginImporterRecords([{ type: 'draft', draft: {
|
|
kind: 'host',
|
|
value: {
|
|
label: 'Overlong transport',
|
|
hostname: 'host.test',
|
|
protocol: 'plugin:'.concat('a'.repeat(193)),
|
|
},
|
|
} }]);
|
|
assert.deepEqual(result.hosts, []);
|
|
assert.deepEqual(result.errors, ['Importer returned an invalid host draft.']);
|
|
});
|
|
|
|
test('plugin importer validates supported host fields before sanitizing drafts', () => {
|
|
assert.doesNotThrow(() => normalizePluginImporterRecords([{ type: 'draft', draft: {
|
|
kind: 'host',
|
|
value: {
|
|
label: 'Malformed host',
|
|
hostname: 'host.test',
|
|
notes: 5,
|
|
},
|
|
} }]));
|
|
const result = normalizePluginImporterRecords([{ type: 'draft', draft: {
|
|
kind: 'host',
|
|
value: {
|
|
label: 'Malformed host',
|
|
hostname: 'host.test',
|
|
notes: 5,
|
|
},
|
|
} }]);
|
|
assert.deepEqual(result.hosts, []);
|
|
assert.deepEqual(result.errors, ['Importer returned an invalid host draft.']);
|
|
});
|
|
|
|
test('plugin importer preserves validated host fields without retaining arbitrary draft properties', () => {
|
|
const result = normalizePluginImporterRecords([{ type: 'draft', draft: {
|
|
kind: 'host',
|
|
value: {
|
|
label: 'Imported host',
|
|
hostname: 'host.test',
|
|
username: 'alice',
|
|
group: 'Imported',
|
|
notes: ' visible notes ',
|
|
port: 2222,
|
|
deviceType: 'network',
|
|
sftpFileProtocol: 'scp',
|
|
pinned: true,
|
|
unsafeExtra: 'discarded',
|
|
},
|
|
} }]);
|
|
assert.equal(result.hosts.length, 1);
|
|
assert.equal(result.hosts[0].group, 'Imported');
|
|
assert.equal(result.hosts[0].notes, 'visible notes');
|
|
assert.equal(result.hosts[0].port, 2222);
|
|
assert.equal(result.hosts[0].deviceType, 'network');
|
|
assert.equal(result.hosts[0].sftpFileProtocol, 'scp');
|
|
assert.equal(result.hosts[0].pinned, true);
|
|
assert.equal(Object.hasOwn(result.hosts[0] as unknown as Record<string, unknown>, 'unsafeExtra'), false);
|
|
});
|
|
|
|
test('plugin importer drops startup commands from host drafts', () => {
|
|
const result = normalizePluginImporterRecords([{ type: 'draft', draft: {
|
|
kind: 'host',
|
|
value: {
|
|
label: 'Imported host',
|
|
hostname: 'host.test',
|
|
startupCommand: 'rm -rf /tmp/example',
|
|
},
|
|
} }]);
|
|
assert.equal(result.hosts.length, 1);
|
|
assert.equal(result.hosts[0].startupCommand, undefined);
|
|
});
|
|
|
|
test('plugin importer drops hidden plaintext built-in credential fields from host drafts', () => {
|
|
const result = normalizePluginImporterRecords([{ type: 'draft', draft: {
|
|
kind: 'host',
|
|
value: {
|
|
label: 'Imported host',
|
|
hostname: 'host.test',
|
|
telnetUsername: 'admin',
|
|
telnetPassword: 'plaintext',
|
|
savePassword: true,
|
|
},
|
|
} }]);
|
|
assert.equal(result.hosts.length, 1);
|
|
assert.equal(result.hosts[0].telnetUsername, undefined);
|
|
assert.equal(result.hosts[0].telnetPassword, undefined);
|
|
assert.equal(result.hosts[0].savePassword, undefined);
|
|
});
|
|
|
|
test('plugin importer rejects malformed supported host enum fields', () => {
|
|
for (const [key, value] of [
|
|
['deviceType', 'router'],
|
|
['sftpFileProtocol', 'ftp'],
|
|
] as const) {
|
|
const result = normalizePluginImporterRecords([{ type: 'draft', draft: {
|
|
kind: 'host',
|
|
value: {
|
|
label: 'Malformed host',
|
|
hostname: 'host.test',
|
|
[key]: value,
|
|
},
|
|
} }]);
|
|
assert.deepEqual(result.hosts, []);
|
|
assert.deepEqual(result.errors, ['Importer returned an invalid host draft.']);
|
|
}
|
|
});
|
|
|
|
test('plugin importer remaps provider-local key and identity references into host-owned IDs', () => {
|
|
const result = normalizePluginImporterRecords([
|
|
{ type: 'draft', draft: { kind: 'key', value: {
|
|
id: 'provider-key', label: 'Key', type: 'ED25519', privateKey: 'private',
|
|
} } },
|
|
{ type: 'draft', draft: { kind: 'identity', value: {
|
|
id: 'provider-identity', label: 'Identity', username: 'root', authMethod: 'key', keyId: 'provider-key',
|
|
} } },
|
|
{ type: 'draft', draft: { kind: 'host', value: {
|
|
label: 'Host', hostname: 'host.test', identityId: 'provider-identity', identityFileId: 'provider-key',
|
|
} } },
|
|
]);
|
|
assert.equal(result.identities[0].keyId, result.keys[0].id);
|
|
assert.equal(result.hosts[0].identityId, result.identities[0].id);
|
|
assert.equal(result.hosts[0].identityFileId, result.keys[0].id);
|
|
});
|
|
|
|
test('plugin importer merge skips duplicates and remaps relationships to retained Vault records', () => {
|
|
const records = [
|
|
{ type: 'draft', draft: { kind: 'key', value: {
|
|
id: 'provider-key', label: 'Key', type: 'ED25519', privateKey: 'private',
|
|
} } },
|
|
{ type: 'draft', draft: { kind: 'identity', value: {
|
|
id: 'provider-identity', label: 'Identity', username: 'root', authMethod: 'key', keyId: 'provider-key',
|
|
} } },
|
|
{ type: 'draft', draft: { kind: 'host', value: {
|
|
label: 'Host', hostname: 'host.test', username: 'root', identityId: 'provider-identity', identityFileId: 'provider-key',
|
|
} } },
|
|
{ type: 'draft', draft: { kind: 'snippet', value: {
|
|
label: 'Check', command: 'uptime',
|
|
} } },
|
|
] as const;
|
|
const existingDrafts = normalizePluginImporterRecords(records);
|
|
const duplicateDrafts = normalizePluginImporterRecords(records);
|
|
const merged = mergePluginImporterDrafts({
|
|
hosts: existingDrafts.hosts,
|
|
identities: existingDrafts.identities,
|
|
keys: existingDrafts.keys,
|
|
snippets: existingDrafts.snippets,
|
|
customGroups: [],
|
|
}, duplicateDrafts);
|
|
assert.equal(merged.duplicateCount, 4);
|
|
assert.equal(merged.addedCount, 0);
|
|
assert.equal(merged.keys.length, 1);
|
|
assert.equal(merged.identities.length, 1);
|
|
assert.equal(merged.hosts.length, 1);
|
|
assert.equal(merged.snippets.length, 1);
|
|
});
|
|
|
|
test('plugin importer preview is bounded and never exposes secret or command payloads', () => {
|
|
const drafts = normalizePluginImporterRecords([
|
|
{ type: 'draft', draft: { kind: 'identity', value: {
|
|
label: 'Deploy\nidentity', username: 'root', authMethod: 'password', password: 'do-not-render',
|
|
} } },
|
|
{ type: 'draft', draft: { kind: 'key', value: {
|
|
label: 'Production key', type: 'ED25519', privateKey: 'private-key-material', passphrase: 'secret',
|
|
} } },
|
|
{ type: 'draft', draft: { kind: 'snippet', value: {
|
|
label: 'Restart service', command: 'contains-sensitive-command',
|
|
} } },
|
|
{ type: 'warning', message: 'Check\nsource' },
|
|
{ type: 'error', message: 'Invalid\tentry' },
|
|
]);
|
|
const preview = buildPluginImporterSafePreview(drafts, 2, 1);
|
|
assert.deepEqual(preview.items, [
|
|
{ kind: 'identity', label: 'Deploy identity', detail: 'root · password' },
|
|
{ kind: 'key', label: 'Production key', detail: 'ED25519 · imported' },
|
|
]);
|
|
assert.deepEqual(preview.warnings, ['Check source']);
|
|
assert.deepEqual(preview.errors, []);
|
|
assert.equal(preview.omittedItemCount, 1);
|
|
assert.equal(preview.omittedDiagnosticCount, 1);
|
|
assert.equal(JSON.stringify(preview).includes('do-not-render'), false);
|
|
assert.equal(JSON.stringify(preview).includes('private-key-material'), false);
|
|
assert.equal(JSON.stringify(preview).includes('contains-sensitive-command'), false);
|
|
});
|