-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommandInputGuard.cs
More file actions
106 lines (91 loc) · 3.79 KB
/
Copy pathCommandInputGuard.cs
File metadata and controls
106 lines (91 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Collections.Generic;
namespace Flow.Launcher.Plugin.QuickSSH
{
/// <summary>
/// Normalizes menu-assisted query input and validates names used for saved profiles and actions.
/// </summary>
public static class CommandInputGuard
{
private static readonly HashSet<string> ReservedNames = new HashSet<string>(
new[]
{
"ssh", "profiles", "actions", "keys", "shell", "config", "help",
"add", "run", "use", "manage", "remove", "rename", "copy", "export", "import",
"install", "generate", "scan", "copy-path", "copy-pub"
},
StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Removes a command prefix that the user pasted again after selecting a menu item.
/// Repeats until nested duplicates are gone.
/// </summary>
public static string NormalizeNestedCommandInput(
string input,
string actionKeyword,
string commandPath)
{
var value = (input ?? string.Empty).Trim();
var canonical = (commandPath ?? string.Empty).Trim();
var keyword = (actionKeyword ?? string.Empty).Trim();
if (string.IsNullOrEmpty(value) || string.IsNullOrEmpty(canonical))
return value;
var prefixes = string.IsNullOrEmpty(keyword)
? new[] { canonical }
: new[] { keyword + " " + canonical, canonical };
bool changed;
do
{
changed = false;
foreach (var prefix in prefixes)
{
if (value.Equals(prefix, StringComparison.OrdinalIgnoreCase))
return string.Empty;
var prefixWithSpace = prefix + " ";
if (value.StartsWith(prefixWithSpace, StringComparison.OrdinalIgnoreCase))
{
value = value.Substring(prefixWithSpace.Length).TrimStart();
changed = true;
break;
}
}
}
while (changed && !string.IsNullOrEmpty(value));
return value;
}
/// <summary>
/// Returns true for concise names that are safe to use inside QuickSSH query navigation.
/// Unicode letters and digits are supported; separators are limited to dot, dash, and underscore.
/// </summary>
public static bool IsValidSavedName(string name)
{
if (string.IsNullOrWhiteSpace(name) || name.Length > 64)
return false;
if (!char.IsLetterOrDigit(name[0]))
return false;
foreach (var c in name)
{
if (char.IsLetterOrDigit(c) || c == '-' || c == '_' || c == '.')
continue;
return false;
}
return true;
}
/// <summary>Returns true when a name collides with a QuickSSH command or sub-command.</summary>
public static bool IsReservedSavedName(string name) =>
!string.IsNullOrWhiteSpace(name) && ReservedNames.Contains(name.Trim());
/// <summary>Finds the stored spelling of a key using case-insensitive matching.</summary>
public static string FindExistingName<T>(
IEnumerable<KeyValuePair<string, T>> entries,
string candidate)
{
if (entries == null || string.IsNullOrWhiteSpace(candidate))
return null;
foreach (var entry in entries)
{
if (string.Equals(entry.Key, candidate.Trim(), StringComparison.OrdinalIgnoreCase))
return entry.Key;
}
return null;
}
}
}