-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.js
More file actions
207 lines (193 loc) · 7.4 KB
/
Copy pathadmin.js
File metadata and controls
207 lines (193 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
'use strict';
const url = require('url');
const settings = require('ep_etherpad-lite/node/utils/Settings');
const log4js = require('ep_etherpad-lite/node_modules/log4js');
const logger = log4js.getLogger('ep_ai_core:admin');
/**
* Middleware to require admin authentication for AI settings routes.
* Reuses Etherpad's existing auth — checks for is_admin on the session.
* Fix: properly awaits JWT verification (no race condition).
*/
const requireAdmin = async (req, res, next) => {
// Check session-based auth (Etherpad's webaccess sets this)
const session = req.session;
const user = session?.user;
if (user && user.is_admin) return next();
// Check JWT/OAuth admin claim
const authHeader = req.headers.authorization || '';
if (authHeader) {
try {
const jose = require('ep_etherpad-lite/node_modules/jose');
const {publicKeyExported} = require('ep_etherpad-lite/node/security/OAuth2Provider');
if (publicKeyExported) {
const token = authHeader.replace(/^Bearer\s+/i, '');
const result = await jose.jwtVerify(token, publicKeyExported, {algorithms: ['RS256']});
if (result.payload.admin || result.payload.is_admin) return next();
return res.status(403).json({error: 'Admin access required'});
}
} catch {
return res.status(401).json({error: 'Invalid token'});
}
}
// No valid auth — redirect to admin login
if (req.method === 'GET') {
return res.redirect('/admin/');
}
res.status(401).json({error: 'Admin authentication required'});
};
/**
* Sanitize settings for client display — strip sensitive fields.
*/
const sanitizeSettingsForClient = (aiSettings) => {
const safe = {...aiSettings};
delete safe.apiKey;
return safe;
};
/**
* Escape a JSON string for safe embedding in HTML <script> tags.
*/
const safeJsonForHtml = (obj) => {
return JSON.stringify(obj)
.replace(/</g, '\\u003c')
.replace(/>/g, '\\u003e')
.replace(/&/g, '\\u0026');
};
/**
* Check if a URL points to a private/internal network address.
* Blocks RFC 1918, loopback, link-local, and cloud metadata IPs.
*/
const isPrivateUrl = (urlString) => {
try {
const parsed = new URL(urlString);
const hostname = parsed.hostname;
// Block obvious private/internal hostnames
if (hostname === 'localhost' || hostname === '127.0.0.1' || hostname === '::1') return true;
if (hostname === '0.0.0.0') return true;
// Cloud metadata endpoints
if (hostname === '169.254.169.254') return true;
if (hostname.endsWith('.internal') || hostname.endsWith('.local')) return true;
// RFC 1918 ranges
const parts = hostname.split('.').map(Number);
if (parts.length === 4 && parts.every((p) => !isNaN(p))) {
if (parts[0] === 10) return true; // 10.0.0.0/8
if (parts[0] === 172 && parts[1] >= 16 && parts[1] <= 31) return true; // 172.16.0.0/12
if (parts[0] === 192 && parts[1] === 168) return true; // 192.168.0.0/16
}
return false;
} catch {
return true; // If we can't parse it, block it
}
};
/**
* Validate and sanitize settings input.
*/
const validateSettings = (input) => {
const allowed = {
provider: ['anthropic', 'openai'],
defaultMode: ['full', 'readOnly', 'none'],
};
const cleaned = {};
if (input.apiBaseUrl && typeof input.apiBaseUrl === 'string') {
const urlStr = input.apiBaseUrl.trim();
if (/^https?:\/\//i.test(urlStr) && !isPrivateUrl(urlStr)) {
cleaned.apiBaseUrl = urlStr;
}
}
if (input.model && typeof input.model === 'string') {
cleaned.model = input.model.trim().substring(0, 100);
}
if (input.maxTokens) {
const n = parseInt(input.maxTokens);
if (n > 0 && n <= 100000) cleaned.maxTokens = n;
}
if (input.provider && allowed.provider.includes(input.provider)) {
cleaned.provider = input.provider;
}
if (input.defaultMode && allowed.defaultMode.includes(input.defaultMode)) {
cleaned.defaultMode = input.defaultMode;
}
if (input.trigger && typeof input.trigger === 'string') {
cleaned.trigger = input.trigger.trim().substring(0, 20);
}
if (input.authorName && typeof input.authorName === 'string') {
cleaned.authorName = input.authorName.trim().substring(0, 50);
}
if (input.authorColor && /^#[0-9a-f]{6}$/i.test(input.authorColor)) {
cleaned.authorColor = input.authorColor;
}
return cleaned;
};
exports.expressCreateServer = (hookName, {app}) => {
logger.info('Registering /admin-plugins/ai routes');
app.get('/admin-plugins/ai', requireAdmin, (req, res) => {
const aiSettings = settings.ep_ai_core || {};
const fs = require('fs');
const path = require('path');
let html;
try {
html = fs.readFileSync(
require.resolve('ep_ai_core/templates/admin.html'), 'utf8');
} catch {
html = fs.readFileSync(
path.join(__dirname, 'templates', 'admin.html'), 'utf8');
}
html = html.replace('__SETTINGS_JSON__',
safeJsonForHtml(sanitizeSettingsForClient(aiSettings)));
res.type('html').send(html);
});
app.post('/admin-plugins/ai/save', requireAdmin, (req, res) => {
try {
const validated = validateSettings(req.body || {});
settings.ep_ai_core = {
...settings.ep_ai_core,
...(validated.apiBaseUrl && {apiBaseUrl: validated.apiBaseUrl}),
...(validated.model && {model: validated.model}),
...(validated.maxTokens && {maxTokens: validated.maxTokens}),
...(validated.provider && {provider: validated.provider}),
access: {
defaultMode: validated.defaultMode || settings.ep_ai_core?.access?.defaultMode || 'full',
pads: settings.ep_ai_core?.access?.pads || {},
},
chat: {
...settings.ep_ai_core?.chat,
...(validated.trigger && {trigger: validated.trigger}),
...(validated.authorName && {authorName: validated.authorName}),
...(validated.authorColor && {authorColor: validated.authorColor}),
},
};
logger.info('AI settings updated via admin UI');
res.json({success: true});
} catch (err) {
logger.error(`Failed to save AI settings: ${err.message}`);
res.status(500).json({success: false, error: 'Internal error'});
}
});
app.post('/admin-plugins/ai/test', requireAdmin, async (req, res) => {
try {
const aiSettings = settings.ep_ai_core || {};
if (!aiSettings.apiBaseUrl || !aiSettings.apiKey) {
return res.json({success: false, error: 'API base URL and key must be configured in settings.json'});
}
const llmClient = require('./llmClient');
const client = llmClient.create({
apiBaseUrl: aiSettings.apiBaseUrl,
apiKey: aiSettings.apiKey,
model: aiSettings.model,
provider: aiSettings.provider,
});
const result = await client.complete([
{role: 'user', content: 'Reply with exactly: "Connection successful"'},
], {maxTokens: 50});
res.json({success: true, response: result.content, usage: result.usage});
} catch (err) {
// Sanitize error — don't leak internal API details
const safeMessage = err.message.includes('401') ? 'Authentication failed (check API key)'
: err.message.includes('429') ? 'Rate limited by provider'
: err.message.includes('ECONNREFUSED') ? 'Connection refused (check API URL)'
: 'Connection failed';
res.json({success: false, error: safeMessage});
}
});
};
exports.isPrivateUrl = isPrivateUrl;
exports.validateSettings = validateSettings;