Skip to content

Commit 748e989

Browse files
author
Miguel Targa
committed
Data validation
1 parent d91b601 commit 748e989

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "ssh-control",
33
"displayName": "SSH Control",
44
"description": "Manage SSH connections directly from VSCode.",
5-
"version": "0.2.0",
5+
"version": "0.2.1",
66
"publisher": "migueltarga",
77
"author": {
88
"name": "Miguel Targa",

src/remoteHostsService.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,15 @@ export class RemoteHostsService {
101101
if (Array.isArray(jsonData)) {
102102
return { hosts: jsonData };
103103
} else if (jsonData.hosts || jsonData.groups) {
104+
// Ensure remote groups have required properties
105+
const normalizedGroups = (jsonData.groups || []).map((group: any) => ({
106+
...group,
107+
hosts: group.hosts || []
108+
}));
109+
104110
return {
105111
hosts: jsonData.hosts || [],
106-
groups: jsonData.groups || []
112+
groups: normalizedGroups
107113
};
108114
} else if (jsonData.hosts && Array.isArray(jsonData.hosts)) {
109115
return { hosts: jsonData.hosts };

src/sshConfigManager.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,43 @@ export class SSHConfigManager {
5151

5252
try {
5353
const configData = fs.readFileSync(this.configPath, 'utf8');
54-
return JSON.parse(configData);
54+
const config = JSON.parse(configData);
55+
return this.validateAndNormalizeConfig(config);
5556
} catch (error) {
5657
vscode.window.showErrorMessage(`Failed to load SSH config: ${error}`);
5758
return { groups: [] };
5859
}
5960
}
6061

62+
private validateAndNormalizeConfig(config: any): SSHConfig {
63+
if (!config || typeof config !== 'object') {
64+
return { groups: [] };
65+
}
66+
67+
const normalizedConfig: SSHConfig = {
68+
groups: Array.isArray(config.groups) ? config.groups.map(this.validateAndNormalizeGroup) : []
69+
};
70+
71+
return normalizedConfig;
72+
}
73+
74+
private validateAndNormalizeGroup = (group: any): SSHGroup => {
75+
if (!group || typeof group !== 'object' || !group.name) {
76+
throw new Error('Invalid group structure');
77+
}
78+
79+
return {
80+
name: group.name,
81+
defaultUser: group.defaultUser,
82+
defaultPort: group.defaultPort,
83+
defaultIdentityFile: group.defaultIdentityFile,
84+
defaultPreferredAuthentication: group.defaultPreferredAuthentication,
85+
hosts: Array.isArray(group.hosts) ? group.hosts : [],
86+
groups: Array.isArray(group.groups) ? group.groups.map(this.validateAndNormalizeGroup) : undefined,
87+
remoteHosts: group.remoteHosts
88+
};
89+
};
90+
6191
async saveConfig(config: SSHConfig): Promise<void> {
6292
try {
6393
const configData = JSON.stringify(config, null, 2);

src/sshTreeDataProvider.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ export class SSHTreeDataProvider implements vscode.TreeDataProvider<SSHTreeItem>
5959
if (!element) {
6060
// Root level - return top-level groups
6161
const config = await this.configManager.loadConfig();
62+
if (!config || !config.groups || !Array.isArray(config.groups)) {
63+
return [];
64+
}
6265
return config.groups.map((group, index) => ({
6366
type: 'group',
6467
group,

0 commit comments

Comments
 (0)