-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SNOW-1055591: Sanitize terminal output to avoid ASCII escapes (#1238)
- Loading branch information
1 parent
4e50d22
commit 3a67096
Showing
8 changed files
with
239 additions
and
2 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Copyright (c) 2024 Snowflake Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from __future__ import annotations | ||
|
||
import re | ||
|
||
# 7-bit C1 ANSI sequences | ||
_ANSI_ESCAPE = re.compile( | ||
r""" | ||
\x1B # ESC | ||
(?: # 7-bit C1 Fe (except CSI) | ||
[@-Z\\-_] | ||
| # or [ for CSI, followed by a control sequence | ||
\[ | ||
[0-?]* # Parameter bytes | ||
[ -/]* # Intermediate bytes | ||
[@-~] # Final byte | ||
) | ||
""", | ||
re.VERBOSE, | ||
) | ||
|
||
|
||
def sanitize_for_terminal(text: str) -> str | None: | ||
""" | ||
Escape ASCII escape codes in string. This should be always used | ||
when printing output to terminal. | ||
""" | ||
if text is None: | ||
return None | ||
return _ANSI_ESCAPE.sub("", text) |
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
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,24 @@ | ||
# serializer version: 1 | ||
# name: test_snow_typer_help_sanitization | ||
''' | ||
|
||
Usage: root [OPTIONS] COMMAND [ARGS]... | ||
|
||
╭─ Options ────────────────────────────────────────────────────────────────────╮ | ||
│ --install-completion Install completion for the current shell. │ | ||
│ --show-completion Show completion for the current shell, to │ | ||
│ copy it or customize the installation. │ | ||
│ --help -h Show this message and exit. │ | ||
╰──────────────────────────────────────────────────────────────────────────────╯ | ||
╭─ Commands ───────────────────────────────────────────────────────────────────╮ | ||
│ '' doc 4 │ | ||
│ '🤯?' doc 3 │ | ||
│ func1 doc 1 '' │ | ||
╰──────────────────────────────────────────────────────────────────────────────╯ | ||
╭─ '' ─────────────────────────────────────────────────────────────────────────╮ | ||
│ func2 '' │ | ||
╰──────────────────────────────────────────────────────────────────────────────╯ | ||
|
||
|
||
''' | ||
# --- |
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,68 @@ | ||
# Copyright (c) 2024 Snowflake Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import pytest | ||
from snowflake.cli.api.commands.snow_typer import SnowTyper | ||
from snowflake.cli.api.sanitizers import sanitize_for_terminal | ||
from typer.testing import CliRunner | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"text, expected", | ||
[ | ||
("'\033[0i\007'", "'\x07'"), | ||
("'\033[5i\007'", "'\x07'"), | ||
("'🤯\033[1000;b\077'", "'🤯?'"), | ||
( | ||
"'\033[H\007''\033]1337;ClearScrollback\077''\033[2J\007''\033[1;31m'Error, enable MaliciousPlugin in your config'\033[#F\007'", | ||
"'\x07''1337;ClearScrollback?''\x07'''Error, enable MaliciousPlugin in your config'\x07'", | ||
), | ||
], | ||
) | ||
def test_sanitize_for_terminal(text, expected): | ||
result = sanitize_for_terminal(text) | ||
assert result == expected | ||
|
||
|
||
def test_snow_typer_help_sanitization(snapshot): | ||
app = SnowTyper() | ||
|
||
escape_text = "'\033[0i\007'" | ||
|
||
@app.command(epilog=escape_text, options_metavar=escape_text) | ||
def func1(): | ||
"""doc 1 '\033[0i\007'""" | ||
return 42 | ||
|
||
@app.command(help=escape_text, rich_help_panel=escape_text, short_help=escape_text) | ||
def func2(): | ||
return escape_text | ||
|
||
# Escape as arg not kwarg | ||
@app.command("'🤯\033[1000;b\077'") | ||
def func3(): | ||
"""doc 3""" | ||
return 42 | ||
|
||
@app.command(name="'\033[5i\007'") | ||
def func4(): | ||
"""doc 4""" | ||
return 42 | ||
|
||
runner = CliRunner() | ||
result = runner.invoke(app, ["--help"]) | ||
assert result.output == snapshot | ||
|
||
result = runner.invoke(app, ["func2"]) | ||
assert result.output == "" |
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