Download and copy sync actions print remote file names without escape_control_chars
メンテナーはふだん 1 日以内に返信
評価
調査の方向性
b2sdk/_internal/sync/action.pyを読み、343行目と433行目の完了時の呼び出し、および60行目の共通エラーパスから確認してください。すでにエスケープされている呼び出しと比較します。出力経路を理解するため、scan/report.pyのProgressReport._print_lineを確認してください。ダウンロード、コピー、エラーの出力で制御文字がエスケープされ、端末出力に生の制御文字が含まれないことをテストで確認できれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Description
Four of the six completion call sites in b2sdk/_internal/sync/action.py pass the remote name through escape_control_chars first, at :179 (upload), :259 (hide), :482 and :523 (delete). The download and copy sites concatenate it raw: reporter.print_completion('dnload ' + self.source_path.relative_path) at :343 and reporter.print_completion('copy ' + self.source_path.relative_path) at :433. The shared error path at :60 is unescaped too, and str(self) there embeds the raw b2_file_name via B2DownloadAction.__str__ at :346. From any of those the string reaches ProgressReport._print_line (scan/report.py:144-155), whose own docstring states the precondition the callers violate, ":param line: a string without a \r or \n in it", and which writes it straight out with self.stdout.write(line). Nothing on the inbound path strips control characters: validate_b2_file_name_as_path covers relative and absolute path components only, and RawApi.check_b2_filename (raw_api.py:991) does reject code points below 32 but has exactly one caller, the upload path at raw_api.py:1059, so it never runs on a name the SDK receives. The effect is that a file name from the endpoint is written to the user's terminal with its control characters intact and the terminal acts on them, which covers title spoofing, colour changes that persist for everything printed afterwards, screen clears and more on emulators that still implement them; all ten control characters tested reached stdout raw. Worth being clear about the limit: a well-behaved Backblaze bucket cannot hold such a name, since the documentation states that "character codes below U+0020 (decimal 32) are not allowed" and "DEL characters (127) are not allowed" and the service enforces that on upload independently of the client, so reaching this needs a B2-compatible endpoint under the attacker's control via a custom realm, an active TLS interception, or a future server-side regression. That is why I am filing it in the open rather than privately.
Reproduction
On master @ ffa02b32 with Python 3.10.19, in a container built from a fresh clone at that ref. RawSimulator is the SDK's own public test double, exported from b2sdk.v2 and b2sdk.v3, and it stands in for exactly one thing here, the endpoint's own name validation, which is the single element the real service would supply. Build with this Dockerfile, save the script as poc.py, then run docker build -t b2poc . && docker run --rm -u 1001 -v "$PWD/poc.py:/t.py:ro" b2poc python /t.py; replacing SyncReport(out, True) with SyncReport(sys.stdout, True) sends the same bytes to a real terminal:
FROM python:3.10.19-slim-bookworm
RUN apt-get update -qq && apt-get install -y -qq --no-install-recommends git >/dev/null
RUN git clone -q https://github.com/Backblaze/b2-sdk-python.git /src \
&& git -C /src checkout -q ffa02b3264bc0e7069ef2c8b25facd9a467e65a6 \
&& pip install --no-cache-dir -q /src
RUN useradd -m -u 1001 victim
import io, os, time
from b2sdk.v3 import (B2Api, B2HttpApiConfig, RawSimulator, StubAccountInfo,
Synchronizer, SyncReport, B2Folder, LocalFolder)
NAME = '\x1b]0;PWNED\x07\x1b[31minvoice.pdf' # a name the endpoint returns in b2_list_file_versions
api = B2Api(StubAccountInfo(), api_config=B2HttpApiConfig(_raw_api_class=RawSimulator))
acct, key = api.session.raw_api.create_account()
api.authorize_account(application_key_id=acct, application_key=key, realm='production')
bucket = api.create_bucket('evil', 'allPublic')
bucket.upload_bytes(b'x', NAME)
bucket.upload_bytes(b'x', 'ordinary.txt')
os.makedirs('/tmp/dest', exist_ok=True)
out = io.StringIO()
Synchronizer(max_workers=1).sync_folders(source_folder=B2Folder('evil', '', api),
dest_folder=LocalFolder('/tmp/dest'), now_millis=int(time.time() * 1000),
reporter=SyncReport(out, True))
for line in out.getvalue().split('\n'):
if line.strip():
print('stdout got:', repr(line.rstrip()))
stdout got: 'dnload \x1b]0;PWNED\x07\x1b[31minvoice.pdf'
stdout got: 'dnload ordinary.txt'
Suggested remediation
Wrap the two names and the error path, matching the four sites that already do:
--- a/b2sdk/_internal/sync/action.py
+++ b/b2sdk/_internal/sync/action.py
@@ -57,7 +57,9 @@ class AbstractAction(metaclass=ABCMeta):
self.do_report(bucket, reporter)
except Exception as e:
logger.exception('an exception occurred in a sync action')
- reporter.error(str(self) + ': ' + repr(e) + ' ' + str(e))
+ reporter.error(
+ f'{escape_control_chars(str(self))}: {e!r} {escape_control_chars(str(e))}'
+ )
raise # Re-throw so we can identify failed actions
@@ -340,7 +342,7 @@ class B2DownloadAction(AbstractAction):
- reporter.print_completion('dnload ' + self.source_path.relative_path)
+ reporter.print_completion(f'dnload {escape_control_chars(self.source_path.relative_path)}')
@@ -430,7 +432,7 @@ class B2CopyAction(AbstractAction):
- reporter.print_completion('copy ' + self.source_path.relative_path)
+ reporter.print_completion(f'copy {escape_control_chars(self.source_path.relative_path)}')
repr(e) is deliberately left unescaped, written as {e!r}, because Python's repr of a string already renders control characters as escapes and it was measured not to leak ESC, whereas str(e) does leak it and so is wrapped.
Credits
- 主要言語
- Python
- スター
- 214
- フォーク
- 68
- 平均マージ
- 51分
- マージ済み PR(30日)
- 6
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
Backblaze/b2-sdk-python のほかの issue
-
SqliteAccountInfo profile name validation uses "re.match", so only the first character is checked対応中かも @nazarli-shabnam が 9 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
Backblaze/b2-sdk-python#629 ·
メンテナーはふだん 1 日以内に返信
-
Adopt the AI-native SDLC and repository policies対応中かも @goanpeca が 18 日前に担当しました。 オープンenhancement
難易度 5/5 1週間以上 初心者へのやさしさ 20/100
Backblaze/b2-sdk-python#634 ·
メンテナーはふだん 1 日以内に返信
-
難易度 4/5 3〜5日 初心者へのやさしさ 50/100
Backblaze/b2-sdk-python#630 ·
メンテナーはふだん 1 日以内に返信
-
ci: cli-compat fails — 4 sse/sync integration tests broken in B2_Command_Line_Tool (all environments)再び着手できるかも @ffumero2003 が 37 日前に担当しましたが、オープン中のプルリクエストはありません。 オープンbug testing
Backblaze/b2-sdk-python#627 · コメント 2 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
難易度 3/5 半日 初心者へのやさしさ 62/100
Backblaze/b2-sdk-python#626 ·
メンテナーはふだん 1 日以内に返信
Backblaze/b2-sdk-python の issue をすべて見る
似ている issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 72/100
btclib-org/btclib-node#2505 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
open-webui/open-webui#34462 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
gorakhargosh/watchdog#1462 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
openwisp/openwisp-monitoring#889 ·
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
IETF-Hackathon/pqc-certificates#351 ·
メンテナーはふだん 2 日以内に返信