Files
wekan/server/methods/listSync.js
T
Lauri Ojansivu 2172f19d88 Fix SyncBleed: send List Sync requests through the SSRF guard and keep responses out of errors.
GHSA-5q84-p3vr-f3xv: a board member with write access chooses the List Sync
server URL for Gitea, Forgejo, GitLab and Jira. It was fetched with the
platform fetch(), so it reached 127.0.0.1, private networks and the cloud
metadata address, and up to 200 bytes of a failed response were copied into
the error the preview returns and lastSyncError stores. An open and a closed
internal port also answered differently.

Every request now goes through fetchSafe: internal addresses refused, DNS
pinned, redirects refused, response size and time bounded. An error carries the
origin and HTTP status only, a non-JSON answer says so without quoting it, and
a guard refusal is one message that does not name what a host resolved to.
Saving Sync settings refuses an internal address too, so none is stored. Both
refusals are recorded as SyncBleed in Admin Panel -> Problems.

A self-hosted tracker on the server's network is allowed only by the
administrator, with LIST_SYNC_ALLOWED_PRIVATE_HOSTS (exact hosts); the test
server allows 127.0.0.1 for the List Sync specs' mock tracker. The browser test
found that an IPv6 literal such as [::1] kept its brackets and was looked up as
a name at save time; the shared validator now checks it as an address.

tests/syncBleed.test.cjs reproduces the report on the real fetcher for every
provider, and fails on a platform fetch() to anything but a fixed or
administrator-allowed host, or a response body copied into an error, anywhere
in the server tree. list-sync-source.e2e.js saves and previews internal
addresses in the browser and checks no request reaches the mock tracker.
securityRegressionCoverage lists CacheBleed and SyncBleed with their tests.

Thanks to alham-rizvi and xet7 !
2026-10-02 09:46:00 +03:00

230 lines
11 KiB
JavaScript

// List sync configuration methods (docs/Features/ImportExport/Sync.md). Lets
// a board member with write access mark a list as synced from an external
// tracker and store its credential. The credential itself is written into
// ListSyncCredentials (models/listSyncCredentials.js), a collection with no
// publication anywhere in the codebase - `listSync.hasCredential` only ever
// returns a boolean, never the token, mirroring how
// server/models/attachmentStorageSettings.js masks its cloud secrets before
// they can reach the client.
import { Meteor } from 'meteor/meteor';
import { check, Match } from 'meteor/check';
import { DDPRateLimiter } from 'meteor/ddp-rate-limiter';
import Lists from '/models/lists';
import Cards from '/models/cards';
import CustomFields from '/models/customFields';
const { syncEstimateMapping } = require('/models/lib/listSyncEstimate');
const { TIME_FIELDS, syncTimeMappings, timeMappingIdentities } = require('/models/lib/listSyncTimeEstimates');
import ListSyncCredentials from '/models/listSyncCredentials';
import ListSyncRunReports from '/server/lib/listSyncRunReports';
import { assertSyncUrlAllowed } from '/server/lib/listSyncFetch';
const { reportScope } = require('/server/lib/syncRunReport');
const { syncRunReportPage } = require('/server/lib/syncRunReportPage');
import { ReactiveCache } from '/imports/reactiveCache';
import { allowIsBoardMemberWithWriteAccess } from '/server/lib/utils';
import { SYNC_CAPABLE_SOURCES } from '/models/lib/externalParsers';
import { syncOneList } from '/server/listSync';
import { withListSyncLease } from '/server/lib/listSyncLease';
const { normalizeSyncSource, syncSourceKey } = require('/models/lib/listSyncSourceIdentity');
const { readSyncCredential, commitSyncConfiguration } = require('/server/lib/listSyncConfiguration');
const { assignedOnlyCardScope } = require('/models/lib/boardCardScope');
async function assertWriteAccess(userId, boardId) {
if (!userId) {
throw new Meteor.Error('not-authorized', 'You must be logged in.');
}
const board = await ReactiveCache.getBoard(boardId);
if (!board || !allowIsBoardMemberWithWriteAccess(userId, board)) {
throw new Meteor.Error('not-authorized', 'You do not have write access to this board.');
}
return board;
}
async function assertConflictAccess(userId, boardId) {
const board = await assertWriteAccess(userId, boardId);
return assignedOnlyCardScope(board, userId);
}
Meteor.methods({
// Configure (or clear) a list's sync source. `token`/`username` are stored
// separately in ListSyncCredentials; pass `token: null` to leave a
// previously stored credential untouched only for the SAME source.
async setListSyncSource(listId, config) {
check(listId, String);
check(config, Match.OneOf(null, {
type: Match.OneOf(...SYNC_CAPABLE_SOURCES),
url: Match.Optional(String),
projectKey: String,
enabled: Match.Optional(Boolean),
createCards: Match.Optional(Boolean),
archiveCards: Match.Optional(Boolean),
estimateCustomFieldId: Match.Optional(String),
fields: Match.Optional([Match.OneOf('title', 'description', 'spentTime', 'estimate', 'originalEstimate', 'remainingEstimate')]),
token: Match.Optional(Match.OneOf(String, null)),
username: Match.Optional(String),
}));
const list = await Lists.findOneAsync(listId);
if (!list) throw new Meteor.Error('list-not-found', 'List not found.');
await assertWriteAccess(this.userId, list.boardId);
return withListSyncLease(listId, async ({ assertCurrent }) => {
const list = await Lists.findOneAsync(listId);
if (!list) throw new Meteor.Error('list-not-found', 'List not found.');
await assertWriteAccess(this.userId, list.boardId);
let source, sourceKey, oldKey;
try {
source = config === null ? null : normalizeSyncSource(config);
sourceKey = source && syncSourceKey(source);
} catch (error) {
throw new Meteor.Error('invalid-sync-source', error.message);
}
// SyncBleed: never store an address the fetch would refuse.
if (source) {
try { await assertSyncUrlAllowed(source.url, { userId: this.userId }); } catch (error) {
throw new Meteor.Error('sync-url-blocked', error.message);
}
}
// An old malformed URL must not prevent disconnecting a broken source.
// Its legacy cards remain unbound; a new project must never adopt them.
try { oldKey = list.syncSource && syncSourceKey(list.syncSource); } catch (error) { oldKey = null; }
let estimateMapping;
let timeMappings = {};
try {
estimateMapping = source && syncEstimateMapping(config,
config.estimateCustomFieldId && await CustomFields.findOneAsync({
_id: config.estimateCustomFieldId, boardIds: list.boardId }));
if (source) timeMappings = syncTimeMappings(config, config.fields?.some(field => Object.hasOwn(TIME_FIELDS, field))
? await CustomFields.find({ boardIds: list.boardId, 'settings.jiraTimeField': { $in: ['original', 'remaining'] } }).fetchAsync() : [], estimateMapping);
} catch (error) { throw new Meteor.Error('invalid-sync-source', error.message); }
// Before replacing or clearing a legacy configuration, retain its identity
// on its cards. Never infer an old card's project from the NEW config.
const legacyCards = { boardId: list.boardId, listId,
syncSourceType: { $exists: true }, syncSourceKey: { $exists: false } };
const unknownCards = oldKey ? { ...legacyCards,
syncSourceType: { $exists: true, $ne: list.syncSource.type } } : legacyCards;
if (source && await Cards.findOneAsync(unknownCards)) {
throw new Meteor.Error('sync-source-unknown',
'This list has legacy Sync cards without a known source. Configure Sync in a new list.');
}
if (oldKey) {
await assertCurrent();
await Cards.updateAsync({ ...legacyCards, syncSourceType: list.syncSource.type },
{ $set: { syncSourceKey: oldKey } }, { multi: true });
}
const previousCredential = await readSyncCredential(ListSyncCredentials, list);
let credential = null;
if (config?.token) {
credential = { token: config.token, username: config.username || '', sourceKey };
} else if (source && previousCredential && oldKey === sourceKey &&
(!previousCredential.sourceKey || previousCredential.sourceKey === sourceKey)) {
credential = { ...previousCredential, sourceKey };
}
const publicSource = source ? {
type: source.type, url: source.url, projectKey: source.projectKey,
enabled: config.enabled !== false,
createCards: config.createCards !== false,
archiveCards: config.archiveCards !== false,
fields: config.fields || ['title', 'description'],
...(Object.keys(timeMappings).length ? { timeMappingIdentities: timeMappingIdentities(timeMappings) } : {}),
...(estimateMapping ? { estimateCustomFieldId: estimateMapping.localFieldId,
estimateMappingIdentity: estimateMapping.identity } : {}),
} : null;
try {
return await commitSyncConfiguration({ lists: Lists, credentials: ListSyncCredentials,
list, source: publicSource,
credential: credential && { ...credential, runAsUserId: this.userId },
previousCredential, assertCurrent });
} catch (error) {
if (error.code === 'sync-config-changed') {
throw new Meteor.Error(error.code, error.message);
}
throw error;
}
});
},
async hasListSyncCredential(listId) {
check(listId, String);
const list = await Lists.findOneAsync(listId);
if (!list) throw new Meteor.Error('list-not-found', 'List not found.');
await assertWriteAccess(this.userId, list.boardId);
const credential = await readSyncCredential(ListSyncCredentials, list);
if (!credential || !list.syncSource) return false;
try { return credential.sourceKey === syncSourceKey(list.syncSource); } catch (error) { return false; }
},
// Manual "sync now" - runs the same reconcile the periodic cron job runs,
// synchronously, so the UI can show the result immediately.
async syncListNow(listId) {
check(listId, String);
const list = await Lists.findOneAsync(listId);
if (!list) throw new Meteor.Error('list-not-found', 'List not found.');
await assertWriteAccess(this.userId, list.boardId);
return syncOneList(list, { previewConflicts: true,
assertConflictAccess: () => assertConflictAccess(this.userId, list.boardId) });
},
async syncRecoveryReport(query) {
check(query, { search: String, status: String, page: Number });
const assertAdmin = async () => {
const user = this.userId && await Meteor.users.findOneAsync(this.userId, { fields: { isAdmin: 1 } });
if (!user?.isAdmin) throw new Meteor.Error('not-authorized');
};
await assertAdmin();
const result = await syncRunReportPage(ListSyncRunReports.rawCollection(), query);
await assertAdmin();
return result;
},
async listSyncRunReports(listId) {
check(listId, String);
const list = await Lists.findOneAsync(listId);
if (!list) throw new Meteor.Error('list-not-found', 'List not found.');
const access = async () => {
if (await assertConflictAccess(this.userId, list.boardId)) {
throw new Meteor.Error('not-authorized', 'Full-list write access is required.');
}
const current = await Lists.findOneAsync(listId);
if (!current || JSON.stringify(reportScope(current)) !== JSON.stringify(reportScope(list))) {
throw new Meteor.Error('list-changed', 'The list changed.');
}
};
await access();
const reports = await ListSyncRunReports.find({ ...reportScope(list),
startedAt: { $gte: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000) } }, {
sort: { startedAt: -1, _id: -1 }, limit: 20,
fields: { startedAt: 1, finishedAt: 1, status: 1, created: 1,
updated: 1, archived: 1, coverage: 1 },
}).fetchAsync();
await access();
return reports;
},
async previewListSync(listId) {
check(listId, String);
const list = await Lists.findOneAsync(listId);
if (!list) throw new Meteor.Error('list-not-found', 'List not found.');
await assertWriteAccess(this.userId, list.boardId);
return syncOneList(list, { dryRun: true,
assertConflictAccess: () => assertConflictAccess(this.userId, list.boardId) });
},
async resolveListSyncConflict(listId, resolution) {
check(listId, String);
check(resolution, { cardId: String, field: Match.OneOf('title', 'description', 'spentTime', 'estimate', 'originalEstimate', 'remainingEstimate', 'syncExternalId', 'archive', 'creation'),
choice: Match.OneOf('local', 'source', 'detach', 'replace'), fingerprint: String });
if (!/^[a-f0-9]{64}$/.test(resolution.fingerprint)) throw new Meteor.Error('invalid-sync-conflict');
const list = await Lists.findOneAsync(listId);
if (!list) throw new Meteor.Error('list-not-found', 'List not found.');
await assertConflictAccess(this.userId, list.boardId);
return syncOneList(list, { resolution, assertConflictAccess: () => assertConflictAccess(this.userId, list.boardId) });
},
});
DDPRateLimiter.addRule({ type: 'method', name: 'listSyncRunReports',
connectionId: () => true }, 10, 10000);
DDPRateLimiter.addRule({ type: 'method', name: 'syncRecoveryReport',
connectionId: () => true }, 10, 10000);