-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
400 additions
and
747 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
|
||
public static class ExecService | ||
{ | ||
private static readonly HashSet<string> AllowedCommands = new HashSet<string> | ||
{ | ||
"ls", | ||
"cat" | ||
}; | ||
|
||
public static ExecResponse Exec(ExecInput input) | ||
{ | ||
try | ||
{ | ||
if (!AllowedCommands.Contains(input.Command)) | ||
{ | ||
return new ExecResponse | ||
{ | ||
Error = $"Command '{input.Command}' is not allowed. Only 'ls' and 'cat' are permitted." | ||
}; | ||
} | ||
|
||
if (!input.Arg.StartsWith("./")) | ||
{ | ||
return new ExecResponse | ||
{ | ||
Error = "Can only access paths starting with ./" | ||
}; | ||
} | ||
|
||
var startInfo = new ProcessStartInfo | ||
{ | ||
FileName = input.Command, | ||
Arguments = input.Arg, | ||
RedirectStandardOutput = true, | ||
RedirectStandardError = true, | ||
UseShellExecute = false, | ||
CreateNoWindow = true | ||
}; | ||
|
||
using var process = Process.Start(startInfo); | ||
var stdout = process.StandardOutput.ReadToEnd().Trim(); | ||
var stderr = process.StandardError.ReadToEnd().Trim(); | ||
process.WaitForExit(); | ||
|
||
return new ExecResponse | ||
{ | ||
Stdout = stdout, | ||
Stderr = stderr | ||
}; | ||
} | ||
catch (Exception ex) | ||
{ | ||
return new ExecResponse | ||
{ | ||
Error = ex.Message | ||
}; | ||
} | ||
} | ||
} | ||
|
||
public class ExecInput | ||
{ | ||
public string Command { get; set; } | ||
public string Arg { get; set; } | ||
} | ||
|
||
public class ExecResponse | ||
{ | ||
public string Stdout { get; set; } | ||
public string Stderr { get; set; } | ||
public string Error { get; set; } | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.