Skip to content

Commit

Permalink
fix(gnoweb): escape bash chars in help args (#3672)
Browse files Browse the repository at this point in the history
This PR ensures that special characters like ! and ? in user inputs are
properly escaped when generating commands in the docs page ($help).
Previously, entering ! or any other special char could cause the command
string to break by omitting a closing ", making it invalid.

This fix applies proper escaping to prevent such issues, ensuring that
generated commands remain valid and executable.

The fix introduces an escaping function that handles shell-sensitive
characters before inserting them into the generated command strings.
This approach ensures the commands remain intact without affecting their
output when executed. Thus, the escape char is also removed from the cmd
when the shell-sensitive char is removed from the arg input.

cf: [issue
3355](#3355 (comment))
  • Loading branch information
alexiscolin authored Feb 6, 2025
1 parent 0b76b0b commit 0bc4423
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion gno.land/pkg/gnoweb/components/layouts/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
<form class="sidemenu col-span-3 flex justify-end lg:justify-start gap-3 sm:gap-6 h-full text-100 text-gray-400">{{ range .Links }} {{ template "ui/header_link" . }} {{ end }}</form>
</nav>
</header>
{{ end }}
{{ end }}
9 changes: 5 additions & 4 deletions gno.land/pkg/gnoweb/frontend/js/realmhelp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { debounce } from "./utils";
import { debounce, escapeShellSpecialChars } from "./utils";

class Help {
private DOM: {
Expand Down Expand Up @@ -67,7 +67,7 @@ class Help {

localStorage.setItem("helpAddressInput", address);
this.funcList.forEach((func) => func.updateAddr(address));
});
}, 50);
addressInput?.addEventListener("input", () => debouncedUpdate(addressInput));

cmdModeSelect?.addEventListener("change", (e) => {
Expand Down Expand Up @@ -124,7 +124,7 @@ class HelpFunc {
private bindEvents(): void {
const debouncedUpdate = debounce((paramName: string, paramValue: string) => {
if (paramName) this.updateArg(paramName, paramValue);
});
}, 50);

this.DOM.el.addEventListener("input", (e) => {
const target = e.target as HTMLInputElement;
Expand All @@ -143,10 +143,11 @@ class HelpFunc {
}

public updateArg(paramName: string, paramValue: string): void {
const escapedValue = escapeShellSpecialChars(paramValue);
this.DOM.args
.filter((arg) => arg.dataset.arg === paramName)
.forEach((arg) => {
arg.textContent = paramValue || "";
arg.textContent = escapedValue || "";
});
}

Expand Down
4 changes: 4 additions & 0 deletions gno.land/pkg/gnoweb/frontend/js/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ export function debounce<T extends (...args: any[]) => void>(func: T, delay: num
}, delay);
};
}

export function escapeShellSpecialChars(arg: string): string {
return arg.replace(/([$`"\\!|&;<>*?{}()])/g, "\\$1");
}
2 changes: 1 addition & 1 deletion gno.land/pkg/gnoweb/public/js/realmhelp.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gno.land/pkg/gnoweb/public/js/utils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0bc4423

Please sign in to comment.