Files
wekan/models/trelloCreator.js
T
Lauri Ojansivu 4c183b4d59 GHSA-j9p2-jm73-p549 and GHSA-c5xr-mg26-vq5w: two ways past the SSRF guard.
Two separately reported bypasses of the same defence, fixed together because
they are the same mistake in the two halves of it: the guard decided from
something it did not control.

FollowBleed (GHSA-j9p2-jm73-p549, reported by RandomGenerator against v10.53) -
the live Trello import validated the attachment URL with validateAttachmentUrl(),
which is the fix that closed LiveBleed / CVE-2026-30844, and then downloaded it
with the platform fetch(), which FOLLOWS REDIRECTS. So the guard only ever saw
the request, and the target gets to answer:

  1. the attacker puts http://<public-host>/attachment.txt on a Trello card
  2. validateAttachmentUrl() resolves it, sees a public IP, allows it
  3. that host answers  302 Location: http://127.0.0.1:18080/secret
  4. fetch() follows, and the loopback body is stored as the imported
     attachment, readable back through WeKan

Non-blind SSRF against loopback services, internal admin panels, cloud metadata
and anything else reachable from the container - the exact thing the validation
was added to stop, reached through the response instead of the request.

A guard on the URL alone cannot hold, so fetchSafe() now guards EVERY HOP.
`maxRedirects` (default 0) is how many redirects a caller is willing to follow:
0 keeps the old behaviour of refusing any 3xx outright, which is right for
outgoing webhooks and avatar downloads because a legitimate one never redirects.
A caller that must follow one passes a small number, and each hop goes through
the same protocol allowlist, blocked-range check and DNS pinning as the original
URL before a packet is sent to it. Credentials are dropped on a cross-origin
redirect, so following Trello's 302 to S3 cannot hand the API key and token to
whoever the redirect names.

All three live-import sinks - downloadAttachmentBase64, inlineBoardBackground,
inlineMemberAvatars - download through it. Refusing every redirect was not an
option: Trello's own attachment endpoint answers with a 302 to a signed S3 URL,
so that would have meant importing no attachments at all.

The offline importers had the same hole and were not in the report. They handed
the validated URL to Attachments.loadAsync(), and Meteor-Files downloads with
the platform fetch() too, so a pasted Trello or WeKan board export reached
127.0.0.1 by exactly the same 302. models/lib/importAttachmentDownload.js
validates, downloads through fetchSafe with every hop checked, and hands back
the bytes, which are stored with the writeAsync() both importers already use for
an attachment that arrived inline.

TransitBleed (GHSA-c5xr-mg26-vq5w, reported by tonghuaroot) - isIpBlocked() in
models/lib/attachmentUrlValidation.js is the one block-list behind BOTH the
input-time validator and the delivery-time guard, and its IPv6 half classified
an address by its spelling: startsWith('::ffff:'), startsWith('2001:db8'), and
the first hextet parsed out of the string. IPv6 has several standard ways to
write "this packet goes to an IPv4 address" and none of them looks like
`::ffff:`:

  2002:a9fe:a9fe::       6to4 (RFC 3056)     -> 169.254.169.254
  64:ff9b::c0a8:101      NAT64 (RFC 6052)    -> 192.168.1.1
  2001:0:...             Teredo (RFC 4380)   -> complement of the low 32 bits
  0:0:0:0:0:ffff:7f00:1  IPv4-mapped, spelled out -> 127.0.0.1

On a host with a 6to4 relay or a NAT64 gateway, which is ordinary in cloud and
Kubernetes networks, the packet arrives at that IPv4 address. So
http://[2002:a9fe:a9fe::]/latest/meta-data/ read cloud metadata straight through
the guard whose whole job was to stop it, and even a plain IPv4-mapped address
written out in full missed the check.

The address is EXPANDED to its 16 bytes once and every check reads those bytes,
so notation cannot change the answer, and every transition form has its embedded
IPv4 extracted and re-checked with the IPv4 rules - 6to4, NAT64 (well-known and
RFC 8215 local-use prefixes), Teredo through both its server and its obfuscated
client address, IPv4-mapped, IPv4-translated, IPv4-compatible and ISATAP under
any routing prefix, not only the link-local one. The deprecated fec0::/10
site-local range is blocked too. A transition address wrapping a PUBLIC IPv4 is
still allowed, because a guard that blocks everything gets switched off.

Tests. tests/followbleed.test.cjs replays the reported attack against a stubbed
transport - a public host that 302s to 127.0.0.1 - and asserts the second hop is
never sent, then covers redirects to a hostname resolving to a private IP, to
metadata, to a non-http scheme, relative Location, the chain limit, credential
stripping across origins, 303/307 method handling, and that a legitimate
public-to-public redirect is still followed with each hop pinned. It also pins
the sinks: the only bare fetch() left in the live import is the hardcoded
api.trello.com call, and neither offline importer uses loadAsync any more.
tests/transitbleed.test.cjs checks every transition form in both directions.
Both are in the plain-node suite; the guard's own dnsbleed suite gained the same
cases. 300 node suites pass.

Thanks to RandomGenerator and tonghuaroot for the coordinated disclosures, and
xet7 !
2026-08-03 15:30:15 +03:00

1161 lines
42 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { ReactiveCache } from '/imports/reactiveCache';
import { TAPi18n } from '/imports/i18n';
import Activities from '/models/activities';
import Attachments from '/models/attachments';
import Boards from '/models/boards';
import Users from '/models/users';
import { generateUniversalAttachmentUrl } from '/models/lib/universalUrlGenerator';
import { BOARD_COLORS, CARD_COLORS } from '/models/metadata/colors';
import { trelloStickerToFa, trelloStickerHighlight } from '/models/metadata/stickers';
import CardComments from '/models/cardComments';
import Cards from '/models/cards';
import ChecklistItems from '/models/checklistItems';
import Checklists from '/models/checklists';
import CustomFields from './customFields';
import Lists from '/models/lists';
import Swimlanes from '/models/swimlanes';
import {
formatDateTime,
formatDate,
formatTime,
getISOWeek,
isValidDate,
isBefore,
isAfter,
isSame,
add,
subtract,
startOf,
endOf,
format,
parseDate,
now,
createDate,
fromNow,
calendar
} from '/imports/lib/dateUtils';
import getSlug from 'limax';
import { fetchImportedAttachment } from './lib/importAttachmentDownload';
const DateString = Match.Where(function(dateAsString) {
check(dateAsString, String);
return isValidDate(new Date(dateAsString));
});
export class TrelloCreator {
constructor(data) {
// we log current date, to use the same timestamp for all our actions.
// this helps to retrieve all elements performed by the same import.
this._nowDate = new Date();
// The object creation dates, indexed by Trello id
// (so we only parse actions once!)
this.createdAt = {
board: null,
cards: {},
lists: {},
};
// The object creator Trello Id, indexed by the object Trello id
// (so we only parse actions once!)
this.createdBy = {
cards: {}, // only cards have a field for that
};
// Map of labels Trello ID => Wekan ID
this.labels = {};
// Default swimlane
this.swimlane = null;
// Map of lists Trello ID => Wekan ID
this.lists = {};
// Map of cards Trello ID => Wekan ID
this.cards = {};
// Map of attachments Wekan ID => Wekan ID
this.attachmentIds = {};
// Map of checklists Wekan ID => Wekan ID
this.checklists = {};
// The comments, indexed by Trello card id (to map when importing cards)
this.comments = {};
// the members, indexed by Trello member id => Wekan user ID
this.members = data.membersMapping ? data.membersMapping : {};
// maps a trelloCardId to an array of trelloAttachments
this.attachments = {};
this.customFields = {};
}
/**
* If dateString is provided,
* return the Date it represents.
* If not, will return the date when it was first called.
* This is useful for us, as we want all import operations to
* have the exact same date for easier later retrieval.
*
* @param {String} dateString a properly formatted Date
*/
_now(dateString) {
if (dateString) {
return new Date(dateString);
}
if (!this._nowDate) {
this._nowDate = new Date();
}
return this._nowDate;
}
/**
* if trelloUserId is provided and we have a mapping,
* return it.
* Otherwise return current logged user.
* @param trelloUserId
* @private
*/
_user(trelloUserId) {
if (trelloUserId && this.members[trelloUserId]) {
return this.members[trelloUserId];
}
return Meteor.userId();
}
checkActions(trelloActions) {
check(trelloActions, [
Match.ObjectIncluding({
data: Object,
date: DateString,
type: String,
}),
]);
// XXX we could perform more thorough checks based on action type
}
checkBoard(trelloBoard) {
check(
trelloBoard,
Match.ObjectIncluding({
// closed: Boolean, // issue #3840, should import closed Trello boards
name: String,
prefs: Match.ObjectIncluding({
// XXX refine control by validating 'background' against a list of
// allowed values (is it worth the maintenance?)
background: String,
permissionLevel: Match.Where(value => {
return ['org', 'private', 'public'].indexOf(value) >= 0;
}),
}),
}),
);
}
checkCards(trelloCards) {
check(trelloCards, [
Match.ObjectIncluding({
closed: Boolean,
dateLastActivity: DateString,
desc: String,
idLabels: [String],
idMembers: [String],
name: String,
pos: Number,
}),
]);
}
checkLabels(trelloLabels) {
check(trelloLabels, [
Match.ObjectIncluding({
// XXX refine control by validating 'color' against a list of allowed
// values (is it worth the maintenance?)
name: String,
}),
]);
}
checkLists(trelloLists) {
check(trelloLists, [
Match.ObjectIncluding({
closed: Boolean,
name: String,
}),
]);
}
checkChecklists(trelloChecklists) {
check(trelloChecklists, [
Match.ObjectIncluding({
idBoard: String,
idCard: String,
name: String,
checkItems: [
Match.ObjectIncluding({
state: String,
name: String,
}),
],
}),
]);
}
// You must call parseActions before calling this one.
async createBoardAndLabels(trelloBoard) {
let color = 'blue';
if (this.getColor(trelloBoard.prefs.background) !== undefined) {
color = this.getColor(trelloBoard.prefs.background);
}
const boardToCreate = {
archived: trelloBoard.closed,
color: color,
// very old boards won't have a creation activity so no creation date
createdAt: this._now(this.createdAt.board),
labels: [],
customFields: [],
members: [
{
userId: Meteor.userId(),
isAdmin: true,
isActive: true,
isNoComments: false,
isCommentOnly: false,
swimlaneId: false,
},
],
permission: this.getPermission(trelloBoard.prefs.permissionLevel),
slug: getSlug(trelloBoard.name) || 'board',
stars: 0,
title: await Boards.uniqueTitle(trelloBoard.name),
};
// Import the board background image. When Trello uses an image background,
// prefs.backgroundImage is a public URL (and prefs.backgroundImageScaled
// holds scaled variants); Wekan references it directly via
// backgroundImageURL. A solid-color background is already covered by
// `color` above.
const prefs = trelloBoard.prefs || {};
const scaled = Array.isArray(prefs.backgroundImageScaled)
? prefs.backgroundImageScaled
: [];
const bgImage =
prefs.backgroundImage ||
(scaled.length && scaled[scaled.length - 1] && scaled[scaled.length - 1].url);
if (bgImage && /^https?:\/\//i.test(bgImage)) {
boardToCreate.backgroundImageURL = bgImage;
}
// now add other members
if (trelloBoard.memberships) {
trelloBoard.memberships.forEach(trelloMembership => {
const trelloId = trelloMembership.idMember;
// do we have a mapping?
if (this.members[trelloId]) {
const wekanId = this.members[trelloId];
// do we already have it in our list?
const wekanMember = boardToCreate.members.find(
wekanMember => wekanMember.userId === wekanId,
);
if (wekanMember) {
// we're already mapped, but maybe with lower rights
if (!wekanMember.isAdmin) {
wekanMember.isAdmin = this.getAdmin(trelloMembership.memberType);
}
} else {
boardToCreate.members.push({
userId: wekanId,
isAdmin: this.getAdmin(trelloMembership.memberType),
isActive: true,
isNoComments: false,
isCommentOnly: false,
swimlaneId: false,
});
}
}
});
}
if (trelloBoard.labels) {
trelloBoard.labels.forEach(label => {
const labelToCreate = {
_id: Random.id(6),
color: this.mapToWekanColor(label.color) || 'black',
name: label.name,
};
// We need to remember them by Trello ID, as this is the only ref we have
// when importing cards.
this.labels[label.id] = labelToCreate._id;
boardToCreate.labels.push(labelToCreate);
});
}
const boardId = await Boards.direct.insertAsync(boardToCreate);
await Boards.direct.updateAsync(boardId, { $set: { modifiedAt: this._now() } });
// log activity
await Activities.direct.insertAsync({
activityType: 'importBoard',
boardId,
createdAt: this._now(),
source: {
id: trelloBoard.id,
system: 'Trello',
// XSS fix (reported by meifukun): only persist an http(s) source URL.
// trelloBoard.url is attacker-controlled (a javascript: URL in the
// imported board), and the activity sidebar renders it as a link — so a
// hostile scheme must never reach the database (defence in depth with the
// render-time scheme check in activities.js).
url: /^https?:\/\//i.test(String(trelloBoard.url || '')) ? trelloBoard.url : undefined,
},
// We attribute the import to current user,
// not the author from the original object.
userId: this._user(),
});
if (trelloBoard.customFields) {
for (const field of trelloBoard.customFields) {
const fieldToCreate = {
// trelloId: field.id,
name: field.name,
showOnCard: field.display.cardFront,
showLabelOnMiniCard: field.display.cardFront,
automaticallyOnCard: true,
alwaysOnCard: false,
type: field.type,
boardIds: [boardId],
settings: {},
};
if (field.type === 'list') {
fieldToCreate.type = 'dropdown';
fieldToCreate.settings = {
dropdownItems: field.options.map(opt => {
return {
_id: opt.id,
name: opt.value.text,
};
}),
};
}
// We need to remember them by Trello ID, as this is the only ref we have
// when importing cards.
this.customFields[field.id] = await CustomFields.direct.insertAsync(fieldToCreate);
}
}
// Store the board background image as a board-level Attachment (in the
// default attachments storage), served by the same /cdn/storage/attachments
// route as every other attachment. The bytes are downloaded server-side by
// the live API import (which has credentials) and injected as
// trelloBoard.backgroundFile. If there are no bytes (offline JSON import),
// the backgroundImageURL set above keeps Trello's public URL as a fallback.
if (
trelloBoard.backgroundFile &&
trelloBoard.backgroundFile.file &&
Meteor.isServer
) {
try {
const bf = trelloBoard.backgroundFile;
const buffer = Buffer.from(bf.file, 'base64');
const fileRef = await Attachments.writeAsync(
buffer,
{
fileName: bf.name || 'trello-background.jpg',
type: bf.type || 'image/jpeg',
userId: this._user(),
meta: { boardId, source: 'board-background' },
},
true,
);
if (fileRef && fileRef._id) {
await Boards.direct.updateAsync(boardId, {
$set: {
backgroundImageId: fileRef._id,
backgroundImageURL: generateUniversalAttachmentUrl(fileRef._id),
},
});
}
} catch (e) {
if (process.env.DEBUG === 'true') {
console.warn('Failed to store Trello board background', e && e.message);
}
}
}
return boardId;
}
/**
* Create the Wekan cards corresponding to the supplied Trello cards,
* as well as all linked data: activities, comments, and attachments
* @param trelloCards
* @param boardId
* @returns {Array}
*/
async createCards(trelloCards, boardId) {
const result = [];
// .direct.insertAsync below bypasses the before.insert hook that assigns
// cardNumber, so handle it here. Prefer Trello's own short card number
// (idShort, the #N shown in Trello), falling back to a freshly allocated
// one. Without this every imported card defaults to #0.
const boardObj = await ReactiveCache.getBoard(boardId);
for (const card of trelloCards) {
const cardToCreate = {
archived: card.closed,
boardId,
cardNumber: card.idShort || (await boardObj.getNextCardNumber()),
// very old boards won't have a creation activity so no creation date
createdAt: this._now(this.createdAt.cards[card.id]),
dateLastActivity: this._now(),
description: card.desc,
listId: this.lists[card.idList],
swimlaneId: this.swimlane,
sort: card.pos,
title: card.name,
// we attribute the card to its creator if available
userId: this._user(this.createdBy.cards[card.id]),
dueAt: card.due ? this._now(card.due) : null,
// Trello marks a due date complete with a checkbox on the card front
dueComplete: card.dueComplete || false,
};
// card cover: Trello stores it as card.cover = { color, idAttachment, ... }.
// A color cover maps to the Wekan card color; an attachment cover is
// resolved to coverId in the attachment loop below.
if (card.cover && card.cover.color) {
const coverColor = this.mapToWekanColor(card.cover.color);
if (coverColor) {
cardToCreate.color = coverColor;
}
}
// location (Trello map power-up): address / coordinates / location name
if (card.locationName) {
cardToCreate.locationName = card.locationName;
}
if (card.address) {
cardToCreate.locationAddress = card.address;
}
if (card.coordinates) {
if (typeof card.coordinates.latitude === 'number') {
cardToCreate.locationLatitude = card.coordinates.latitude;
}
if (typeof card.coordinates.longitude === 'number') {
cardToCreate.locationLongitude = card.coordinates.longitude;
}
}
// stickers: Trello card.stickers[] = { image, top, left, zIndex, ... }
// where `image` is the sticker name (built-in/premium packs) or a custom
// sticker id (uploaded packs). Map to a similar Font Awesome icon, and
// keep the original name for the tooltip.
if (card.stickers && card.stickers.length > 0) {
cardToCreate.stickers = card.stickers.map((sticker, index) => {
const stickerData = {
icon: this.getStickerIcon(sticker.image),
name: this.stickerLabel(sticker.image),
position: typeof sticker.zIndex === 'number' ? sticker.zIndex : index,
};
const highlight = trelloStickerHighlight(sticker.image);
if (highlight) stickerData.highlight = highlight;
return stickerData;
});
}
// add labels
if (card.idLabels) {
cardToCreate.labelIds = card.idLabels.map(trelloId => {
return this.labels[trelloId];
});
}
// add members {
if (card.idMembers) {
const wekanMembers = [];
// we can't just map, as some members may not have been mapped
card.idMembers.forEach(trelloId => {
if (this.members[trelloId]) {
const wekanId = this.members[trelloId];
// we may map multiple Trello members to the same wekan user
// in which case we risk adding the same user multiple times
if (!wekanMembers.find(wId => wId === wekanId)) {
wekanMembers.push(wekanId);
}
}
return true;
});
if (wekanMembers.length > 0) {
cardToCreate.members = wekanMembers;
}
}
// add vote
if (card.idMembersVoted) {
// Trello only know's positive votes
const positiveVotes = [];
card.idMembersVoted.forEach(trelloId => {
if (this.members[trelloId]) {
const wekanId = this.members[trelloId];
// we may map multiple Trello members to the same wekan user
// in which case we risk adding the same user multiple times
if (!positiveVotes.find(wId => wId === wekanId)) {
positiveVotes.push(wekanId);
}
}
return true;
});
if (positiveVotes.length > 0) {
cardToCreate.vote = {
question: cardToCreate.title,
public: true,
positive: positiveVotes,
};
}
}
if (card.customFieldItems) {
cardToCreate.customFields = [];
card.customFieldItems.forEach(item => {
const custom = {
_id: this.customFields[item.idCustomField],
};
if (item.idValue) {
custom.value = item.idValue;
} else if (item.value.hasOwnProperty('checked')) {
custom.value = item.value.checked === 'true';
} else if (item.value.hasOwnProperty('text')) {
custom.value = item.value.text;
} else if (item.value.hasOwnProperty('date')) {
custom.value = item.value.date;
} else if (item.value.hasOwnProperty('number')) {
custom.value = item.value.number;
}
cardToCreate.customFields.push(custom);
});
}
// insert card
const cardId = await Cards.direct.insertAsync(cardToCreate);
// keep track of Trello id => Wekan id
this.cards[card.id] = cardId;
// log activity
// Activities.direct.insert({
// activityType: 'importCard',
// boardId,
// cardId,
// createdAt: this._now(),
// listId: cardToCreate.listId,
// source: {
// id: card.id,
// system: 'Trello',
// url: card.url,
// },
// // we attribute the import to current user,
// // not the author of the original card
// userId: this._user(),
// });
// add comments
const comments = this.comments[card.id];
if (comments) {
for (const comment of comments) {
const commentToCreate = {
boardId,
cardId,
createdAt: this._now(comment.date),
text: comment.data.text,
// we attribute the comment to the original author, default to current user
userId: this._user(comment.idMemberCreator),
};
// dateLastActivity will be set from activity insert, no need to
// update it ourselves
const commentId = await CardComments.direct.insertAsync(commentToCreate);
// We need to keep adding comment activities this way with Trello
// because it doesn't provide a comment ID
await Activities.direct.insertAsync({
activityType: 'addComment',
boardId: commentToCreate.boardId,
cardId: commentToCreate.cardId,
commentId,
createdAt: this._now(comment.date),
// we attribute the addComment (not the import)
// to the original author - it is needed by some UI elements.
userId: commentToCreate.userId,
});
}
}
// Gather this card's attachments from BOTH the addAttachmentToCard
// actions (this.attachments) and the card.attachments[] array present in
// newer Trello exports, de-duplicated by Trello attachment id. `file`
// (base64) is injected client-side when a matching file was found in the
// uploaded attachments ZIP (the offline TCAD download).
const mergedAttachments = [];
const attachmentsById = new Map();
const pushAttachment = raw => {
if (!raw) return;
const id = raw.id || raw._id || `__noid_${mergedAttachments.length}`;
const norm = {
id: raw.id || raw._id,
name: raw.name || raw.fileName || '',
fileName: raw.fileName || raw.name || '',
url: raw.url || '',
type: raw.mimeType || raw.type || undefined,
userId: raw.idMemberCreator || raw.userId,
file: raw.file,
};
const existing = attachmentsById.get(id);
if (existing) {
// Same attachment seen in both actions and card.attachments[].
// Fill in whichever copy carries the bytes / url / type.
if (!existing.file && norm.file) existing.file = norm.file;
if (!existing.url && norm.url) existing.url = norm.url;
if (!existing.type && norm.type) existing.type = norm.type;
return;
}
attachmentsById.set(id, norm);
mergedAttachments.push(norm);
};
(this.attachments[card.id] || []).forEach(pushAttachment);
(card.attachments || []).forEach(pushAttachment);
const trelloCoverId =
card.idAttachmentCover || (card.cover && card.cover.idAttachment);
if (mergedAttachments.length && Meteor.isServer) {
// Trello "link attachments" (where the attachment name is the URL
// itself) are not real files. Collect them here and append them to the
// card description below, instead of trying to download them.
const links = [];
for (const att of mergedAttachments) {
// attached link, not a file
if (att.name && att.name === att.url) {
links.push(att.url);
continue;
}
const meta = { boardId, cardId, source: 'import' };
const setCover = async newId => {
if (!newId) return;
this.attachmentIds[att.id] = newId;
if (trelloCoverId && trelloCoverId === att.id) {
await Cards.direct.updateAsync(cardId, {
$set: { coverId: newId },
});
}
};
try {
if (att.file) {
// Bytes already provided from the uploaded attachments ZIP.
// Insert them directly instead of downloading the
// OAuth-protected Trello URL. writeAsync is the server-side
// Meteor-Files API (insertAsync is client-only).
const buffer = Buffer.from(att.file, 'base64');
const fileRef = await Attachments.writeAsync(
buffer,
{
fileName: att.fileName || att.name || 'attachment',
type: att.type || 'application/octet-stream',
userId: this._user(att.userId),
meta,
},
true,
);
await setCover(fileRef && fileRef._id);
} else if (att.url) {
// Best effort: works for publicly reachable URLs. Trello-hosted
// uploads require OAuth and should instead be supplied via the
// attachments ZIP (offline) or downloaded server-side in the
// live Trello API import.
//
// FollowBleed (GHSA-j9p2-jm73-p549): the download is NOT
// Attachments.loadAsync() any more. Meteor-Files fetches with the
// platform fetch(), which follows redirects, so validating att.url
// and then handing it to loadAsync let a public URL 302 the
// download to 127.0.0.1 and stored that body as the attachment.
// fetchImportedAttachment validates every hop and hands back the
// bytes, which are stored exactly like inline ones above.
const downloaded = await fetchImportedAttachment(att.url);
if (downloaded.blocked) {
if (process.env.DEBUG === 'true') {
console.warn(
'Blocked attachment URL during Trello import:',
downloaded.reason,
att.url,
);
}
// Skip just this attachment; a blocked URL must not abort the
// whole import.
continue;
}
const fileRef = await Attachments.writeAsync(
downloaded.buffer,
{
fileName: att.fileName || att.name || 'attachment',
type: att.type || downloaded.type || 'application/octet-stream',
userId: this._user(att.userId),
meta,
},
true,
);
await setCover(fileRef && fileRef._id);
}
} catch (e) {
if (process.env.DEBUG === 'true') {
console.warn(
'Failed to import Trello attachment',
att.name,
e && e.message,
);
}
// Never let one failed attachment abort the whole import.
}
}
if (links.length) {
let desc = (cardToCreate.description || '').trim();
if (desc) {
desc += '\n\n';
}
desc += `## ${TAPi18n.__('links-heading')}\n`;
links.forEach(link => {
desc += `* ${link}\n`;
});
await Cards.direct.updateAsync(cardId, {
$set: {
description: desc,
},
});
}
}
result.push(cardId);
}
return result;
}
// Create labels if they do not exist and load this.labels.
createLabels(trelloLabels, board) {
trelloLabels.forEach(label => {
const color = label.color;
const name = label.name;
const existingLabel = board.getLabel(name, color);
if (existingLabel) {
this.labels[label.id] = existingLabel._id;
} else {
const idLabelCreated = board.pushLabel(name, color);
this.labels[label.id] = idLabelCreated;
}
});
}
async createLists(trelloLists, boardId) {
for (const list of trelloLists) {
const listToCreate = {
archived: list.closed,
boardId,
// Attach the list to the imported board's default swimlane so it shows
// in Swimlane View (a list appears under a swimlane only when its
// swimlaneId matches that swimlane or is empty). createSwimlanes runs
// before createLists, so this.swimlane is set.
swimlaneId: this.swimlane,
// We are being defensing here by providing a default date (now) if the
// creation date wasn't found on the action log. This happen on old
// Trello boards (eg from 2013) that didn't log the 'createList' action
// we require.
createdAt: this._now(this.createdAt.lists[list.id]),
title: list.name,
sort: list.pos,
};
const listId = await Lists.direct.insertAsync(listToCreate);
await Lists.direct.updateAsync(listId, { $set: { updatedAt: this._now() } });
this.lists[list.id] = listId;
// log activity
// Activities.direct.insert({
// activityType: 'importList',
// boardId,
// createdAt: this._now(),
// listId,
// source: {
// id: list.id,
// system: 'Trello',
// },
// // We attribute the import to current user,
// // not the creator of the original object
// userId: this._user(),
// });
}
}
async createSwimlanes(boardId) {
const swimlaneToCreate = {
archived: false,
boardId,
// We are being defensing here by providing a default date (now) if the
// creation date wasn't found on the action log. This happen on old
// Wekan boards (eg from 2013) that didn't log the 'createList' action
// we require.
createdAt: this._now(),
title: 'Default',
sort: 1,
};
const swimlaneId = await Swimlanes.direct.insertAsync(swimlaneToCreate);
await Swimlanes.direct.updateAsync(swimlaneId, { $set: { updatedAt: this._now() } });
this.swimlane = swimlaneId;
}
async createChecklists(trelloChecklists, boardId) {
for (const checklist of trelloChecklists) {
if (this.cards[checklist.idCard]) {
// Create the checklist
const checklistToCreate = {
cardId: this.cards[checklist.idCard],
// Denormalized boardId; .direct.insert bypasses the before.insert hook
// that would otherwise derive it, so set it explicitly here.
boardId,
title: checklist.name,
createdAt: this._now(),
sort: checklist.pos,
};
const checklistId = await Checklists.direct.insertAsync(checklistToCreate);
// keep track of Trello id => Wekan id
this.checklists[checklist.id] = checklistId;
// Now add the items to the checklistItems
let counter = 0;
for (const item of checklist.checkItems) {
counter++;
const checklistItemTocreate = {
_id: checklistId + counter,
title: item.name,
checklistId: this.checklists[checklist.id],
cardId: this.cards[checklist.idCard],
boardId,
sort: item.pos,
isFinished: item.state === 'complete',
};
await ChecklistItems.direct.insertAsync(checklistItemTocreate);
}
}
}
}
getAdmin(trelloMemberType) {
return trelloMemberType === 'admin';
}
getStickerIcon(trelloStickerName) {
// Map a Trello sticker name to a similar WeKan card sticker icon (Font
// Awesome v4 name, rendered as `i.fa.fa-<name>`). Handles built-in and
// named premium packs via models/metadata/stickers.js.
return trelloStickerToFa(trelloStickerName);
}
// A readable tooltip for an imported sticker. For named stickers (taco-love,
// globe, …) this humanises the name; custom uploaded stickers only have an
// opaque id, so fall back to a generic label rather than showing the id.
stickerLabel(trelloStickerName) {
const raw = String(trelloStickerName || '').trim();
if (!raw) return 'sticker';
// Long hex/base-id strings (custom uploaded stickers) aren't descriptive.
if (/^[0-9a-f]{16,}$/i.test(raw) || /^[A-Za-z0-9_-]{20,}$/.test(raw)) {
return 'custom sticker';
}
// Trello's named premium packs are renamed: taco => mascot, pete => computer
// (e.g. "pete-ghost" => "computer ghost"). The highlight style (underline /
// round ring) is applied separately via trelloStickerHighlight().
return raw
.replace(/[-_]+/g, ' ')
.replace(/\btaco\b/gi, 'mascot')
.replace(/\bpete\b/gi, 'computer')
.trim();
}
// Map a Trello label/cover color to a valid WeKan card/label color
// (CARD_COLORS === LABEL_COLORS === ALLOWED_COLORS). Trello uses base colors
// that WeKan also has (green, blue, red, …) plus `_light`/`_dark` variants
// (e.g. `purple_light`) that WeKan does not, so strip the variant suffix.
// Returns null when there is no valid mapping.
mapToWekanColor(trelloColor) {
if (!trelloColor) return null;
const base = String(trelloColor).split('_')[0];
return CARD_COLORS.includes(base) ? base : null;
}
getColor(trelloColorCode) {
// trello color name => wekan color
const mapColors = {
blue: 'belize',
orange: 'pumpkin',
green: 'nephritis',
red: 'pomegranate',
purple: 'wisteria',
pink: 'moderatepink',
lime: 'limegreen',
sky: 'strongcyan',
grey: 'midnight',
};
const wekanColor = mapColors[trelloColorCode];
return wekanColor || BOARD_COLORS[0];
}
getPermission(trelloPermissionCode) {
if (trelloPermissionCode === 'public') {
return 'public';
}
// Wekan does NOT have organization level, so we default both 'private' and
// 'org' to private.
return 'private';
}
parseActions(trelloActions) {
trelloActions.forEach(action => {
if (action.type === 'addAttachmentToCard') {
// We have to be cautious, because the attachment could have been removed later.
// In that case Trello still reports its addition, but removes its 'url' field.
// So we test for that
const trelloAttachment = action.data.attachment;
// We need the idMemberCreator
trelloAttachment.idMemberCreator = action.idMemberCreator;
if (trelloAttachment.url) {
// we cannot actually create the Wekan attachment, because we don't yet
// have the cards to attach it to, so we store it in the instance variable.
const trelloCardId = action.data.card.id;
if (!this.attachments[trelloCardId]) {
this.attachments[trelloCardId] = [];
}
this.attachments[trelloCardId].push(trelloAttachment);
}
} else if (action.type === 'commentCard') {
const id = action.data.card.id;
if (this.comments[id]) {
this.comments[id].push(action);
} else {
this.comments[id] = [action];
}
} else if (action.type === 'createBoard') {
this.createdAt.board = action.date;
} else if (action.type === 'createCard') {
const cardId = action.data.card.id;
this.createdAt.cards[cardId] = action.date;
this.createdBy.cards[cardId] = action.idMemberCreator;
} else if (action.type === 'createList') {
const listId = action.data.list.id;
this.createdAt.lists[listId] = action.date;
}
});
}
async importActions(actions, boardId) {
for (const action of actions) {
switch (action.type) {
// Board related actions
// TODO: addBoardMember, removeBoardMember
case 'createBoard': {
await Activities.direct.insertAsync({
userId: this._user(action.idMemberCreator),
type: 'board',
activityTypeId: boardId,
activityType: 'createBoard',
boardId,
createdAt: this._now(action.date),
});
break;
}
// List related activities
// TODO: removeList, archivedList
case 'createList': {
await Activities.direct.insertAsync({
userId: this._user(action.idMemberCreator),
type: 'list',
activityType: 'createList',
listId: this.lists[action.data.list.id],
boardId,
createdAt: this._now(action.date),
});
break;
}
// Card related activities
// TODO: archivedCard, restoredCard, joinMember, unjoinMember
case 'createCard': {
await Activities.direct.insertAsync({
userId: this._user(action.idMemberCreator),
activityType: 'createCard',
listId: this.lists[action.data.list.id],
cardId: this.cards[action.data.card.id],
boardId,
createdAt: this._now(action.date),
});
break;
}
case 'updateCard': {
if (action.data.old.idList) {
await Activities.direct.insertAsync({
userId: this._user(action.idMemberCreator),
oldListId: this.lists[action.data.old.idList],
activityType: 'moveCard',
listId: this.lists[action.data.listAfter.id],
cardId: this.cards[action.data.card.id],
boardId,
createdAt: this._now(action.date),
});
}
break;
}
// Comment related activities
// Trello doesn't export the comment id
// Attachment related activities
case 'addAttachmentToCard': {
await Activities.direct.insertAsync({
userId: this._user(action.idMemberCreator),
type: 'card',
activityType: 'addAttachment',
attachmentId: this.attachmentIds[action.data.attachment.id],
cardId: this.cards[action.data.card.id],
boardId,
createdAt: this._now(action.date),
});
break;
}
// Checklist related activities
case 'addChecklistToCard': {
await Activities.direct.insertAsync({
userId: this._user(action.idMemberCreator),
activityType: 'addChecklist',
cardId: this.cards[action.data.card.id],
checklistId: this.checklists[action.data.checklist.id],
boardId,
createdAt: this._now(action.date),
});
break;
}
}
// Trello doesn't have an add checklist item action
}
}
check(board) {
try {
// check(data, {
// membersMapping: Match.Optional(Object),
// });
this.checkActions(board.actions);
this.checkBoard(board);
this.checkLabels(board.labels);
this.checkLists(board.lists);
this.checkCards(board.cards);
this.checkChecklists(board.checklists);
} catch (e) {
throw new Meteor.Error('error-json-schema');
}
}
async create(board, currentBoardId) {
// TODO : Make isSandstorm variable global
const isSandstorm =
Meteor.settings &&
Meteor.settings.public &&
Meteor.settings.public.sandstorm;
if (isSandstorm && currentBoardId) {
const currentBoard = await ReactiveCache.getBoard(currentBoardId);
await currentBoard.archive();
}
// #6506: bring every imported member in as its own user (mapped to an existing
// account or a virtual placeholder) BEFORE creating the board, so board members,
// card authors, comments and activities keep the original person instead of
// collapsing onto the importer.
await this.createPlaceholderUsers(board);
this.parseActions(board.actions);
const boardId = await this.createBoardAndLabels(board);
// Create the default swimlane first so lists can be attached to it (see
// createLists): a list shows under a swimlane in Swimlane View only when its
// swimlaneId matches that swimlane (or is empty).
await this.createSwimlanes(boardId);
await this.createLists(board.lists, boardId);
await this.createCards(board.cards, boardId);
await this.createChecklists(board.checklists, boardId);
await this.importActions(board.actions, boardId);
await this.recordImportedUsernames(board, boardId);
// XXX add members
return boardId;
}
// Pick a free username: keep the original if it is not taken, else suffix it so we
// never rename/steal a different existing account's username.
async _uniquePlaceholderUsername(base) {
const name = (String(base || '').trim()) || 'imported';
let candidate = name;
let n = 0;
// eslint-disable-next-line no-await-in-loop
while (await Meteor.users.findOneAsync({ username: candidate }, { fields: { _id: 1 } })) {
n += 1;
candidate = `${name}-${n}`;
}
return candidate;
}
// #6506: for every Trello member NOT already mapped (by the map-members step or the
// API importer's buildMembersMapping), either map it to an EXISTING WeKan user with
// the same username / recorded importUsername, or create an inert virtual
// (placeholder) user carrying the original username / full name. This keeps imported
// authorship on the original person instead of the importing user, for BOTH the
// Trello JSON import and the Trello API (all-boards) import. Placeholders cannot log
// in (loginDisabled), are inactive until reconciled (isActive:false) and carry no
// secrets. Server-only; best-effort so one bad member never fails the import.
async createPlaceholderUsers(board) {
if (!Meteor.isServer) return;
for (const member of board.members || []) {
if (!member || !member.id) continue;
if (this.members[member.id]) continue; // already mapped to an existing user
if (member.username) {
const existing =
(await ReactiveCache.getUser({ username: member.username })) ||
(await ReactiveCache.getUser({ importUsernames: member.username }));
if (existing) {
this.members[member.id] = existing._id;
continue;
}
}
// Reuse the Trello member id as the WeKan user _id so every reference resolves.
const existingById = await ReactiveCache.getUser(member.id);
if (!existingById) {
const username = await this._uniquePlaceholderUsername(
member.username || member.fullName || `imported-${member.id}`,
);
try {
// .direct bypasses the after.insert hooks (Sandstorm identity sync, the
// registration/invitation gate) that would reject a non-account placeholder.
await Users.direct.insertAsync({
_id: member.id,
username,
profile: {
fullname: member.fullName || '',
initials: String(member.initials || '').slice(0, 4),
},
authenticationMethod: 'imported',
loginDisabled: true,
isActive: false,
importUsernames: member.username ? [member.username] : [],
importedFromBoardId: board.id || null,
importedAt: new Date(),
createdAt: new Date(),
services: {},
});
} catch (e) {
if (process.env.DEBUG === 'true') {
console.warn('trello placeholder user insert:', member.id, e && e.message);
}
}
}
this.members[member.id] = member.id;
}
}
// Keep the original Trello usernames so user mapping can happen later:
// members that were mapped to a WeKan user get the Trello username added to
// that user's importUsernames (so future imports auto-map); members that were
// not mapped have their username stored on the board's importUsernames, where
// an admin can later assign them to a real user via the People panel.
async recordImportedUsernames(board, boardId) {
if (!Meteor.isServer) return;
const unmapped = [];
for (const member of board.members || []) {
if (!member.username) continue;
const wekanId = this.members[member.id];
if (wekanId) {
await Users.updateAsync(wekanId, {
$addToSet: { importUsernames: member.username },
});
} else if (!unmapped.includes(member.username)) {
unmapped.push(member.username);
}
}
if (unmapped.length) {
await Boards.direct.updateAsync(boardId, {
$addToSet: { importUsernames: { $each: unmapped } },
});
}
}
}