Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6a40040573 | |||
| cac56740cc | |||
| f75a9abecc | |||
| d3fe1b21ed | |||
| afc474b0c4 | |||
| a963bec3f3 | |||
| 81079c7364 | |||
| 4079397625 |
@@ -134,3 +134,13 @@ pnpm start:prod
|
||||
通用实验消费者可经 `GET /api/digital/decisions/:key` 读取本人类型决策的 `data.value`,沿用 HttpOnly 会话与不缓存策略。类型、测试方案和用户分组全部在后台管理;新类型需要应用真实使用返回值并完成后台接入验收后才可启动。
|
||||
|
||||
`src/components/conversation-row.tsx` 提供会话列表横滑删除、桌面删除入口与模态确认。灵感回复读取本人会话上下文,选择后只加入草稿,原草稿保留;回车发送覆盖 H5/Web,支持中文输入法、Shift+Enter 与手机换行按钮。
|
||||
|
||||
内部滚动区域统一隐藏原生轨道,保留触摸/键盘滚动,设定表单关闭横向溢出。风格控制提示使用只读 `GET /api/digital/conversations/:id/style-policy`,打开设定不会写入实验分组、决策或曝光。
|
||||
|
||||
实验决策支持 A~Z 动态组;未入组时组别为 null。界面消费 `decision.value`(`new` / `true` 为新版),组名仅用于归因。决策过期使用服务端的 `fallbackValue`,不将无实验用户伪装成 A 组;旧服务未返回value时保留既有兼容路径。
|
||||
|
||||
## 登录跳转回归
|
||||
|
||||
- `src/lib/auth-redirect.ts`:登出与会话过期后使用站内相对 `Location`,避免跳到 Next.js 内部监听地址 `0.0.0.0:3210`;仅允许站内返回路径。
|
||||
- `tests/auth-redirect.test.mjs`:覆盖正式域名/本地地址的登录页跳转、站内参数和外部地址拦截。
|
||||
- 执行:`node --experimental-strip-types --test tests/auth-redirect.test.mjs`(Node.js 22)。
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
/** 清理会话后返回当前站点,保持反向代理域名与本地访问地址。 */
|
||||
import { NextRequest } from "next/server";
|
||||
import { logoutRedirect } from "@/lib/auth-redirect";
|
||||
|
||||
import { clearSessionToken } from "@/lib/session";
|
||||
|
||||
/** 客户端主动退出时仅清理 Cookie,由页面处理导航。 */
|
||||
export async function POST() {
|
||||
await clearSessionToken();
|
||||
return Response.json({ success: true, data: null });
|
||||
}
|
||||
|
||||
/** 会话过期或链接退出后跳转站内页面,不使用服务端监听地址。 */
|
||||
export async function GET(request: NextRequest) {
|
||||
await clearSessionToken();
|
||||
const next = request.nextUrl.searchParams.get("next") || "/login";
|
||||
|
||||
return NextResponse.redirect(new URL(next, request.url));
|
||||
return logoutRedirect(request.nextUrl.searchParams.get("next"));
|
||||
}
|
||||
|
||||
@@ -33,12 +33,15 @@ async function proxy(
|
||||
const { path } = await context.params;
|
||||
const joined = path.join("/");
|
||||
if (
|
||||
!/^(bootstrap|profile|exposure|decisions\/[a-zA-Z0-9_-]+|conversations(?:\/[a-zA-Z0-9_-]+(?:\/(?:stream|stop|branch|suggestions|messages\/[a-zA-Z0-9_-]+))?)?)$/.test(
|
||||
!/^(bootstrap|profile|exposure|decisions\/[a-zA-Z0-9_-]+|conversations(?:\/[a-zA-Z0-9_-]+(?:\/(?:stream|stop|branch|suggestions|style-policy|messages\/[a-zA-Z0-9_-]+))?)?)$/.test(
|
||||
joined,
|
||||
)
|
||||
)
|
||||
return Response.json({ message: "接口不存在" }, { status: 404 });
|
||||
if (joined.startsWith("decisions/") && request.method !== "GET")
|
||||
if (
|
||||
(joined.startsWith("decisions/") || joined.endsWith("/style-policy")) &&
|
||||
request.method !== "GET"
|
||||
)
|
||||
return Response.json(
|
||||
{ message: "仅支持读取决策" },
|
||||
{ status: 405, headers: { Allow: "GET" } },
|
||||
|
||||
@@ -105,7 +105,7 @@ function MessageText({
|
||||
</div>
|
||||
);
|
||||
}
|
||||
/** 前台无分组按钮、调试开关或实验配置。 */
|
||||
/** 前台管理个人会话偏好;后台方案仅展示覆盖提示,不开放实验操作。 */
|
||||
export function DigitalHome({
|
||||
initial,
|
||||
initialSummaries,
|
||||
@@ -126,6 +126,9 @@ export function DigitalHome({
|
||||
[opening, setOpening] = useState(false),
|
||||
[error, setError] = useState(""),
|
||||
[notice, setNotice] = useState("");
|
||||
const [stylePolicy, setStylePolicy] = useState<
|
||||
"loading" | "personal" | "managed" | "unknown"
|
||||
>("loading");
|
||||
const [showSettings, setShowSettings] = useState(false),
|
||||
[settings, setSettings] = useState<Settings | null>(null),
|
||||
[profile, setProfile] = useState<Profile | null>(initial.profile);
|
||||
@@ -170,6 +173,25 @@ export function DigitalHome({
|
||||
books = boot?.catalog.filter((c) => c.kind === "worldbook") ?? [],
|
||||
presets = boot?.catalog.filter((c) => c.kind === "preset") ?? [];
|
||||
const conversationId = conversation?.id;
|
||||
// 状态提示走只读接口,打开设定不应写入实验分组、决策或曝光。
|
||||
useEffect(() => {
|
||||
if (!showSettings || !conversationId) return;
|
||||
const controller = new AbortController();
|
||||
digital<{ managed: boolean }>(
|
||||
`conversations/${conversationId}/style-policy`,
|
||||
"GET",
|
||||
undefined,
|
||||
controller.signal,
|
||||
)
|
||||
.then((policy) => {
|
||||
if (!controller.signal.aborted)
|
||||
setStylePolicy(policy.managed ? "managed" : "personal");
|
||||
})
|
||||
.catch(() => {
|
||||
if (!controller.signal.aborted) setStylePolicy("unknown");
|
||||
});
|
||||
return () => controller.abort();
|
||||
}, [showSettings, conversationId]);
|
||||
const decisionExpiresAt = boot?.decision.expiresAt,
|
||||
decisionServerNow = boot?.serverNow;
|
||||
const currentCard = cards.find((c) => c.id === conversation?.characterId);
|
||||
@@ -226,6 +248,7 @@ export function DigitalHome({
|
||||
decision: {
|
||||
...current.decision,
|
||||
variant: current.decision.fallbackVariant,
|
||||
value: current.decision.fallbackValue,
|
||||
decisionId: null,
|
||||
},
|
||||
}
|
||||
@@ -792,7 +815,11 @@ export function DigitalHome({
|
||||
await digital(`conversations/${conversation.id}`, "PATCH", { settings });
|
||||
setConversation((c) => (c ? { ...c, settings } : c));
|
||||
setShowSettings(false);
|
||||
setNotice("对话设定已保存,下次回复生效");
|
||||
setNotice(
|
||||
stylePolicy === "managed"
|
||||
? "设定已保存;当前叙事风格仍由统一方案控制"
|
||||
: "对话设定已保存,下次回复生效",
|
||||
);
|
||||
} catch (e) {
|
||||
reportError(e);
|
||||
}
|
||||
@@ -872,7 +899,15 @@ export function DigitalHome({
|
||||
<legend className={inSheet ? "dh-visually-hidden" : undefined}>
|
||||
叙事风格
|
||||
</legend>
|
||||
<p className="dh-style-help">为这段故事选一种氛围</p>
|
||||
<p className="dh-style-help" role="status">
|
||||
{stylePolicy === "loading"
|
||||
? "正在确认风格设置…"
|
||||
: stylePolicy === "managed"
|
||||
? "当前使用统一叙事风格。可以保存个人偏好,统一风格解除后采用。"
|
||||
: stylePolicy === "unknown"
|
||||
? "暂时无法确认当前风格,可以先保存个人偏好。"
|
||||
: "为这段故事选一种氛围,也可以跟随角色默认风格。"}
|
||||
</p>
|
||||
<div className="dh-style-options">
|
||||
{[
|
||||
{
|
||||
@@ -886,8 +921,7 @@ export function DigitalHome({
|
||||
{
|
||||
id: value,
|
||||
name: "当前会话风格",
|
||||
description:
|
||||
"此风格暂不在可选列表中,可以保留或选择其他风格。",
|
||||
description: "这个风格暂不可用,回复时会采用可用的默认风格。",
|
||||
},
|
||||
]
|
||||
: []),
|
||||
@@ -960,7 +994,11 @@ export function DigitalHome({
|
||||
},
|
||||
[],
|
||||
);
|
||||
const modern = boot?.decision.variant === "B";
|
||||
// UI按消费者返回值决定,组名仅用于归因;C到Z同样可以返回新版UI。
|
||||
// 缺少value时兼容尚未升级的服务响应。
|
||||
const modern = boot?.decision.value !== undefined
|
||||
? boot.decision.value === "new" || boot.decision.value === true
|
||||
: boot?.decision.variant === "B";
|
||||
return (
|
||||
<div
|
||||
className={`dh-app ${modern ? "dh-modern" : "dh-classic"} ${conversation ? "dh-in-chat" : ""}`}
|
||||
@@ -1051,6 +1089,7 @@ export function DigitalHome({
|
||||
aria-label="对话设定"
|
||||
onClick={() => {
|
||||
setSettings(conversation.settings);
|
||||
setStylePolicy("loading");
|
||||
setShowSettings(true);
|
||||
}}
|
||||
>
|
||||
@@ -1876,7 +1915,7 @@ export function DigitalHome({
|
||||
)}
|
||||
</div>
|
||||
<p className="dh-form-note">
|
||||
保存后从下一次回复生效。可以固定单条消息,将它加入对话记忆。
|
||||
保存后用于接下来的回复。固定单条消息,可将它加入对话记忆。
|
||||
</p>
|
||||
</div>
|
||||
<button className="dh-primary" type="submit">
|
||||
|
||||
@@ -969,6 +969,11 @@
|
||||
color: #d4cbd5;
|
||||
font-size: 12px;
|
||||
}
|
||||
.dh-settings > label small {
|
||||
color: var(--dh-muted);
|
||||
font-size: 11px;
|
||||
line-height: 1.7;
|
||||
}
|
||||
.dh-profile-form textarea,
|
||||
.dh-settings textarea {
|
||||
resize: vertical;
|
||||
@@ -1601,8 +1606,7 @@
|
||||
overflow-y: auto;
|
||||
overflow-x: hidden;
|
||||
overscroll-behavior: contain;
|
||||
scrollbar-width: thin;
|
||||
scrollbar-color: #ffffff20 transparent;
|
||||
scrollbar-width: none;
|
||||
}
|
||||
.dh-messages-inner {
|
||||
max-width: 840px;
|
||||
@@ -2289,3 +2293,38 @@
|
||||
body:has(.dh-app) .environment-badge {
|
||||
display: none;
|
||||
}
|
||||
|
||||
/* 内部区域保留手势和键盘滚动,不显示系统轨道,也不允许设定表单横向漂移。 */
|
||||
.dh-app,
|
||||
.dh-app * {
|
||||
scrollbar-width: none !important;
|
||||
}
|
||||
.dh-app::-webkit-scrollbar,
|
||||
.dh-app *::-webkit-scrollbar {
|
||||
display: none !important;
|
||||
width: 0 !important;
|
||||
height: 0 !important;
|
||||
}
|
||||
.dh-settings-body,
|
||||
.dh-selection-body,
|
||||
.dh-detail-body,
|
||||
.dh-ideas-list {
|
||||
min-width: 0;
|
||||
max-width: 100%;
|
||||
overflow-x: hidden;
|
||||
}
|
||||
.dh-settings-body {
|
||||
width: 100%;
|
||||
}
|
||||
.dh-settings-body > label,
|
||||
.dh-settings fieldset,
|
||||
.dh-selection-sheet fieldset {
|
||||
min-width: 0;
|
||||
max-width: 100%;
|
||||
}
|
||||
.dh-app textarea {
|
||||
width: 100%;
|
||||
max-width: 100%;
|
||||
overflow-x: hidden;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
/** 登出后的站内跳转,避免反向代理后的内部监听地址泄漏到浏览器。 */
|
||||
|
||||
/** 使用相对 Location 留在当前站点,并拒绝外部地址和 URL 控制字符。 */
|
||||
export function logoutRedirect(next: string | null) {
|
||||
let destination = "/login";
|
||||
if (
|
||||
next?.startsWith("/") &&
|
||||
!/^\/[/\\]/.test(next) &&
|
||||
!/[\\\u0000-\u0020\u007f]/.test(next)
|
||||
) {
|
||||
const local = new URL(next, "https://same-origin.invalid");
|
||||
// 消除点路径后仍须禁止双斜杠,避免 /.//host 被规范化为外站地址。
|
||||
if (local.origin === "https://same-origin.invalid" && !local.pathname.startsWith("//")) {
|
||||
destination = local.pathname + local.search + local.hash;
|
||||
}
|
||||
}
|
||||
// Route Handler 可直接返回相对 Location;不使用 request.url(生产中可能是 0.0.0.0)。
|
||||
return new Response(null, { status: 307, headers: { Location: destination } });
|
||||
}
|
||||
+4
-2
@@ -123,10 +123,12 @@ export type Bootstrap = {
|
||||
catalog: Card[];
|
||||
profile: Profile;
|
||||
decision: {
|
||||
variant: "A" | "B";
|
||||
variant: string | null;
|
||||
value?: unknown;
|
||||
fallbackValue?: unknown;
|
||||
decisionId: string | null;
|
||||
expiresAt: number;
|
||||
fallbackVariant: "A" | "B";
|
||||
fallbackVariant: string | null;
|
||||
};
|
||||
serverNow: number;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
/** 回归登录过期后的跳转:仅返回站内路径,不把服务监听地址发送给浏览器。 */
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { logoutRedirect } from '../src/lib/auth-redirect.ts';
|
||||
|
||||
test('登录过期后跳回当前站点的登录页,不依赖服务内部地址', () => {
|
||||
const response = logoutRedirect(null);
|
||||
assert.equal(response.status, 307);
|
||||
assert.equal(response.headers.get('location'), '/login');
|
||||
assert.equal(new URL(response.headers.get('location'), 'https://mrzhan.top/api/auth/logout').href, 'https://mrzhan.top/login');
|
||||
assert.equal(new URL(response.headers.get('location'), 'http://localhost:3210/api/auth/logout').href, 'http://localhost:3210/login');
|
||||
});
|
||||
|
||||
test('保留站内路径和查询参数', () => {
|
||||
assert.equal(logoutRedirect('/login?reason=expired').headers.get('location'), '/login?reason=expired');
|
||||
});
|
||||
|
||||
test('拒绝外部链接、协议相对地址及反斜杠变体', () => {
|
||||
for (const next of ['https://example.com', '//example.com', '/\\example.com', 'javascript:alert(1)', '\n//example.com', '/\n/example.com', '/.//example.com', '/%2e//example.com']) {
|
||||
assert.equal(logoutRedirect(next).headers.get('location'), '/login', next);
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user