diff --git a/.gitattributes b/.gitattributes index 53c3832..c6798f0 100644 --- a/.gitattributes +++ b/.gitattributes @@ -7,6 +7,7 @@ # Binary assets *.png binary +*.ico binary *.gif binary *.pfx binary *.p12 binary diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e6ff894..a33a43a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -90,7 +90,7 @@ jobs: mkdir -p dist cp out/termrest* dist/ 2>/dev/null || true rm -rf dist/*.pdb dist/*.dbg dist/*.dSYM - cp README.md dist/ + cp README.md LICENSE dist/ ls -la dist - uses: actions/upload-artifact@v4 with: diff --git a/Directory.Build.props b/Directory.Build.props index 065e664..720f9b6 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -8,6 +8,7 @@ true 0.1.0 TermRest + Copyright (c) 2026 Shike Chen A lightweight, mouse-friendly terminal REST client with Postman-compatible collections. en en diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..27ff3a2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 Shike Chen + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index 1e13505..3290c27 100644 --- a/README.md +++ b/README.md @@ -186,7 +186,7 @@ A runtime answers in about a millisecond once started; starting takes 0.1–0.3 **Choosing the runtimes** - **In the UI:** *Tools › Scripts…*, one row for each language. Type a command (`python3`, `node`, `bun`, `dotnet`, `pwsh`) or a path, which may be relative to the workspace (`.venv/bin/python`, `.venv\Scripts\python.exe`). A command is looked up in the folders of PATH only (on Windows as `name.exe`, unless the name already ends with a program extension from PATHEXT, such as `.cmd`: `python3.12` is `python3.12.exe`; for pyenv-win's shim, type `python.bat`), never in the workspace or TermRest's own folder. A file picked with *Browse…* in the workspace's own folder is kept as `./name`, a path. Or press **Find**: for Python, it looks for `.venv`, `venv` and `env` in the workspace, then `python3` and `python` on PATH; for JavaScript, `node` and `bun` on PATH; for C#, `dotnet` on PATH, then `~/.dotnet`; for PowerShell, `pwsh` on PATH, then `C:\Program Files\PowerShell\7` on Windows. A Python found in the workspace is filled in without being run, as it may have come with the repository: **Check** runs it and shows the version. Python 3.8, Node.js 18 or Bun 1.1, the .NET 10 SDK, and PowerShell 7.0 or later are needed (for C#, the SDK: a .NET runtime alone cannot build scripts; for PowerShell, `pwsh`: Windows PowerShell 5.1, `powershell.exe`, is too old). -- **Your choices are kept per workspace folder in your user settings**, never in the workspace. A cloned repository cannot choose a program for TermRest to run. +- **Your choices are kept per workspace folder in your user settings**, never in the workspace. A cloned repository cannot choose a program for TermRest to run. Outside Windows, a folder is known by its path exactly as written: a path in another case may be another folder (on Linux, and on a macOS volume formatted case-sensitive), so neither these choices nor the proxy and files from outside the folder you allowed carry over to it. - **On the command line:** `--python `, `--javascript `, `--csharp ` and `--powershell ` on `send` and `run`; without them, the UI's choices for that workspace apply. A path there is relative to the current folder, as the command line's other paths are (from a repository's root: `termrest run postman/api.postman_collection.json --python .venv/bin/python`); a command such as `node` is looked up on PATH. `--script-order ` changes the [language order](#languages) for the run. `--no-scripts` turns scripts off. - **No runtime chosen for a language:** in the UI, its scripts are skipped and the request is sent; the status line says `⚠ scripts not run` and the *Scripts* tab says which. On the command line, that is an error (so a CI run never passes without its tests): the request is not sent, and the message says to pass `--python`, `--javascript`, `--csharp`, `--powershell` or `--no-scripts`. A missing runtime does not make a request choose another language: the language depends on the scripts only (see below). @@ -771,3 +771,7 @@ Notes: - Proxy authentication. - Code snippets. - **Packaging:** winget, Homebrew, `.deb`/`.rpm`, and signed binaries. + +## License + +TermRest is released under the [MIT License](LICENSE). Its icon is in [`assets`](assets): `termrest.svg` is the source, and `termrest.ico` and `termrest.png` are drawn from it. diff --git a/assets/termrest.ico b/assets/termrest.ico new file mode 100644 index 0000000..ddacba7 Binary files /dev/null and b/assets/termrest.ico differ diff --git a/assets/termrest.png b/assets/termrest.png new file mode 100644 index 0000000..1c173ce Binary files /dev/null and b/assets/termrest.png differ diff --git a/assets/termrest.svg b/assets/termrest.svg new file mode 100644 index 0000000..6f957af --- /dev/null +++ b/assets/termrest.svg @@ -0,0 +1,9 @@ + + TermRest + + + + + + + diff --git a/src/TermRest.Cli/TermRest.Cli.csproj b/src/TermRest.Cli/TermRest.Cli.csproj index ed29327..b951fb5 100644 --- a/src/TermRest.Cli/TermRest.Cli.csproj +++ b/src/TermRest.Cli/TermRest.Cli.csproj @@ -11,6 +11,11 @@ true false false + + ..\..\assets\termrest.ico diff --git a/src/TermRest.Core/Import/ShellWords.cs b/src/TermRest.Core/Import/ShellWords.cs index 0835900..5471e7b 100644 --- a/src/TermRest.Core/Import/ShellWords.cs +++ b/src/TermRest.Core/Import/ShellWords.cs @@ -23,7 +23,7 @@ public static class ShellWords { /// /// Guesses the shell from continuation characters and Windows-only escapes outside quoted values (see - /// and ); defaults to POSIX. + /// , and ); defaults to POSIX. /// public static ShellDialect Detect(string text) { @@ -43,9 +43,19 @@ public static ShellDialect Detect(string text) return ShellDialect.Posix; } - if (HasPowerShellContinuation(text)) + // A quoted value left open may be one another shell closes, or never opens: PowerShell's "…" ends at the quote after + // a \ ("C:\temp\"), and cmd.exe has no '…' (foo'bar). A line continuation outside that shell's quoted values tells. + if (marks.Unclosed) { - return ShellDialect.PowerShell; + if (HasPowerShellContinuation(text)) + { + return ShellDialect.PowerShell; + } + + if (HasCmdContinuation(text)) + { + return ShellDialect.Cmd; + } } var start = text.TrimStart(); @@ -80,10 +90,11 @@ public static ShellDialect Detect(string text) /// curl ^"https://…^"), outside quoted values, which say nothing about the shell: '…' (PowerShell's '' inside one /// ends it and starts another), $'…' (where \ escapes the next character) and "…" (where \ and ` do). Outside them, \, ^ /// and ` escape the next character, so \' and ^" start no quoted value, and a # that starts a word starts a comment. + /// Also whether a quoted value is left open, as the text ends before its closing quote. /// - private static (bool Backtick, bool Caret, bool Backslash, bool CaretQuote) FindMarks(string text) + private static (bool Backtick, bool Caret, bool Backslash, bool CaretQuote, bool Unclosed) FindMarks(string text) { - bool backtick = false, caret = false, backslash = false, caretQuote = false; + bool backtick = false, caret = false, backslash = false, caretQuote = false, unclosed = false; for (var i = 0; i < text.Length; i++) { var wordStart = i == 0 || IsBlank(text[i - 1]); @@ -102,12 +113,15 @@ private static (bool Backtick, bool Caret, bool Backslash, bool CaretQuote) Find break; case '\'': i = QuotedEnd(text, i + 1, '\'', ""); + unclosed |= i >= text.Length; break; case '$' when i + 1 < text.Length && text[i + 1] == '\'': i = QuotedEnd(text, i + 2, '\'', "\\"); + unclosed |= i >= text.Length; break; case '"': i = QuotedEnd(text, i + 1, '"', "\\`"); + unclosed |= i >= text.Length; break; case '#' when wordStart: while (i + 1 < text.Length && text[i + 1] != '\n') @@ -119,7 +133,7 @@ private static (bool Backtick, bool Caret, bool Backslash, bool CaretQuote) Find } } - return (backtick, caret, backslash, caretQuote); + return (backtick, caret, backslash, caretQuote, unclosed); } /// Where the quoted value whose text starts at ends: its closing quote, or the end of the text. @@ -174,6 +188,33 @@ private static bool HasPowerShellContinuation(string text) return false; } + /// + /// Whether a ^ ends a line outside quoted values as cmd.exe reads them: only "…", in which ^ is a character. An apostrophe + /// is a character there too (foo'bar), where starts a quoted value that hides the ^ ending a line. + /// + private static bool HasCmdContinuation(string text) + { + var quoted = false; + for (var i = 0; i < text.Length; i++) + { + if (text[i] == '"') + { + quoted = !quoted; + } + else if (text[i] == '^' && !quoted) + { + if (NewlineLength(text, i + 1) > 0) + { + return true; + } + + i++; + } + } + + return false; + } + /// Length of the line break starting at (LF or CRLF), or 0. private static int NewlineLength(string text, int index) => index >= text.Length ? 0 diff --git a/src/TermRest.Core/Postman/V3/V3Names.cs b/src/TermRest.Core/Postman/V3/V3Names.cs index a7cd869..2140335 100644 --- a/src/TermRest.Core/Postman/V3/V3Names.cs +++ b/src/TermRest.Core/Postman/V3/V3Names.cs @@ -1,3 +1,5 @@ +using System.Runtime.ExceptionServices; + namespace TermRest.Core.Postman.V3; /// File and folder naming rules of the Postman collection format v3 layout. @@ -84,18 +86,45 @@ public static bool IsCollectionDirectory(string directory) => private static bool HasContent(string directory) => File.Exists(DefinitionPath(directory)) || Directory.EnumerateFiles(directory, "*.request.y*ml").Any(IsRequestFile); - /// Whether folders of (not hidden ones, nor links) have definitions or requests. + /// + /// Whether folders of (not hidden ones, nor links) have definitions or requests. A folder that + /// cannot be listed (another user's) may hold them: when no other folder has any, its error is thrown, so that loading + /// says why rather than the collection being left out without a word. + /// private static bool FoldersHaveContent(string directory, int depth) { - try + if (depth <= 0) + { + return false; + } + + Exception? unreadable = null; + foreach (var sub in Directory.EnumerateDirectories(directory, "*", FolderSearch)) { - return depth > 0 && Directory.EnumerateDirectories(directory, "*", FolderSearch) - .Any(sub => !Path.GetFileName(sub).StartsWith('.') && (HasContent(sub) || FoldersHaveContent(sub, depth - 1))); + if (Path.GetFileName(sub).StartsWith('.')) + { + continue; + } + + try + { + if (HasContent(sub) || FoldersHaveContent(sub, depth - 1)) + { + return true; + } + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + unreadable ??= ex; // another folder may have them + } } - catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + + if (unreadable is not null) { - return false; + ExceptionDispatchInfo.Throw(unreadable); } + + return false; } /// Whether is in a postman/collections folder, where Postman keeps collections. diff --git a/src/TermRest.Core/Workspace/UserSettings.cs b/src/TermRest.Core/Workspace/UserSettings.cs index ccf4595..a9cf8c2 100644 --- a/src/TermRest.Core/Workspace/UserSettings.cs +++ b/src/TermRest.Core/Workspace/UserSettings.cs @@ -191,8 +191,13 @@ public bool AllowOutsideFiles(string workspaceRoot, bool allow) return true; } + /// + /// Whether two workspace folders are the same, for the choices kept for them: on Windows without regard to case, as its + /// file systems are; elsewhere by their paths exactly, as on Linux and on a case-sensitive macOS volume work/api and + /// work/API are two folders (a repository cloned next to a trusted one must not get its runtimes or approvals). + /// private static bool SameFolder(string a, string b) => - string.Equals(NormalizeFolder(a), NormalizeFolder(b), OperatingSystem.IsWindows() || OperatingSystem.IsMacOS() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); + string.Equals(NormalizeFolder(a), NormalizeFolder(b), OperatingSystem.IsWindows() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); private static string NormalizeFolder(string path) { diff --git a/tests/TermRest.Core.Tests/CurlImporterTests.cs b/tests/TermRest.Core.Tests/CurlImporterTests.cs index 4ee5b37..278be9d 100644 --- a/tests/TermRest.Core.Tests/CurlImporterTests.cs +++ b/tests/TermRest.Core.Tests/CurlImporterTests.cs @@ -461,6 +461,29 @@ public void A_powershell_value_that_ends_in_a_backslash_ends_at_its_quote() Assert.Equal(("https://h/upload", "C:\\temp\\"), (result.Spec.Url, result.Spec.Body.Raw)); } + [Fact] + public void A_cmd_value_that_holds_an_apostrophe_keeps_the_lines_that_follow() + { + // cmd.exe has no '…': an apostrophe is a character there, where the other shells start a quoted value with it. + var result = CurlImporter.Import("curl https://h/x --data foo'bar ^\n -H X-Test:ok"); + + Assert.Equal(ShellDialect.Cmd, result.Dialect); + Assert.Equal(("foo'bar", "ok"), (result.Spec.Body.Raw, Header(result.Spec, "X-Test"))); + } + + [Theory] + [InlineData("curl https://h/x -d 'it'\\''s ```\nx'", "it's ```\nx")] + [InlineData("curl https://h/x -d 'a ^\nb'", "a ^\nb")] + public void A_line_that_ends_inside_a_closed_quoted_value_says_nothing_about_the_shell(string command, string body) + { + // Read as PowerShell or cmd.exe would read it, the quotes of 'it'\''s fall elsewhere: only a value they leave open + // has them try other shells' quoting. + var result = CurlImporter.Import(command); + + Assert.Equal(ShellDialect.Posix, result.Dialect); + Assert.Equal(body, result.Spec.Body.Raw); + } + [Fact] public void Browser_escapes_of_emoji_keep_the_character() { diff --git a/tests/TermRest.Core.Tests/PostmanV3Tests.cs b/tests/TermRest.Core.Tests/PostmanV3Tests.cs index e781c93..8dc33c6 100644 --- a/tests/TermRest.Core.Tests/PostmanV3Tests.cs +++ b/tests/TermRest.Core.Tests/PostmanV3Tests.cs @@ -157,6 +157,38 @@ public void Links_of_a_workspace_to_folders_that_are_gone_are_load_errors() } /// Lets the current user list , or not. + [Fact] + public void A_collection_whose_requests_are_in_a_folder_that_cannot_be_listed_is_a_load_error() + { + // Without a definition, only its folders tell a folder of postman/collections is a collection: one that cannot be + // listed (another user's) may hold its requests, so the collection is not left out without a word. + var root = TestFiles.NewTempDirectory(); + var cart = Directory.CreateDirectory(Path.Combine(root, "postman", "collections", "Shop", "Cart")).FullName; + File.WriteAllText(Path.Combine(cart, "Add item.request.yaml"), "$kind: http-request\nurl: https://shop.example.com/cart/items\nmethod: POST\n"); + Listable(cart, false); + try + { + try + { + Directory.GetFileSystemEntries(cart); + Assert.Skip("This user can list any folder (root)."); + } + catch (UnauthorizedAccessException) + { + // as for other users + } + + var workspace = ApiWorkspace.Open(root); + + Assert.Empty(workspace.Collections); + Assert.Contains(workspace.LoadErrors, e => e.StartsWith("Shop: ", StringComparison.Ordinal)); + } + finally + { + Listable(cart, true); + } + } + private static void Listable(string folder, bool listable) { if (OperatingSystem.IsWindows()) diff --git a/tests/TermRest.Core.Tests/WorkspaceAndCollectionTests.cs b/tests/TermRest.Core.Tests/WorkspaceAndCollectionTests.cs index e674188..9f8fb9a 100644 --- a/tests/TermRest.Core.Tests/WorkspaceAndCollectionTests.cs +++ b/tests/TermRest.Core.Tests/WorkspaceAndCollectionTests.cs @@ -6,6 +6,7 @@ using TermRest.Core.Json; using TermRest.Core.Model; using TermRest.Core.Postman; +using TermRest.Core.Scripting; using TermRest.Core.Tests.Infrastructure; using TermRest.Core.Workspace; using Xunit; @@ -251,6 +252,26 @@ public void Requests_send_only_the_files_of_their_workspace_until_others_are_all Assert.Null(user.OutsideFilesAllowed); } + [Fact] + public void Approvals_for_a_workspace_folder_do_not_cover_its_path_in_another_case_outside_windows() + { + // On a case-sensitive volume (Linux, and macOS volumes formatted so), work/api and work/API are two folders: a + // repository cloned next to a trusted one must not get its script runtime, its proxy or files from outside it. + var parent = TestFiles.NewTempDirectory(); + var root = Path.Combine(parent, "api"); + var otherCase = Path.Combine(parent, "API"); + const string Proxy = "http://proxy.example:3128"; + var user = new UserSettings(); + user.SetScriptRuntime(ScriptLanguage.Python, root, "python3"); + user.AllowProxy(root, Proxy); + user.AllowOutsideFiles(root, true); + + var sameFolder = OperatingSystem.IsWindows(); + Assert.Equal(sameFolder, user.GetScriptRuntime(ScriptLanguage.Python, otherCase) is not null); + Assert.Equal(sameFolder, user.IsProxyAllowed(otherCase, Proxy)); + Assert.Equal(sameFolder, user.AreOutsideFilesAllowed(otherCase)); + } + [Fact] public void A_link_that_climbs_out_of_the_workspace_through_another_link_is_not_sent() {