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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
'use strict';
'require baseclass';
'require fs';
'require form';
'require ui';
'require view';
const APK_DIR = '/etc/apk/keys/';
const OPKG_DIR = '/etc/opkg/keys/';
const isReadonlyView = !L.hasViewPermission() || null;
let KEYDIR = null;
let KEYEXT = null;
/* This safeList is not bullet-proof, but should prevent users
accidentally deleting official repo keys */
const safeList = [
'd310c6f2833e97f7', // 24.10 release usign key
'openwrt-snapshots.pem', // main snapshots EC pub key
];
function isFileInSafeList(file){
for (let name of safeList) {
if (file === name)
return true;
if (file.toLocaleLowerCase().replace(/^openwrt-[0-9]+\.[0-9]+/i, '') !== file)
return true;
if (file.toLocaleLowerCase().replace(/^openwrt-snapshots/i, '') !== file)
return true;
}
return false;
}
function normalizeKey(s) {
return s?.replace(/\s+/g, ' ')?.trim();
}
function determineKeyEnv() {
return fs.stat(APK_DIR).then(() => {
KEYDIR = APK_DIR;
KEYEXT = '.pem'; // not strictly necessary - apk allows any extension
}).catch(() => {
KEYDIR = OPKG_DIR;
KEYEXT = null; // opkg requires key filenames without an extension
});
}
function listKeyFiles() {
return fs.list(KEYDIR).then(entries =>
Promise.all(entries.map(entry =>
fs.read(KEYDIR + entry.name).then(content => ({
filename: entry.name,
key: content
}))
))
);
}
function saveKeyFile(keyContent, file, fileContent) {
const ts = Date.now();
// Note: opkg can only verify against a key with filename that matches its key hash
// generate a file name in case key content was pasted
const filename = file ? file?.name?.split('.')?.[0] + (KEYEXT || '') : null;
const noname = 'key_' + ts + (KEYEXT || '');
return fs.write(KEYDIR + (filename ?? noname), fileContent ?? keyContent, 384 /* 0600 */);
}
function removeKey(ev, key) {
L.showModal(_('Delete key'), [
E('div', _('Really delete the following software repository public key?')),
E('pre', [ key.filename ]),
E('div', { class: 'right' }, [
E('div', { class: 'btn', click: L.hideModal }, _('Cancel')),
' ',
E('div', {
class: 'btn danger',
click: function() {
fs.remove(KEYDIR + key.filename)
.then(() => window.location.reload())
.catch(e => ui.addNotification(null, E('p', e.message)))
.finally(() => ui.hideModal());
}
}, _('Delete key'))
])
]);
}
function isPemFormat(content) {
return (/-BEGIN ([A-Z ]+)?PUBLIC KEY-/.test(content));
}
function keyEnvironmentCheck(key) {
const isPem = isPemFormat(key);
// Reject PEM in OPKG; reject non-PEM in APK
if (KEYDIR === OPKG_DIR && isPem)
return _('This key appears to be in PEM format, which is not supported in an opkg environment.');
if (KEYDIR === APK_DIR && !isPem)
return _('This key does not appear to be in PEM format, which is required in an apk environment.');
return null;
}
function addKey(ev, file, fileContent) {
const input = document.getElementById('key-input');
const key = (fileContent ?? input?.value?.trim());
if (!key || !key.length)
return;
// Handle remote URL paste
if (/^https?:\/\/\S+$/i.test(key) && !fileContent) {
ui.addTimeLimitedNotification(_('Fetching key from URL…'), [], 5000, 'info');
L.Request.request(key, { method: 'GET' }).then(res => {
if (res.status !== 200) {
ui.addTimeLimitedNotification(_('Failed to fetch key'), [
E('p', _('HTTP error %d').format(res.status)),
], 7000, 'warning');
return;
}
const fetched = res.responseText?.trim();
if (!fetched || fetched.length > 8192) {
ui.addTimeLimitedNotification(_('Key file too large'), [
E('p', _('Fetched content seems too long. Maximum 8192 bytes.')),
], 7000, 'warning');
return;
}
if (!fetched || fetched.length < 32) {
ui.addTimeLimitedNotification(_('Invalid or empty key file'), [
E('p', _('Fetched content seems empty or too short.')),
], 7000, 'warning');
return;
}
const filename = res?.url?.split('/').pop().split('?')[0].split('#')[0];
// Remove extension if any (we'll re-add based on environment)
const file = {name: filename.replace(/\.[^.]+$/, '') };
addKey(ev, file, fetched);
}).catch(err => {
ui.addTimeLimitedNotification(_('Failed to fetch key'), [
E('p', err.message),
], 7000, 'warning');
});
return;
}
// From here on, key content (either pasted, fetched, or dropped)
const formatError = keyEnvironmentCheck(key);
if (formatError) {
ui.addTimeLimitedNotification(_('Invalid key format'), [
E('p', formatError)
], 7000, 'warning');
return;
}
// Prevent duplicates
listKeyFiles().then(existingKeys => {
if (existingKeys.some(k => normalizeKey(k.key) === normalizeKey(key))) {
ui.addTimeLimitedNotification(_('Add key'), [
E('div', _('The given software repository public key is already present.')),
], 7000, 'notice');
return;
}
// Save and refresh the UI
input.value = '';
saveKeyFile(key, file, fileContent)
.then(() => window.location.reload())
.catch(e => ui.addNotification(null, E('p', e.message)));
});
}
function dragKey(ev) {
ev.stopPropagation();
ev.preventDefault();
ev.dataTransfer.dropEffect = 'copy';
}
function dropKey(ev) {
ev.preventDefault();
ev.stopPropagation();
const input = document.getElementById('key-input');
if (!input)
return;
for (const file of ev.dataTransfer.files) {
const reader = new FileReader();
reader.onload = rev => {
input.value = rev.target.result;
addKey(ev, file, rev.target.result);
input.value = '';
};
reader.readAsText(file);
}
}
function handleWindowDragDropIgnore(ev) {
ev.preventDefault();
}
return view.extend({
load() {
return Promise.all([
determineKeyEnv().then(listKeyFiles),
]);
},
render([keys]) {
const m = new form.JSONMap({
keys: keys,
fup: {},
},
_('Repository Public Keys'), _(
_('Each software repository public key (from official or third party repositories) allows packages in lists signed by it to be installed by the package manager.') + '<br/>' +
_('Each key is stored as a file in %s.').format(`<code>${KEYDIR}</code>`)
));
m.submit = false;
m.reset = false;
m.readonly = isReadonlyView;
let s, o;
s = m.section(form.TableSection, 'keys');
s.anonymous = true;
s.nodescriptions = true;
o = s.option(form.DummyValue, 'filename', _('Name'));
o.width = '20%';
o = s.option(form.TextValue, 'key', _('Key'));
o.readonly = true;
o.monospace = true;
o.cols = 85;
o.rows = 5;
s.renderRowActions = function (section_id) {
const key = this.map.data.get(this.map.config, section_id);
const isReservedKey = isFileInSafeList(key.filename);
const btns = [
E('button', {
'class': 'cbi-button cbi-button-negative remove',
'click': ui.createHandlerFn(this, this.handleRemove, key),
'disabled': isReservedKey ? true : null,
}, [_('Delete')]),
];
return E('td', { 'class': 'td middle cbi-section-actions' }, E('div', btns));
};
s.handleRemove = function(key, ev) {
if (isFileInSafeList(key.filename)) {
ui.addTimeLimitedNotification(null, E('p', _('This key is protected and cannot be deleted.')), 3000, 'warning');
return;
}
return removeKey(ev, key)
};
s = m.section(form.NamedSection, 'fup');
o = s.option(form.DummyValue, '_newkey');
o.cfgvalue = function(/* section_id*/) {
const addInput = E('textarea', {
id: 'key-input',
'aria-label': _('Paste or drag repository public key'),
class: 'cbi-input-text',
type: 'text',
style: 'width: 100%; min-height: 120px;',
placeholder: _('Paste content of a file, or a URL to a key file, or drag and drop here to upload a software repository public key…'),
keydown: function(ev) { if (ev.keyCode === 13 && (ev.ctrlKey || ev.metaKey)) addKey(ev); },
disabled: isReadonlyView
});
addInput.addEventListener('dragover', handleWindowDragDropIgnore);
addInput.addEventListener('drop', handleWindowDragDropIgnore);
const addBtn = E('button', {
class: 'cbi-button',
click: ui.createHandlerFn(this, addKey),
disabled: isReadonlyView
}, _('Add key'));
return E('div', {
class: 'cbi-section-node',
dragover: isReadonlyView ? null : dragKey,
drop: isReadonlyView ? null : dropKey
}, [
E('div', { class: 'cbi-section-descr' }, _('Add new repository public key by pasting its content, a file, or a URL.')),
E('div', {
'style': 'height: 20px',
}, [' ']),
addInput,
E('div', { class: 'right' }, [ addBtn ])
]);
};
return m.render();
},
handleSaveApply: null,
handleSave: null,
handleReset: null
});
|