mioku-service-webui 4.4.1 → 4.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/auth.ts +16 -2
- package/index.ts +10 -0
- package/package.json +1 -1
- package/routes/db.ts +30 -4
- package/system.ts +51 -0
- package/utils.ts +27 -3
package/auth.ts
CHANGED
|
@@ -2,6 +2,16 @@ import * as crypto from "node:crypto";
|
|
|
2
2
|
import type { Context, Next } from "hono";
|
|
3
3
|
import type { AuthConfig } from "./types";
|
|
4
4
|
import { AUTH_PATH, WEEK_MS, readJsonFile, writeJsonFile } from "./utils";
|
|
5
|
+
import { notifyOwnersAuthTokenRefreshed } from "./system";
|
|
6
|
+
|
|
7
|
+
function safeEqual(a: string, b: string): boolean {
|
|
8
|
+
const bufA = Buffer.from(a);
|
|
9
|
+
const bufB = Buffer.from(b);
|
|
10
|
+
if (bufA.length !== bufB.length) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
return crypto.timingSafeEqual(bufA, bufB);
|
|
14
|
+
}
|
|
5
15
|
|
|
6
16
|
export function ensureAuthConfig(): AuthConfig {
|
|
7
17
|
const now = Date.now();
|
|
@@ -14,6 +24,10 @@ export function ensureAuthConfig(): AuthConfig {
|
|
|
14
24
|
expiresAt: now + WEEK_MS,
|
|
15
25
|
};
|
|
16
26
|
writeJsonFile(AUTH_PATH, generated);
|
|
27
|
+
void notifyOwnersAuthTokenRefreshed(
|
|
28
|
+
generated.token,
|
|
29
|
+
generated.expiresAt,
|
|
30
|
+
).catch(() => {});
|
|
17
31
|
return generated;
|
|
18
32
|
}
|
|
19
33
|
|
|
@@ -28,7 +42,7 @@ export function verifyAuthHeader(c: Context): boolean {
|
|
|
28
42
|
}
|
|
29
43
|
|
|
30
44
|
const authConfig = ensureAuthConfig();
|
|
31
|
-
return token
|
|
45
|
+
return safeEqual(token, authConfig.token) && Date.now() < authConfig.expiresAt;
|
|
32
46
|
}
|
|
33
47
|
|
|
34
48
|
export async function requireAuth(c: Context, next: Next): Promise<Response | void> {
|
|
@@ -40,7 +54,7 @@ export async function requireAuth(c: Context, next: Next): Promise<Response | vo
|
|
|
40
54
|
|
|
41
55
|
export function loginWithToken(inputToken: string): { ok: boolean; expiresAt?: number } {
|
|
42
56
|
const authConfig = ensureAuthConfig();
|
|
43
|
-
if (inputToken
|
|
57
|
+
if (!safeEqual(inputToken, authConfig.token)) {
|
|
44
58
|
return { ok: false };
|
|
45
59
|
}
|
|
46
60
|
|
package/index.ts
CHANGED
|
@@ -61,6 +61,16 @@ class WebUIRuntime {
|
|
|
61
61
|
);
|
|
62
62
|
});
|
|
63
63
|
|
|
64
|
+
this.app.use("*", async (c, next) => {
|
|
65
|
+
await next();
|
|
66
|
+
c.header("X-Robots-Tag", "noindex, nofollow");
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
this.app.get("/robots.txt", (c) => {
|
|
70
|
+
c.header("content-type", "text/plain");
|
|
71
|
+
return c.body("User-agent: *\nDisallow: /\n");
|
|
72
|
+
});
|
|
73
|
+
|
|
64
74
|
this.app.notFound((c) => {
|
|
65
75
|
if (c.req.path.startsWith("/api/")) {
|
|
66
76
|
return c.json({ ok: false, error: "API_NOT_FOUND" }, 404);
|
package/package.json
CHANGED
package/routes/db.ts
CHANGED
|
@@ -187,6 +187,18 @@ export function createPluginConfigRoutes() {
|
|
|
187
187
|
export function createMemeRoutes() {
|
|
188
188
|
const app = new Hono();
|
|
189
189
|
|
|
190
|
+
const memeRoot = path.resolve(CHAT_DATA_DIR, "meme");
|
|
191
|
+
const isInsideMeme = (target: string): boolean => {
|
|
192
|
+
const resolved = path.resolve(target);
|
|
193
|
+
return resolved === memeRoot || resolved.startsWith(`${memeRoot}${path.sep}`);
|
|
194
|
+
};
|
|
195
|
+
const isSafeSegment = (value: string): boolean =>
|
|
196
|
+
Boolean(value) &&
|
|
197
|
+
!value.includes("/") &&
|
|
198
|
+
!value.includes("\\") &&
|
|
199
|
+
value !== "." &&
|
|
200
|
+
value !== "..";
|
|
201
|
+
|
|
190
202
|
app.get("/tree", (c) =>
|
|
191
203
|
c.json({ ok: true, data: listMemeTree() }),
|
|
192
204
|
);
|
|
@@ -200,11 +212,22 @@ export function createMemeRoutes() {
|
|
|
200
212
|
return c.json({ ok: false, error: "FILE_REQUIRED" }, 400);
|
|
201
213
|
}
|
|
202
214
|
|
|
203
|
-
const dir = path.join(CHAT_DATA_DIR, "meme", character, emotion);
|
|
204
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
205
|
-
|
|
206
215
|
const fileName = file.name || `upload-${Date.now()}.png`;
|
|
216
|
+
if (
|
|
217
|
+
!isSafeSegment(character) ||
|
|
218
|
+
!isSafeSegment(emotion) ||
|
|
219
|
+
!isSafeSegment(fileName)
|
|
220
|
+
) {
|
|
221
|
+
return c.json({ ok: false, error: "INVALID_NAME" }, 400);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const dir = path.join(memeRoot, character, emotion);
|
|
207
225
|
const filePath = path.join(dir, fileName);
|
|
226
|
+
if (!isInsideMeme(filePath)) {
|
|
227
|
+
return c.json({ ok: false, error: "INVALID_PATH" }, 400);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
208
231
|
const buffer = Buffer.from(await file.arrayBuffer());
|
|
209
232
|
fs.writeFileSync(filePath, buffer);
|
|
210
233
|
|
|
@@ -213,7 +236,10 @@ export function createMemeRoutes() {
|
|
|
213
236
|
|
|
214
237
|
app.delete("/", async (c) => {
|
|
215
238
|
const body = await c.req.json();
|
|
216
|
-
const filePath = path.
|
|
239
|
+
const filePath = path.resolve(process.cwd(), String(body.path || ""));
|
|
240
|
+
if (!isInsideMeme(filePath)) {
|
|
241
|
+
return c.json({ ok: false, error: "INVALID_PATH" }, 400);
|
|
242
|
+
}
|
|
217
243
|
if (!fs.existsSync(filePath)) {
|
|
218
244
|
return c.json({ ok: false, error: "NOT_FOUND" }, 404);
|
|
219
245
|
}
|
package/system.ts
CHANGED
|
@@ -1814,6 +1814,57 @@ async function notifyOwnerWebUIDownloaded(version: string): Promise<void> {
|
|
|
1814
1814
|
}
|
|
1815
1815
|
}
|
|
1816
1816
|
|
|
1817
|
+
export async function notifyOwnersAuthTokenRefreshed(
|
|
1818
|
+
token: string,
|
|
1819
|
+
expiresAt: number,
|
|
1820
|
+
): Promise<void> {
|
|
1821
|
+
const owners = getMiokuConfig()?.owners || [];
|
|
1822
|
+
if (owners.length === 0) {
|
|
1823
|
+
return;
|
|
1824
|
+
}
|
|
1825
|
+
|
|
1826
|
+
const MAX_ATTEMPTS = 24;
|
|
1827
|
+
const RETRY_INTERVAL_MS = 5000;
|
|
1828
|
+
|
|
1829
|
+
let bots: any[] = [];
|
|
1830
|
+
for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt += 1) {
|
|
1831
|
+
bots = Array.from(connectedBots.values()).filter(Boolean);
|
|
1832
|
+
if (bots.length > 0) {
|
|
1833
|
+
break;
|
|
1834
|
+
}
|
|
1835
|
+
await sleep(RETRY_INTERVAL_MS);
|
|
1836
|
+
}
|
|
1837
|
+
|
|
1838
|
+
if (bots.length === 0) {
|
|
1839
|
+
logger.warn("[webui] 密钥已刷新,但暂无在线 Bot,未能通知主人");
|
|
1840
|
+
return;
|
|
1841
|
+
}
|
|
1842
|
+
|
|
1843
|
+
const bot = bots[0];
|
|
1844
|
+
const settings = getWebUISettings();
|
|
1845
|
+
const url = `http://${settings.host}:${settings.port}`;
|
|
1846
|
+
const expireText = new Date(expiresAt).toLocaleString("zh-CN");
|
|
1847
|
+
const message = [
|
|
1848
|
+
"🔑 WebUI 鉴权密钥已更新",
|
|
1849
|
+
`地址: ${url}`,
|
|
1850
|
+
`新密钥: ${token}`,
|
|
1851
|
+
`有效期至: ${expireText}`,
|
|
1852
|
+
].join("\n");
|
|
1853
|
+
|
|
1854
|
+
for (const ownerId of owners) {
|
|
1855
|
+
try {
|
|
1856
|
+
await bot.api("send_private_msg", {
|
|
1857
|
+
user_id: Number(ownerId),
|
|
1858
|
+
message,
|
|
1859
|
+
});
|
|
1860
|
+
} catch (err: any) {
|
|
1861
|
+
logger.warn(
|
|
1862
|
+
`[webui] 通知主人 ${ownerId} 密钥更新失败: ${err?.message || err}`,
|
|
1863
|
+
);
|
|
1864
|
+
}
|
|
1865
|
+
}
|
|
1866
|
+
}
|
|
1867
|
+
|
|
1817
1868
|
export async function updateWebUIDistFromRelease(): Promise<
|
|
1818
1869
|
Record<string, any>
|
|
1819
1870
|
> {
|
package/utils.ts
CHANGED
|
@@ -36,7 +36,7 @@ export const BACKUP_DIR = path.join(process.cwd(), "backup");
|
|
|
36
36
|
|
|
37
37
|
export const defaultWebUISettings: WebUISettings = {
|
|
38
38
|
port: 3339,
|
|
39
|
-
host: "
|
|
39
|
+
host: "127.0.0.1",
|
|
40
40
|
packageManager: "bun" as PackageManager,
|
|
41
41
|
};
|
|
42
42
|
|
|
@@ -125,8 +125,32 @@ export function normalizePackageManager(): PackageManager {
|
|
|
125
125
|
}
|
|
126
126
|
|
|
127
127
|
export function isValidRepoUrl(url: string): boolean {
|
|
128
|
-
const
|
|
129
|
-
|
|
128
|
+
const trimmed = url.trim();
|
|
129
|
+
// 拒绝空白字符:堵 `ssh://host -o ProxyCommand=...` 之类参数注入
|
|
130
|
+
if (!trimmed || /\s/.test(trimmed)) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (/^https?:\/\//.test(trimmed)) {
|
|
135
|
+
try {
|
|
136
|
+
const parsed = new URL(trimmed);
|
|
137
|
+
return Boolean(parsed.hostname) && !parsed.hostname.startsWith("-");
|
|
138
|
+
} catch {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const sshMatch = trimmed.match(/^ssh:\/\/(?:[^@/]+@)?([^/:]+)/);
|
|
144
|
+
if (sshMatch) {
|
|
145
|
+
return !sshMatch[1].startsWith("-");
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const scpMatch = trimmed.match(/^git@([^:]+):.+/);
|
|
149
|
+
if (scpMatch) {
|
|
150
|
+
return !scpMatch[1].startsWith("-");
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return false;
|
|
130
154
|
}
|
|
131
155
|
|
|
132
156
|
export function isNpmPackageName(name: string): boolean {
|