Files
Kelx/public/js/template-volumes.js
2026-11-30 16:32:37 +00:00

64 lines
2.8 KiB
JavaScript

(function (root) {
'use strict';
const MAX_MOUNTS = 32;
const MAX_SPEC_LENGTH = 512;
function destination(spec) {
const segments = [];
for (const segment of spec.split(':', 2)[1].split('/')) {
if (segment === '..') segments.pop();
else if (segment && segment !== '.') segments.push(segment);
}
return '/' + segments.join('/');
}
// adpanel:template-bind-scope - mounts must stay in the server's managed
// storage so both backup and reinstall cover the configured files.
function sanitizeVolumeSpec(entry) {
const spec = String(entry ?? '').trim();
if (!spec || spec.length > MAX_SPEC_LENGTH || /[\0\r\n]/.test(spec)) return '';
const parts = spec.split(':');
if (parts.length < 2 || parts.length > 3) return '';
const [source, target, mode] = parts;
if (!/^\{(?:BOT_DIR|SERVER_DIR|DATA_DIR)\}(?:\/|$)/.test(source) || /\s/.test(source) || source.includes('\\')) return '';
if (source.split('/').some(segment => segment === '.' || segment === '..')) return '';
if (!target.startsWith('/') || /\s/.test(target) || destination(spec) === '/') return '';
if (mode !== undefined && !/^[A-Za-z0-9,]+$/.test(mode)) return '';
return spec;
}
function sanitizeTemplateVolumeList(entries) {
if (!Array.isArray(entries)) return [];
return entries.map(sanitizeVolumeSpec).filter(Boolean).slice(0, MAX_MOUNTS);
}
function validateTemplateVolumeListPayload(value) {
if (value == null) return { volumes: [] };
if (typeof value === 'string') value = value.trim() ? [value] : [];
if (!Array.isArray(value)) {
const kind = typeof value === 'object' ? 'an object' : `a ${typeof value}`;
return { error: `expected an array of "host:container" mount strings, not ${kind}` };
}
if (value.length > MAX_MOUNTS) return { error: `too many volume mounts (max ${MAX_MOUNTS})` };
const volumes = [];
const targets = new Set();
for (const entry of value) {
const spec = String(entry ?? '').trim();
if (!spec) continue;
if (!sanitizeVolumeSpec(spec)) {
const shown = spec.length > 120 ? spec.slice(0, 120) + '...' : spec;
return { error: `invalid volume mount "${shown}"; use a {BOT_DIR}, {SERVER_DIR}, or {DATA_DIR} source and an absolute container directory other than /` };
}
const target = destination(spec);
if (targets.has(target)) return { error: `duplicate volume mount destination "${target}"` };
targets.add(target);
volumes.push(spec);
}
return { volumes };
}
const api = { sanitizeVolumeSpec, sanitizeTemplateVolumeList, validateTemplateVolumeListPayload };
if (typeof module !== 'undefined' && module.exports) module.exports = api;
else root.ADPanelTemplateVolumes = api;
})(typeof window !== 'undefined' ? window : globalThis);