mirror of
https://github.com/wekan/wekan.git
synced 2026-11-25 04:49:41 -05:00
Split the testable, security- and correctness-critical logic of the new avatars +
original-members feature into pure Node modules (module.exports, no Meteor) and cover them
with plain-Node tests/*.test.cjs (positive AND negative), matching the repo's existing
unit-test style. Wired into the test:unit:node script.
- models/lib/avatarUrlSafety.js (from server/lib/localizeAvatar.js): isExternalAvatarUrl,
isBlockedIp (SSRF ranges), decodeDataUri, checkUrlHostSync. Tests assert external vs
local/relative/non-http URLs, that private/loopback/link-local/CGNAT/metadata/multicast
IPv4+IPv6 (incl. IPv4-mapped) are blocked while genuine public addresses and near-miss
ranges are allowed, data: decoding, and that non-http(s) schemes, localhost and
metadata hosts are rejected.
- models/lib/importedUserReconciliationPlan.js (from server/importedUserReconciliation.js):
planReconciliation. Tests: matches merge, non-matches deactivate, never match another
placeholder or self or a username-less placeholder.
- packages/wekan-ldap/server/ldapPhoto.js (from sync.js): getLdapPhotoBuffer. Tests the
Buffer / array / {type:'Buffer'} / base64 shapes and jpegPhoto>thumbnailPhoto, and that
missing/empty/short values yield null (never throw).
- models/lib/importedBoardMemberPlan.js (from wekanCreator.js): planImportedBoardMember.
Tests: unmapped member kept by original id but inactive+non-admin, mapped role preserved,
importer and id-less members dropped.
The extractions are behaviour-preserving; the app modules now import these helpers.
Thanks to xet7.
158 lines
7.1 KiB
JavaScript
158 lines
7.1 KiB
JavaScript
import { Meteor } from 'meteor/meteor';
|
|
import { ReactiveCache } from '/imports/reactiveCache';
|
|
import Avatars from '/models/avatars';
|
|
import dns from 'dns';
|
|
import {
|
|
isExternalAvatarUrl,
|
|
isBlockedIp,
|
|
decodeDataUri,
|
|
checkUrlHostSync,
|
|
} from '/models/lib/avatarUrlSafety';
|
|
|
|
// Re-export so existing importers of isExternalAvatarUrl keep working.
|
|
export { isExternalAvatarUrl };
|
|
|
|
// ============================================================================
|
|
// Avatar localization
|
|
// ----------------------------------------------------------------------------
|
|
// Copy a user's avatar that lives OUTSIDE WeKan (a Sandstorm profile-picture
|
|
// static URL, an LDAP jpegPhoto, an OAuth2/OIDC `picture` claim, a gravatar, …)
|
|
// into WeKan's own `avatars` FilesCollection under WRITABLE_PATH/files/avatars,
|
|
// and repoint the user's profile.avatarUrl at that local `/cdn/storage/avatars/…`
|
|
// copy. Once local, the avatar is served by WeKan itself and is included by the
|
|
// board export/import (which embeds avatar files), so an imported board keeps
|
|
// showing who the original members were even on a server that cannot reach the
|
|
// original identity provider.
|
|
//
|
|
// Every fetch is guarded against SSRF: only http/https, and never a private,
|
|
// loopback, link-local or cloud-metadata address.
|
|
// ============================================================================
|
|
|
|
// Cap the download so a hostile or broken URL cannot exhaust memory. Avatars are
|
|
// tiny; WeKan's own avatar upload limit is ~72 KB, but allow some headroom for
|
|
// provider images that WeKan may downscale later.
|
|
const MAX_AVATAR_BYTES = 5 * 1024 * 1024;
|
|
const FETCH_TIMEOUT_MS = 8000;
|
|
|
|
// Full SSRF guard: synchronous protocol/IP-literal/localhost checks (shared, pure) plus
|
|
// hostname DNS resolution that blocks if ANY resolved address is private/loopback.
|
|
async function assertSafePublicUrl(url) {
|
|
const res = checkUrlHostSync(url);
|
|
if (!res.ok) throw new Error(res.reason);
|
|
if (!res.needsDnsCheck) return;
|
|
const addrs = await dns.promises.lookup(res.host, { all: true }).catch(() => []);
|
|
if (!addrs.length) throw new Error(`cannot resolve ${res.host}`);
|
|
for (const a of addrs) {
|
|
if (isBlockedIp(a.address)) throw new Error(`blocked resolved address ${a.address} for ${res.host}`);
|
|
}
|
|
}
|
|
|
|
// Fetch bytes for an avatar URL (or decode a data: URI), with SSRF guards, a
|
|
// timeout and a size cap. Returns { buffer, type } or null on any failure.
|
|
async function fetchAvatarBytes(url) {
|
|
try {
|
|
if (/^data:/i.test(url)) return decodeDataUri(url);
|
|
await assertSafePublicUrl(url);
|
|
const controller = new AbortController();
|
|
const timer = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS);
|
|
let res;
|
|
try {
|
|
res = await fetch(url, { redirect: 'follow', signal: controller.signal });
|
|
} finally { clearTimeout(timer); }
|
|
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
const type = (res.headers.get('content-type') || '').split(';')[0].trim() || 'image/png';
|
|
if (!/^image\//i.test(type)) throw new Error(`not an image (${type})`);
|
|
const ab = await res.arrayBuffer();
|
|
if (ab.byteLength > MAX_AVATAR_BYTES) throw new Error('avatar too large');
|
|
if (!ab.byteLength) throw new Error('empty avatar');
|
|
return { buffer: Buffer.from(ab), type };
|
|
} catch (e) {
|
|
if (process.env.DEBUG === 'true') console.warn('localizeAvatar: fetch failed for', url, '-', e.message);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function extForType(type) {
|
|
const map = {
|
|
'image/png': 'png', 'image/jpeg': 'jpg', 'image/jpg': 'jpg', 'image/gif': 'gif',
|
|
'image/webp': 'webp', 'image/bmp': 'bmp', 'image/svg+xml': 'svg', 'image/avif': 'avif',
|
|
};
|
|
return map[(type || '').toLowerCase()] || 'png';
|
|
}
|
|
|
|
// Write raw bytes into the Avatars FilesCollection for `userId`. onAfterUpload
|
|
// (models/avatars.server.js) sets the user's profile.avatarUrl to the resulting
|
|
// local /cdn/storage/avatars/<id> URL. Returns the new local URL, or null.
|
|
export async function localizeAvatarFromBuffer(userId, buffer, { type = 'image/png', name } = {}) {
|
|
if (!Meteor.isServer || !userId || !buffer || !buffer.length) return null;
|
|
const ext = extForType(type);
|
|
const fileName = (name && String(name).replace(/[^a-zA-Z0-9_.\-]/g, '_')) || `avatar.${ext}`;
|
|
return await new Promise((resolve) => {
|
|
try {
|
|
Avatars.write(
|
|
buffer,
|
|
{ fileName, type, userId, meta: { source: 'localized-external' } },
|
|
(writeErr, fileRef) => {
|
|
if (writeErr || !fileRef) {
|
|
if (process.env.DEBUG === 'true') console.warn('localizeAvatar: write failed:', writeErr && writeErr.message);
|
|
return resolve(null);
|
|
}
|
|
// onAfterUpload has set profile.avatarUrl asynchronously; report the id-based URL.
|
|
resolve(`/cdn/storage/avatars/${fileRef._id}`);
|
|
},
|
|
true, // proceedAfterUpload → runs onAfterUpload (validation + setAvatarUrl)
|
|
);
|
|
} catch (e) {
|
|
if (process.env.DEBUG === 'true') console.warn('localizeAvatar: write threw:', e.message);
|
|
resolve(null);
|
|
}
|
|
});
|
|
}
|
|
|
|
// Fetch an external avatar URL and store it locally for `userId`.
|
|
export async function localizeAvatarFromUrl(userId, url) {
|
|
if (!Meteor.isServer || !userId || !isExternalAvatarUrl(url)) return null;
|
|
const got = await fetchAvatarBytes(url);
|
|
if (!got) return null;
|
|
return await localizeAvatarFromBuffer(userId, got.buffer, {
|
|
type: got.type,
|
|
name: `${userId}.${extForType(got.type)}`,
|
|
});
|
|
}
|
|
|
|
// If the user's current avatar lives outside WeKan, copy it in. Best-effort and
|
|
// idempotent: a user whose avatarUrl is already local is left untouched.
|
|
export async function localizeUserAvatarIfExternal(userOrId) {
|
|
if (!Meteor.isServer) return null;
|
|
const user = typeof userOrId === 'string' ? await ReactiveCache.getUser(userOrId) : userOrId;
|
|
if (!user) return null;
|
|
const url = user.profile && user.profile.avatarUrl;
|
|
if (!isExternalAvatarUrl(url)) return null;
|
|
return await localizeAvatarFromUrl(user._id, url);
|
|
}
|
|
|
|
// Guards against re-fetching the same user's avatar while a localization is already
|
|
// running (e.g. rapid board re-subscribes), so a member's external avatar is copied
|
|
// in at most once.
|
|
const inProgress = new Set();
|
|
|
|
// Localize every board member's external avatar. Best-effort, fire-and-forget: this
|
|
// is the universal catch-all trigger — no matter how a member's avatar became external
|
|
// (Sandstorm, LDAP, OAuth2/OIDC, a pasted URL), opening the board copies it into
|
|
// WeKan's own files/avatars so it displays without the provider and is exportable.
|
|
export async function localizeBoardMemberAvatars(boardId) {
|
|
if (!Meteor.isServer || !boardId) return;
|
|
const board = await ReactiveCache.getBoard(boardId);
|
|
if (!board || !Array.isArray(board.members)) return;
|
|
for (const m of board.members) {
|
|
const uid = m && m.userId;
|
|
if (!uid || inProgress.has(uid)) continue;
|
|
const user = await ReactiveCache.getUser(uid);
|
|
if (!user || !isExternalAvatarUrl(user.profile && user.profile.avatarUrl)) continue;
|
|
inProgress.add(uid);
|
|
try { await localizeUserAvatarIfExternal(user); }
|
|
catch (e) { if (process.env.DEBUG === 'true') console.warn('localizeBoardMemberAvatars:', uid, e.message); }
|
|
finally { inProgress.delete(uid); }
|
|
}
|
|
}
|