CI / test (push) Canceled after 0s
## Summary Nhánh `feature/perf-ui-logic`: tối ưu hiệu năng/UI, sửa lỗi workspace và điều hướng, và làm cho công tắc **"Block network for agent-run commands"** chặn thật mọi đường ra mạng của app, **trừ nhà cung cấp AI**. **Chặn mạng (b78d483,8c497cf,10b8379)** - Bộ kiểm tra chung `application/network/network_guard.py`, nối vào cấu hình đang chạy ở Composition Root: đổi công tắc trong Settings là có hiệu lực ngay. - Lệnh shell của agent và task script chạy trong **Windows AppContainer không có quyền mạng**: kernel chặn socket, ping, DNS, Invoke-WebRequest… Không cần quyền admin. Không cô lập được thì lệnh bị từ chối, không chạy khi mạng còn mở. macOS dùng `sandbox-exec`, Linux dùng `unshare --net`. - Bật chặn thì: dừng MCP đang chạy, không khởi động server mới, từ chối lời gọi connector; Microsoft 365 (đăng nhập, Graph, đồng bộ cloud, rules, mail), Teams, nút Test REST/Jira/MCP, link đính kèm task, pip tự cài và tài nguyên web trong xem trước HTML đều bị từ chối. - Vẫn dùng được: chat, tải danh sách model, thử model; tool OneDrive đã đồng bộ trên máy. - Công tắc **mặc định tắt** khi mở app lần đầu; nhãn giữ nguyên như cũ. - Xem trước HTML trong tab Folder giờ hiện được ảnh/CSS/JS từ web khi mạng mở (trước đây trang `file://` không tải được). - Sửa lỗi app văng khi chuyển tab Graph → Folder: profile WebEngine của trang xem trước bị huỷ trước trang (`0xc0000409` trong Qt6Core.dll); giờ dùng một profile chung thuộc QApplication. - Không cấp quyền AppContainer kế thừa lên thư mục chứa PySide6 (nếu có, Chromium không nạp được `Qt6WebEngineCore.dll` và tab Graph trắng). - Cột mục lục trong Settings tính độ rộng theo kiểu chữ của mục đang chọn, "Sandbox Security Layer" không còn bị cắt. **Các commit khác trong nhánh** - `b7a41b3` mỗi thư mục làm việc chỉ thuộc về một project · `bbdf146` bật nút Sửa project khi đã có project đang mở - `35f24e0`, `cc8d5c8`, `2e3e719`, `c699beb` canh hàng / khoảng cách thanh điều hướng - `2759ed9` không refresh workspace khi chuyển tab Cowork · `7607f44` checkpoint hiệu năng và UI - `8548c1e` chặn tool mạng của agent · `caf3b74` renderer GraphRAG native trên macOS · `c00b83c` khoảng cách metadata hàng project · `a04f8a9` ẩn picker workspace cloud ## Change Type - [x] Cowork feature - [x] Bug fix - [ ] Core AI contribution - [x] Test / hardening - [x] Performance - [ ] Documentation ## Related Work Cowork Task: Core Repo: http://34.143.229.138/gitea-admin/fsg-ai-core-assets Core AI Issue: Core Task: Related PR: ## Scope What is intentionally included? - Mọi đường ra mạng do app tự mở, trừ nhà cung cấp AI (xem Summary). - Test: `tests/test_network_guard_lanes.py` (có bài chạy AppContainer thật trên Windows), `tests/ui/test_html_preview_remote_images.py`. What is intentionally NOT included? - Chặn cả nhà cung cấp AI / chạy model trên máy (Phương án 2). - Terminal người dùng tự gõ trong tab Folder, sinh ảnh, cơ chế tự tin chứng chỉ lạ (`tls_trust`). - Huy hiệu trạng thái "đang chặn" trên thanh trên cùng. ## Validation - [x] Unit tests - [x] Integration tests - [x] Manual verification - [x] Regression check Commands / evidence: - `python -m pytest tests/test_network_guard_lanes.py tests/test_sandbox_block_network.py tests/ui -q` → chỉ còn 1 bài fail, fail cả trên `b7a41b3` (nhãn `ProjectRow` 'Project' chưa dịch, `tests/ui/test_i18n_khong_con_chu_cu.py`). - `python -m pytest tests -q --ignore=tests/ui` → 4 bài fail, cả 4 cũng fail trên `b7a41b3` (`test_canonical_audit_logger`, 2 bài `test_mcp_audit_security`, `test_monitoring_tab_container`). - Chạy cả `tests` trong một lượt thì treo ở các test dựng MainWindow trong `tests/ui`; `b7a41b3` cũng treo đúng chỗ đó. - `check_imports.py` và `check_orphan_modules.py` PASS. `check_loc.py` báo 9 file quá dài, giống hệt trước khi sửa (không file nào do nhánh này làm dài thêm). - Kiểm tra tay trên Windows 11: trong AppContainer, Python báo `WinError 10013`, ping/nslookup/PowerShell/curl đều không ra được mạng; cmd, git, python chạy bình thường. - Kiểm tra tay trên Windows 11: xem trước HTML tải được 4/4 tài nguyên web khi mạng mở, 0/4 khi bật chặn; tab Graph hoạt động; tạo/huỷ trang xem trước nhiều lần không còn cảnh báo profile của Qt. ## Security Impact Permission / credential / network / customer data impact: - Network: khi bật công tắc, chỉ nhà cung cấp AI còn ra mạng; nội dung chat vẫn gửi tới nhà cung cấp AI. - Permission: lần đầu chạy lệnh trong sandbox, app **thêm quyền (ACE) cho SID AppContainer** trên thư mục làm việc (ghi), thư mục cài Python gốc (đọc), gốc venv và `Scripts` (đọc). Không xoá quyền nào. Thư mục chứa PySide6 không bao giờ nhận quyền kế thừa; một quyền kế thừa sai trên venv (từ bản dev trước) được tự gỡ. - Credential: không đổi. Khi chặn, trạng thái đăng nhập M365 được đọc thẳng từ kho token trên máy, không dựng MSAL. ## Compatibility - [x] No breaking change - [ ] Breaking change documented Ghi chú: `block_network` mặc định đổi từ bật sang tắt cho cấu hình mới; máy đã lưu `true` thì giữ nguyên. Khi đang chặn, lệnh dùng công cụ cài trong thư mục người dùng (ngoài Program Files) có thể báo Access denied; thư viện trong venv của app không dùng được trong sandbox. ## Reviewer Notes - `infrastructure/sandbox/appcontainer_process.py` gọi Win32 bằng ctypes (CreateAppContainerProfile, CreateProcessW với SECURITY_CAPABILITIES) và dùng `icacls` để cấp quyền: nên xem kỹ phần cấp quyền. - `tests/conftest.py` thêm fixture autouse gỡ `network_guard` sau mỗi test, vì `build_context()` gắn cổng này ở mức process. - `core/task_executors.py` đang đúng bằng trần LOC nên `_run_script` được tách sang `core/task_script.py`. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: minhanhpkpro <minhanhpkpro@gmail.com> Co-authored-by: Duy Le Huu <duylh19@fpt.com> Co-authored-by: thanhnv <thanhnv.ip@gmail.com> Reviewed-on: #13
276 lines
12 KiB
Python
276 lines
12 KiB
Python
"""Microsoft 365 sign-in — real OAuth via MSAL's device-code flow.
|
|
|
|
ZERO-CONFIG "connect like Claude": the user just clicks Sign in, opens a short
|
|
URL, enters a one-time code, and signs in with their own Microsoft account
|
|
(SSO/MFA as their org normally does) — NO Tenant ID / Client ID to type. This
|
|
works because we ship a well-known Microsoft first-party PUBLIC multi-tenant
|
|
client (``DEFAULT_CLIENT_ID`` — the Microsoft Graph PowerShell client, same
|
|
technique the Azure CLI / Graph CLI use) against the ``common`` authority, so
|
|
any work/school (or personal) account can consent to the delegated Graph
|
|
scopes interactively. Device-code flow needs no client secret and no embedded
|
|
browser / redirect URI.
|
|
|
|
Orgs that require their OWN app registration can still override tenant_id /
|
|
client_id in config (``ms365.tenant_id`` / ``ms365.client_id``); when both are
|
|
blank the bundled defaults are used. The signed-in token (+ refresh token) is
|
|
cached in the OS credential store (Windows Credential Manager / macOS Keychain
|
|
/ Linux Secret Service, via ``keyring``) — never written into config.json, and
|
|
never stored in plaintext. With no OS credential store (e.g. a headless Linux
|
|
box), it falls back to a local file at ``TOKEN_CACHE_PATH``.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from typing import Callable, List, Optional
|
|
|
|
from ..config import CONFIG_DIR
|
|
|
|
TOKEN_CACHE_PATH = CONFIG_DIR / "ms365_token_cache.bin" # fallback only — see module docstring
|
|
_KEYRING_SERVICE = "cowork_local_ms365"
|
|
_KEYRING_KEY = "token_cache"
|
|
|
|
# Bundled zero-config sign-in identity. This is Microsoft's OWN public,
|
|
# multi-tenant "Microsoft Graph PowerShell" client — a first-party client that
|
|
# permits the device-code public-client flow and is broadly pre-consented for
|
|
# delegated Graph scopes, so users need not register (or type) any app id. The
|
|
# same well-known-public-client approach the Azure CLI, Graph CLI and many
|
|
# tools use. NOT a secret (public clients have none). Override via config only
|
|
# if the tenant blocks it and mandates a private app registration.
|
|
DEFAULT_CLIENT_ID = "14d82eec-204b-4c2f-b7e8-296a70dab67e"
|
|
# "common" = any Microsoft account (work/school or personal); the user picks
|
|
# which account at sign-in. Use a specific tenant id only to restrict to one org.
|
|
DEFAULT_TENANT = "common"
|
|
|
|
# One shared scope set for every connector — MSAL requests them all at sign-in
|
|
# so switching a connector on later doesn't force a second sign-in. Some
|
|
# (ChannelMessage.Send, OnlineMeetingTranscript.Read.All) need the tenant
|
|
# admin to have consented the app already.
|
|
SCOPES: List[str] = [
|
|
"User.Read",
|
|
"Mail.Read", "Mail.Send",
|
|
"Calendars.Read",
|
|
"Team.ReadBasic.All", "Channel.ReadBasic.All",
|
|
"ChannelMessage.Read.All", "ChannelMessage.Send",
|
|
"Files.Read.All", "Files.ReadWrite.All",
|
|
"Sites.Read.All",
|
|
"OnlineMeetings.Read", "OnlineMeetingTranscript.Read.All",
|
|
]
|
|
|
|
|
|
class Ms365AuthError(Exception):
|
|
"""Lỗi khi đăng nhập hoặc lấy token Microsoft 365."""
|
|
pass
|
|
|
|
|
|
def _load_cache():
|
|
"""Nạp kho token đã lưu từ đĩa (nếu có)."""
|
|
import msal
|
|
|
|
cache = msal.SerializableTokenCache()
|
|
serialized = None
|
|
try:
|
|
import keyring
|
|
serialized = keyring.get_password(_KEYRING_SERVICE, _KEYRING_KEY)
|
|
except Exception: # noqa: BLE001 - no OS credential store available
|
|
serialized = None
|
|
if serialized is None and TOKEN_CACHE_PATH.exists():
|
|
try:
|
|
serialized = TOKEN_CACHE_PATH.read_text(encoding="utf-8")
|
|
except OSError:
|
|
serialized = None
|
|
if serialized:
|
|
try:
|
|
cache.deserialize(serialized)
|
|
except ValueError:
|
|
pass
|
|
return cache
|
|
|
|
|
|
def _save_cache(cache) -> None:
|
|
"""Ghi kho token xuống đĩa, chỉ khi nó thật sự thay đổi."""
|
|
if not cache.has_state_changed:
|
|
return
|
|
serialized = cache.serialize()
|
|
try:
|
|
import keyring
|
|
keyring.set_password(_KEYRING_SERVICE, _KEYRING_KEY, serialized)
|
|
# Migrated to the OS credential store — drop any older plaintext file
|
|
# so the token isn't left duplicated on disk.
|
|
if TOKEN_CACHE_PATH.exists():
|
|
try:
|
|
TOKEN_CACHE_PATH.unlink()
|
|
except OSError:
|
|
pass
|
|
return
|
|
except Exception: # noqa: BLE001 - no OS credential store available
|
|
pass
|
|
TOKEN_CACHE_PATH.parent.mkdir(parents=True, exist_ok=True)
|
|
TOKEN_CACHE_PATH.write_text(serialized, encoding="utf-8")
|
|
|
|
|
|
def _app(tenant_id: str, client_id: str):
|
|
"""Dựng ứng dụng MSAL cho tenant/client đã cấu hình; thiếu ``msal`` thì báo lỗi
|
|
kèm hướng dẫn cài.
|
|
"""
|
|
try:
|
|
import msal
|
|
except ImportError as exc:
|
|
raise Ms365AuthError(
|
|
"The 'msal' package isn't installed. Run: pip install msal") from exc
|
|
# Zero-config: blank tenant/client → the bundled public client + "common"
|
|
# authority, so sign-in works with no Azure app registration to enter.
|
|
tenant_id = (tenant_id or "").strip() or DEFAULT_TENANT
|
|
client_id = (client_id or "").strip() or DEFAULT_CLIENT_ID
|
|
cache = _load_cache()
|
|
authority = f"https://login.microsoftonline.com/{tenant_id}"
|
|
try:
|
|
# validate_authority=False: skip MSAL's authority-discovery network call at
|
|
# construction time — the host is always our own hardcoded, trusted
|
|
# login.microsoftonline.com, so there is nothing to validate. Without this,
|
|
# simply building the app object (e.g. to check "is anyone signed in?")
|
|
# would silently reach out to Microsoft even when the user turned "Allow
|
|
# external internet access" off, and a mistyped tenant id would raise here
|
|
# instead of at an explicit sign-in action.
|
|
app = msal.PublicClientApplication(
|
|
client_id, authority=authority, token_cache=cache, validate_authority=False)
|
|
except Exception as exc: # noqa: BLE001 - malformed tenant/client id, etc.
|
|
raise Ms365AuthError(f"Invalid Tenant ID / Client ID: {exc}") from exc
|
|
return app, cache
|
|
|
|
|
|
def _cached_account_offline() -> Optional[dict]:
|
|
"""First account in the saved token cache, read without any MSAL network setup."""
|
|
try:
|
|
import msal
|
|
|
|
accounts = _load_cache().find(msal.TokenCache.CredentialType.ACCOUNT)
|
|
except Exception: # noqa: BLE001 - no msal / unreadable cache = not signed in
|
|
return None
|
|
return accounts[0] if accounts else None
|
|
|
|
|
|
def _ensure_network(action: str) -> None:
|
|
"""Turn a "Block network" refusal into the error type callers already handle."""
|
|
from ..application.network import network_guard
|
|
|
|
if network_guard.is_blocked():
|
|
raise Ms365AuthError(network_guard.refusal(action))
|
|
|
|
|
|
def signed_in_account(tenant_id: str, client_id: str) -> Optional[dict]:
|
|
"""The cached account, if any — a local cache lookup, no network call."""
|
|
from ..application.network import network_guard
|
|
|
|
if network_guard.is_blocked():
|
|
# Building the MSAL app fetches the tenant's OpenID configuration, so
|
|
# read the token cache directly instead: the UI still sees who is
|
|
# signed in without the app reaching login.microsoftonline.com.
|
|
return _cached_account_offline()
|
|
try:
|
|
app, _cache = _app(tenant_id, client_id)
|
|
except Ms365AuthError:
|
|
return None
|
|
accounts = app.get_accounts()
|
|
return accounts[0] if accounts else None
|
|
|
|
|
|
def sign_in_device_code(tenant_id: str, client_id: str, on_code: Callable[[dict], None]) -> dict:
|
|
"""Blocking device-code sign-in — call this off the UI thread.
|
|
|
|
``on_code`` is invoked once with the MSAL device-flow dict so the caller can
|
|
both auto-open the browser and show a copyable code. Useful keys:
|
|
``user_code`` (the code to enter), ``verification_uri`` (the page to open),
|
|
``verification_uri_complete`` (URL with the code pre-filled, when the tenant
|
|
returns it) and ``message`` (the full human-readable instruction). Returns
|
|
the MSAL token result dict; raises Ms365AuthError on failure/timeout."""
|
|
_ensure_network("Microsoft 365 sign-in")
|
|
app, cache = _app(tenant_id, client_id)
|
|
flow = app.initiate_device_flow(scopes=SCOPES)
|
|
if "user_code" not in flow:
|
|
raise Ms365AuthError(flow.get("error_description") or "Could not start device sign-in.")
|
|
on_code(flow)
|
|
result = app.acquire_token_by_device_flow(flow) # blocks, polling until done/expired
|
|
_save_cache(cache)
|
|
if not result or "access_token" not in result:
|
|
desc = (result or {}).get("error_description") or "Sign-in failed."
|
|
raise Ms365AuthError(desc)
|
|
return result
|
|
|
|
|
|
def identity_from_result(result: dict) -> str:
|
|
"""The signed-in user's UPN/email from a ``sign_in_device_code()`` result —
|
|
used by ``login_dialog.py`` to map an SSO sign-in to a provisioned
|
|
``accounts.Account`` by username. MSAL requests ``openid``/``profile``
|
|
implicitly on every token request, so ``id_token_claims`` is present
|
|
alongside the resource scopes in ``SCOPES``."""
|
|
claims = (result or {}).get("id_token_claims") or {}
|
|
return claims.get("preferred_username") or claims.get("email") or ""
|
|
|
|
|
|
def get_access_token(tenant_id: str, client_id: str) -> str:
|
|
"""Silently reuse the cached sign-in. Raises Ms365AuthError when there is
|
|
no valid session — the caller (a Graph call) should surface that as a
|
|
normal tool failure telling the user to sign in again from Settings."""
|
|
_ensure_network("Microsoft 365")
|
|
app, cache = _app(tenant_id, client_id)
|
|
accounts = app.get_accounts()
|
|
if not accounts:
|
|
raise Ms365AuthError("Not signed in to Microsoft 365 — sign in from Settings first.")
|
|
result = app.acquire_token_silent(SCOPES, account=accounts[0])
|
|
_save_cache(cache)
|
|
if not result or "access_token" not in result:
|
|
raise Ms365AuthError("Microsoft 365 sign-in expired — sign in again from Settings.")
|
|
return result["access_token"]
|
|
|
|
|
|
# ---- zero-config convenience wrappers (use the bundled default identity) ----
|
|
# The UI calls these with no args for the "connect like Claude" flow; they read
|
|
# the optional config overrides so a custom Azure app still works.
|
|
def _ids(config=None):
|
|
"""Cặp (tenant_id, client_id) đọc từ cấu hình MS365."""
|
|
ms365 = (config.ms365 if config is not None else {}) or {}
|
|
return ms365.get("tenant_id", ""), ms365.get("client_id", "")
|
|
|
|
|
|
def current_identity(config=None) -> str:
|
|
"""Signed-in account's UPN/email, or '' if not signed in (no network)."""
|
|
acc = signed_in_account(*_ids(config))
|
|
return (acc or {}).get("username", "") if acc else ""
|
|
|
|
|
|
def is_signed_in(config=None) -> bool:
|
|
"""Đã có tài khoản MS365 đăng nhập sẵn hay chưa."""
|
|
return signed_in_account(*_ids(config)) is not None
|
|
|
|
|
|
def sign_in(on_code: Callable[[dict], None], config=None) -> dict:
|
|
"""Zero-config device-code sign-in — blocking, call off the UI thread.
|
|
``on_code`` receives the MSAL device-flow dict (user_code/verification_uri/…)."""
|
|
return sign_in_device_code(*_ids(config), on_code)
|
|
|
|
|
|
def sign_out_default(config=None) -> None:
|
|
"""Đăng xuất tài khoản MS365 theo cấu hình hiện tại."""
|
|
sign_out(*_ids(config))
|
|
|
|
|
|
def sign_out(tenant_id: str, client_id: str) -> None:
|
|
"""Đăng xuất và xoá token của một tenant/client khỏi kho."""
|
|
try:
|
|
_ensure_network("Microsoft 365 sign-out") # chặn mạng: chỉ xoá kho token bên dưới
|
|
app, cache = _app(tenant_id, client_id)
|
|
for acc in app.get_accounts():
|
|
app.remove_account(acc)
|
|
_save_cache(cache)
|
|
except Ms365AuthError:
|
|
pass
|
|
try:
|
|
import keyring
|
|
keyring.delete_password(_KEYRING_SERVICE, _KEYRING_KEY)
|
|
except Exception: # noqa: BLE001 - nothing stored there, or no credential store
|
|
pass
|
|
try:
|
|
if TOKEN_CACHE_PATH.exists():
|
|
TOKEN_CACHE_PATH.unlink()
|
|
except OSError:
|
|
pass
|