fix: validate merged voice fallback and host toolbar features

This commit is contained in:
illusia committed 2026-11-27 08:53:26 +08:00
1 parent 02509ea551
commit 001457d004
32 files changed
+319 -102

No files matched your search

+32 -3
View File
@@ -7,6 +7,35 @@ import { DatabaseSync } from "node:sqlite";
import { encryptSecret, decryptSecret } from "../security/secret-crypto.js";
import { DATABASE_SCHEMA_VERSION, WebSpeakDatabase } from "./database.js";
test("voice overrides preserve schema 13 and survive an old binary updating ordinary settings", t => {
const directory = mkdtempSync(path.join(tmpdir(), "webspeak-voice-rollback-"));
const filename = path.join(directory, "webspeak.db");
t.after(() => rmSync(directory, { recursive: true, force: true }));
const database = new WebSpeakDatabase(filename);
const encrypted = encryptSecret("turn-test-secret-value", Buffer.alloc(32, 7), "webspeak:voice-turn-rest-secret:v1");
database.updateSettings({ ...database.getSettings(), voiceProfile: "adaptive", voiceTurnUrls: "turn:turn.example.com:3478",
voiceTurnSecretEncrypted: encrypted, voiceTurnTtlSeconds: 3600, voiceOpusWs: true, voiceJitterMaxMs: 200 });
database.close();
const legacy = new DatabaseSync(filename);
try {
const version = legacy.prepare("PRAGMA user_version").get() as { user_version: number };
assert.equal(version.user_version, 13, "0.3.3 rejects any schema version above 13");
assert.equal((legacy.prepare("PRAGMA table_info(settings)").all() as { name: string }[]).some(row => row.name.startsWith("voice_")), false);
legacy.prepare("UPDATE settings SET site_name = ? WHERE id = 1").run("Updated by old binary");
} finally { legacy.close(); }
const reopened = new WebSpeakDatabase(filename);
try {
const settings = reopened.getSettings();
assert.equal(settings.siteName, "Updated by old binary");
assert.equal(settings.voiceProfile, "adaptive");
assert.equal(settings.voiceTurnSecretEncrypted, encrypted);
assert.equal(settings.voiceOpusWs, true);
assert.equal(settings.voiceJitterMaxMs, 200);
reopened.setMeta("voice_settings_v1", "invalid-json");
assert.equal(reopened.getSettings().voiceProfile, null);
} finally { reopened.close(); }
});
test("relay voice endpoint is stored by the current database migration", (context) => {
const directory = mkdtempSync(path.join(tmpdir(), "webspeak-relay-db-"));
const database = new WebSpeakDatabase(path.join(directory, "webspeak.sqlite"));
@@ -15,7 +44,7 @@ test("relay voice endpoint is stored by the current database migration", (contex
rmSync(directory, { recursive: true, force: true });
});
assert.equal(DATABASE_SCHEMA_VERSION, 14);
assert.equal(DATABASE_SCHEMA_VERSION, 13);
assert.equal(database.schemaVersion, DATABASE_SCHEMA_VERSION);
assert.equal(database.getTsAdminSettings().transport, "web");
assert.equal(database.getTsAdminSettings().enabled, false);
@@ -38,13 +67,13 @@ test("schema 11 migration preserves encrypted SSH credentials and does not silen
const original = new WebSpeakDatabase(filename);
original.close();
const old = new DatabaseSync(filename);
old.exec("ALTER TABLE ts_admin_settings DROP COLUMN transport; ALTER TABLE ts_admin_settings DROP COLUMN query_host; ALTER TABLE ts_admin_settings DROP COLUMN web_query_url; ALTER TABLE ts_admin_settings DROP COLUMN api_key_encrypted; ALTER TABLE ts_admin_settings DROP COLUMN server_id; DROP TABLE workspace_invites; ALTER TABLE settings DROP COLUMN voice_profile; ALTER TABLE settings DROP COLUMN voice_turn_urls; ALTER TABLE settings DROP COLUMN voice_turn_secret_encrypted; ALTER TABLE settings DROP COLUMN voice_turn_ttl_seconds; ALTER TABLE settings DROP COLUMN voice_opus_ws; ALTER TABLE settings DROP COLUMN voice_jitter_max_ms; PRAGMA user_version = 11;");
old.exec("ALTER TABLE ts_admin_settings DROP COLUMN transport; ALTER TABLE ts_admin_settings DROP COLUMN query_host; ALTER TABLE ts_admin_settings DROP COLUMN web_query_url; ALTER TABLE ts_admin_settings DROP COLUMN api_key_encrypted; ALTER TABLE ts_admin_settings DROP COLUMN server_id; DROP TABLE workspace_invites; PRAGMA user_version = 11;");
old.prepare("UPDATE ts_admin_settings SET enabled=1, query_port=10222, username='query-owner', password_encrypted=?, host_key_fingerprint=?").run(encrypted, `SHA256:${"A".repeat(43)}`);
old.close();
const migrated = new WebSpeakDatabase(filename);
t.after(() => { migrated.close(); rmSync(directory, { recursive: true, force: true }); });
const settings = migrated.getTsAdminSettings();
assert.equal(migrated.schemaVersion, 14);
assert.equal(migrated.schemaVersion, 13);
assert.equal(settings.transport, "ssh"); assert.equal(settings.enabled, true);
assert.equal(settings.queryPort, 10222); assert.equal(settings.username, "query-owner");
assert.equal(settings.passwordEncrypted, encrypted);
+31 -38
View File
@@ -7,7 +7,8 @@ import type { TeamSpeakProtocol } from "../server/teamspeak-adapter.js";
import { DEFAULT_WEBRTC_UDP_PORT_RANGE } from "../server/webrtc-config.js";
import type { VoiceProfileSetting } from "../shared/voice-profile.js";
export const DATABASE_SCHEMA_VERSION = 14;
export const DATABASE_SCHEMA_VERSION = 13;
const VOICE_SETTINGS_META_KEY = "voice_settings_v1";
export type AccessMode = "fixed" | "open";
export interface PersistedSettings {
@@ -158,12 +159,6 @@ interface SettingsRow extends Record<string, unknown> {
webrtc_stun_server: string;
webrtc_udp_start: number;
webrtc_udp_end: number;
voice_profile: string | null;
voice_turn_urls: string | null;
voice_turn_secret_encrypted: string | null;
voice_turn_ttl_seconds: number | null;
voice_opus_ws: number | null;
voice_jitter_max_ms: number | null;
relay_configured: number;
relay_enabled: number;
relay_name: string;
@@ -267,6 +262,7 @@ export class WebSpeakDatabase {
getSettings(): PersistedSettings {
const row = this.database.prepare("SELECT * FROM settings WHERE id = 1").get() as SettingsRow;
const voice = this.readVoiceSettings();
return {
siteName: row.site_name,
welcomeText: row.welcome_text,
@@ -289,12 +285,7 @@ export class WebSpeakDatabase {
webRtcStunServer: row.webrtc_stun_server,
webRtcUdpStart: row.webrtc_udp_start,
webRtcUdpEnd: row.webrtc_udp_end,
voiceProfile: row.voice_profile === "legacy" || row.voice_profile === "adaptive" || row.voice_profile === "auto" ? row.voice_profile : null,
voiceTurnUrls: row.voice_turn_urls,
voiceTurnSecretEncrypted: row.voice_turn_secret_encrypted,
voiceTurnTtlSeconds: row.voice_turn_ttl_seconds,
voiceOpusWs: row.voice_opus_ws === null ? null : row.voice_opus_ws === 1,
voiceJitterMaxMs: row.voice_jitter_max_ms,
...voice,
relayConfigured: row.relay_configured === 1,
relayEnabled: row.relay_enabled === 1,
relayName: row.relay_name,
@@ -780,33 +771,35 @@ export class WebSpeakDatabase {
); CREATE INDEX workspace_invites_expiry ON workspace_invites(expires_at);
PRAGMA user_version = 13;`);
});
version = 13;
}
if (version === 13) {
this.transaction(() => {
this.database.exec(`
-- Console-managed voice deployment overrides. NULL keeps the
-- environment baseline; an empty TURN URL or secret explicitly
-- disables the corresponding environment value.
ALTER TABLE settings ADD COLUMN voice_profile TEXT CHECK (voice_profile IS NULL OR voice_profile IN ('auto', 'legacy', 'adaptive'));
ALTER TABLE settings ADD COLUMN voice_turn_urls TEXT;
ALTER TABLE settings ADD COLUMN voice_turn_secret_encrypted TEXT;
ALTER TABLE settings ADD COLUMN voice_turn_ttl_seconds INTEGER CHECK (voice_turn_ttl_seconds IS NULL OR voice_turn_ttl_seconds BETWEEN 300 AND 86400);
ALTER TABLE settings ADD COLUMN voice_opus_ws INTEGER CHECK (voice_opus_ws IS NULL OR voice_opus_ws IN (0, 1));
ALTER TABLE settings ADD COLUMN voice_jitter_max_ms INTEGER CHECK (voice_jitter_max_ms IS NULL OR voice_jitter_max_ms BETWEEN 80 AND 500);
PRAGMA user_version = 14;
`);
});
}
}
/** Optional settings must survive a code-only rollback to the 0.3.3 schema. */
private readVoiceSettings(): Pick<PersistedSettings, "voiceProfile" | "voiceTurnUrls" | "voiceTurnSecretEncrypted" | "voiceTurnTtlSeconds" | "voiceOpusWs" | "voiceJitterMaxMs"> {
let value: Record<string, unknown> = {};
try {
const parsed: unknown = JSON.parse(this.getMeta(VOICE_SETTINGS_META_KEY) ?? "{}");
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) value = parsed as Record<string, unknown>;
} catch { /* A malformed optional value inherits the deployment defaults. */ }
return {
voiceProfile: value.voiceProfile === "legacy" || value.voiceProfile === "adaptive" || value.voiceProfile === "auto"
? value.voiceProfile : null,
voiceTurnUrls: typeof value.voiceTurnUrls === "string" ? value.voiceTurnUrls : null,
voiceTurnSecretEncrypted: typeof value.voiceTurnSecretEncrypted === "string" ? value.voiceTurnSecretEncrypted : null,
voiceTurnTtlSeconds: Number.isInteger(value.voiceTurnTtlSeconds) && Number(value.voiceTurnTtlSeconds) >= 300 && Number(value.voiceTurnTtlSeconds) <= 86400
? Number(value.voiceTurnTtlSeconds) : null,
voiceOpusWs: typeof value.voiceOpusWs === "boolean" ? value.voiceOpusWs : null,
voiceJitterMaxMs: Number.isInteger(value.voiceJitterMaxMs) && Number(value.voiceJitterMaxMs) >= 80 && Number(value.voiceJitterMaxMs) <= 500
? Number(value.voiceJitterMaxMs) : null,
};
}
private writeSettings(settings: SettingsUpdate, now: string): void {
this.database.prepare(
`UPDATE settings SET
site_name = ?, welcome_text = ?, welcome_text_en = ?, welcome_text_de = ?, welcome_text_ru = ?, welcome_text_ja = ?, access_mode = ?, ts_host = ?, ts_port = ?, ts_target = ?,
ts_password_encrypted = ?, webrtc_enabled = ?, webrtc_public_host = ?, webrtc_ipv6_enabled = ?, webrtc_stun_server = ?, webrtc_udp_start = ?,
webrtc_udp_end = ?, voice_profile = ?, voice_turn_urls = ?, voice_turn_secret_encrypted = ?, voice_turn_ttl_seconds = ?, voice_opus_ws = ?, voice_jitter_max_ms = ?,
relay_configured = ?, relay_enabled = ?, relay_name = ?,
webrtc_udp_end = ?, relay_configured = ?, relay_enabled = ?, relay_name = ?,
relay_host = ?, relay_port = ?, relay_token_encrypted = ?, updated_at = ?
WHERE id = 1`,
).run(
@@ -827,12 +820,6 @@ export class WebSpeakDatabase {
settings.webRtcStunServer ?? "",
settings.webRtcUdpStart ?? DEFAULT_WEBRTC_UDP_PORT_RANGE[0],
settings.webRtcUdpEnd ?? DEFAULT_WEBRTC_UDP_PORT_RANGE[1],
settings.voiceProfile ?? null,
settings.voiceTurnUrls ?? null,
settings.voiceTurnSecretEncrypted ?? null,
settings.voiceTurnTtlSeconds ?? null,
settings.voiceOpusWs == null ? null : settings.voiceOpusWs ? 1 : 0,
settings.voiceJitterMaxMs ?? null,
settings.relayConfigured ? 1 : 0,
settings.relayEnabled ? 1 : 0,
settings.relayName,
@@ -841,6 +828,12 @@ export class WebSpeakDatabase {
settings.relayTokenEncrypted,
now,
);
// writeSettings is called within the caller's settings transaction. Older
// binaries ignore this key and keep it intact when updating other fields.
this.setMeta(VOICE_SETTINGS_META_KEY, JSON.stringify({
...this.readVoiceSettings(),
...Object.fromEntries(Object.entries(settings).filter(([key, value]) => key.startsWith("voice") && value !== undefined)),
}));
}
private insertAudit(event: string, details: Record<string, unknown>, now: string): void {