fix(P2-4): use persisted backend security mode as authority
This commit is contained in:
@@ -60,10 +60,10 @@ export function Modal({ open, onClose, children, width = 340 }: { open: boolean;
|
||||
}
|
||||
|
||||
/* ——— 开关 ——— */
|
||||
export function Toggle({ on, onChange, label }: { on: boolean; onChange: (b: boolean) => void; label?: string }) {
|
||||
export function Toggle({ on, onChange, label, disabled = false }: { on: boolean; onChange: (b: boolean) => void; label?: string; disabled?: boolean }) {
|
||||
return (
|
||||
<button onClick={() => onChange(!on)} aria-pressed={on} aria-label={label}
|
||||
className={`relative w-[46px] h-[26px] rounded-full transition-colors duration-200 ${on ? "bg-primary" : "bg-s4"}`}>
|
||||
<button disabled={disabled} onClick={() => onChange(!on)} aria-pressed={on} aria-label={label}
|
||||
className={`relative w-[46px] h-[26px] rounded-full transition-colors duration-200 disabled:opacity-50 disabled:cursor-wait ${on ? "bg-primary" : "bg-s4"}`}>
|
||||
<span className={`absolute top-[3px] w-5 h-5 rounded-full elev-1 transition-all duration-200 ${on ? "left-[23px] bg-on-primary" : "left-[3px] bg-mut"}`} />
|
||||
</button>
|
||||
);
|
||||
|
||||
+3
-14
@@ -3,17 +3,8 @@ import DOMPurify from "dompurify";
|
||||
|
||||
/** 站点根地址:正文里的相对路径要补全 */
|
||||
const SITE = "https://huangguoai.com";
|
||||
const STRICT_LS = "hghub_strict_security";
|
||||
|
||||
function readStrictSecurity(): boolean {
|
||||
try {
|
||||
return localStorage.getItem(STRICT_LS) !== "0";
|
||||
} catch {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
let strictSecurity = readStrictSecurity();
|
||||
// 启动前默认严格;有效状态只由 Rust app_boot / security_set 返回值更新。
|
||||
let strictSecurity = true;
|
||||
|
||||
/** 当前是否启用严格安全模式(默认开启)。 */
|
||||
export function isStrictSecurity(): boolean {
|
||||
@@ -23,9 +14,7 @@ export function isStrictSecurity(): boolean {
|
||||
/** 切换严格安全模式(关闭后回退为兼容清洗与链接策略)。 */
|
||||
export function setStrictSecurity(strict: boolean): void {
|
||||
strictSecurity = strict;
|
||||
try {
|
||||
localStorage.setItem(STRICT_LS, strict ? "1" : "0");
|
||||
} catch { /* ignore */ }
|
||||
|
||||
}
|
||||
|
||||
function isInternalWebviewHost(host: string): boolean {
|
||||
|
||||
@@ -8,7 +8,7 @@ import { IBack, IBoltF, IBroom, IChevR, IHeart, IInfo, ILink, ILockF, IMoon, IRe
|
||||
/* 独立设置页:线路 / 外观 / 本地数据 / 清理缓存 / 网络诊断 / 关于
|
||||
入口:Windows 侧栏「设置」、Android「我的」右上角齿轮 */
|
||||
export default function Settings({ onBack, wide }: { onBack: () => void; wide: boolean }) {
|
||||
const { theme, setTheme, favs, history, strictSecurity, cliRelaxed, setStrictSecurity, toast } = useStore();
|
||||
const { theme, setTheme, favs, history, strictSecurity, strictSecurityReady, securityPending, cliRelaxed, setStrictSecurity, toast } = useStore();
|
||||
const [about, setAbout] = useState(false);
|
||||
const [clearOpen, setClearOpen] = useState(false);
|
||||
const [clearing, setClearing] = useState(false);
|
||||
@@ -112,9 +112,11 @@ export default function Settings({ onBack, wide }: { onBack: () => void; wide: b
|
||||
</div>
|
||||
<Toggle
|
||||
on={strictSecurity}
|
||||
disabled={!strictSecurityReady || securityPending}
|
||||
onChange={(on) => {
|
||||
void setStrictSecurity(on);
|
||||
toast(on ? "已开启严格安全模式" : "已切换为兼容模式(关闭激进清洗与守卫)", "info");
|
||||
void setStrictSecurity(on).then(() => {
|
||||
toast(on ? "已开启严格安全模式" : "已切换为兼容模式(关闭激进清洗与守卫)", "info");
|
||||
}).catch(() => toast("安全设置保存失败,已保持原模式", "info"));
|
||||
}}
|
||||
label="严格安全模式"
|
||||
/>
|
||||
|
||||
+13
-8
@@ -120,7 +120,7 @@ type Store = {
|
||||
muted: boolean; setMuted: (v: boolean | ((p: boolean) => boolean)) => void;
|
||||
volume: number; setVolume: (v: number) => void;
|
||||
searchHist: string[]; addSearch: (q: string) => void; clearSearchHist: () => void;
|
||||
strictSecurity: boolean; cliRelaxed: boolean; setStrictSecurity: (on: boolean) => Promise<void>;
|
||||
strictSecurity: boolean; strictSecurityReady: boolean; securityPending: boolean; cliRelaxed: boolean; setStrictSecurity: (on: boolean) => Promise<void>;
|
||||
toasts: Toast[]; toast: (msg: string, icon?: Toast["icon"]) => void;
|
||||
};
|
||||
|
||||
@@ -140,6 +140,8 @@ export function AppProvider({ children }: { children: React.ReactNode }) {
|
||||
const [fs, setFsState] = useState(false);
|
||||
const [strictSecurity, setStrictSecurityState] = useState<boolean>(isStrictSecurity);
|
||||
const [cliRelaxed, setCliRelaxed] = useState(false);
|
||||
const [strictSecurityReady, setStrictSecurityReady] = useState(false);
|
||||
const [securityPending, setSecurityPending] = useState(false);
|
||||
const toastSeq = useRef(0);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -158,6 +160,7 @@ export function AppProvider({ children }: { children: React.ReactNode }) {
|
||||
if (typeof r.strict_security === "boolean") {
|
||||
setStrictSecurityState(r.strict_security);
|
||||
setHtmlStrictSecurity(r.strict_security);
|
||||
setStrictSecurityReady(true);
|
||||
}
|
||||
setCliRelaxed(!!r.cli_relaxed);
|
||||
})
|
||||
@@ -165,14 +168,16 @@ export function AppProvider({ children }: { children: React.ReactNode }) {
|
||||
}, []);
|
||||
|
||||
const setStrictSecurity = useCallback(async (on: boolean) => {
|
||||
setStrictSecurityState(on);
|
||||
setHtmlStrictSecurity(on);
|
||||
if (!strictSecurityReady) throw new Error("安全设置尚未读取,请重新启动应用");
|
||||
setSecurityPending(true);
|
||||
try {
|
||||
const saved = await api.securitySet(on);
|
||||
setStrictSecurityState(saved);
|
||||
setHtmlStrictSecurity(saved);
|
||||
} catch { /* ignore */ }
|
||||
}, []);
|
||||
setStrictSecurityState(saved);
|
||||
} finally {
|
||||
setSecurityPending(false);
|
||||
}
|
||||
}, [strictSecurityReady]);
|
||||
|
||||
const toast = useCallback((msg: string, icon?: Toast["icon"]) => {
|
||||
const id = ++toastSeq.current;
|
||||
@@ -266,11 +271,11 @@ export function AppProvider({ children }: { children: React.ReactNode }) {
|
||||
toggleLikeLocal, toggleFavLocal, toggleFollowLocal, addHistory, clearHistory,
|
||||
muted: p.muted, setMuted, volume: p.volume, setVolume, theater, setTheater, fs, setFs,
|
||||
searchHist: p.searchHist, addSearch, clearSearchHist,
|
||||
strictSecurity, cliRelaxed, setStrictSecurity,
|
||||
strictSecurity, strictSecurityReady, securityPending, cliRelaxed, setStrictSecurity,
|
||||
toasts, toast,
|
||||
}), [theme, tab, setTab, stack, push, pop, user, loginOpen, logout,
|
||||
p, toggleLikeLocal, toggleFavLocal, toggleFollowLocal, addHistory, clearHistory,
|
||||
setMuted, setVolume, addSearch, clearSearchHist, strictSecurity, cliRelaxed, setStrictSecurity,
|
||||
setMuted, setVolume, addSearch, clearSearchHist, strictSecurity, strictSecurityReady, securityPending, cliRelaxed, setStrictSecurity,
|
||||
toasts, toast, theater, setTheater, fs, setFs]);
|
||||
|
||||
return <Ctx.Provider value={value}>{children}</Ctx.Provider>;
|
||||
|
||||
Reference in New Issue
Block a user