mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
Their exports name attachments without carrying the files, so the files were reported as a loss. Each service documents where a file is downloaded, behind the same sign-in as its API: - Jira: fields.attachment[].content (/rest/api/3/attachment/content/<id>), Basic email:API token; Jira's redirect to its media store is followed without the sign-in. - Deck: /index.php/apps/deck/api/v1.0|v1.1/boards/../stacks/../cards/.. /attachments/<id>, Basic user:app password, OCS-APIRequest: true. - OpenProject: _links.downloadLocation, relative to the instance, Basic apikey:<key>. - Asana: GET /attachments/<gid>?opt_fields=download_url with a personal access token, then the signed download_url without it; a file the export embeds (export_boards.pl) is taken as it is; one kept on Google Drive and the like is a link, reported. - GitLab: a private project's /uploads/ link through /api/v4/projects/<id>/uploads/<secret>/<file> (17.4+), Bearer token. The import page asks for the address, user and token under "Attachment files" for these five (the REST import takes the same as attachmentAuth). They go with that one import: held beside the creator in a WeakMap, never stored or logged, the token field emptied on Import, and sent ONLY to the origin the member typed - an export names any URL it likes. Every download is the import's SSRF-guarded fetch, which validates each hop and drops Authorization on a cross-origin redirect. A file that cannot be fetched is on the loss report with its reason. Unticking Attachments leaves them out (Jira's per-issue field is pruned too). Zenkit is left out: it publishes no way to download a file. Tests: importAttachmentAuth.test.cjs (each service's request, the sign-in only to its origin, malformed sign-ins, never stored or logged), the parser tests pin the attachment lists, a Meteor test imports an embedded Asana file onto its card and shows a Jira file is reported without a sign-in and that the sign-in never reaches an address the guard refuses, and import-attachment-auth.e2e.js the page (not run here). Not run against the live services. Thanks to xet7 !
119 lines
5.1 KiB
JavaScript
119 lines
5.1 KiB
JavaScript
/**
|
|
* models/lib/importAttachmentDownload.js — downloading an attachment that an
|
|
* IMPORTED BOARD FILE named, safely.
|
|
*
|
|
* An imported board (a Trello export, a WeKan export) can give an attachment a
|
|
* `url` instead of inline bytes, and whoever wrote that file chose the URL. The
|
|
* response is stored as an attachment and can be read back through WeKan, so an
|
|
* unguarded download here is a non-blind SSRF — the issue fixed as
|
|
* CVE-2026-30844, and again as LiveBleed for the live Trello import.
|
|
*
|
|
* Validating the URL is not enough on its own. The importers called
|
|
* `Attachments.loadAsync(url)`, and Meteor-Files downloads with the platform
|
|
* `fetch()`, which FOLLOWS REDIRECTS: a public URL that passes validation can
|
|
* answer `302 Location: http://127.0.0.1:.../secret` and the loopback body is
|
|
* what gets stored. That is FollowBleed (GHSA-j9p2-jm73-p549), reported against
|
|
* the live import; the offline importers reached the same end through
|
|
* Meteor-Files.
|
|
*
|
|
* So the download happens here instead, through fetchSafe: the URL is validated
|
|
* at input time, then EVERY hop — the original and each redirect — is validated
|
|
* and DNS-pinned before a packet is sent to it. The bytes come back to the
|
|
* caller, which stores them with the same writeAsync() it already uses for an
|
|
* attachment that arrived inline.
|
|
*/
|
|
|
|
import { validateAttachmentUrl } from './attachmentUrlValidation';
|
|
|
|
// Trello and S3 style download URLs legitimately redirect once or twice (to a
|
|
// signed URL, to a CDN). Each hop is validated, so following a few is safe;
|
|
// the limit only stops a redirect loop.
|
|
const MAX_IMPORT_REDIRECTS = 5;
|
|
|
|
/**
|
|
* Fetch an attachment URL named by an imported board.
|
|
*
|
|
* @param {string} url
|
|
* @param {{headers?: Object}} [options] the service's sign-in, for a file
|
|
* behind it (models/lib/importAttachmentAuth.js); fetchSafe drops it on a
|
|
* redirect to another origin
|
|
* @returns {Promise<{blocked: true, reason: string} | {buffer: Buffer, type: string|null}>}
|
|
* `blocked` when the URL — or a redirect it answered with — is not allowed to
|
|
* be contacted; the caller skips that one attachment. Throws on an ordinary
|
|
* download failure (HTTP error, network), which callers already treat as
|
|
* "skip this attachment".
|
|
*/
|
|
export async function fetchImportedAttachment(url, { headers } = {}) {
|
|
const validation = await validateAttachmentUrl(url);
|
|
if (!validation.valid) {
|
|
return { blocked: true, reason: validation.reason };
|
|
}
|
|
|
|
// Server-only, and required lazily so that importing this module from the
|
|
// isomorphic models/ tree does not pull the guard into the client bundle.
|
|
const { fetchSafe } = require('/server/lib/ssrfGuard');
|
|
|
|
let res;
|
|
try {
|
|
res = await fetchSafe(url, { maxRedirects: MAX_IMPORT_REDIRECTS, ...(headers ? { headers } : {}) });
|
|
} catch (error) {
|
|
const message = (error && error.message) || 'download failed';
|
|
if (/^SSRF_GUARD:/.test(message)) {
|
|
return { blocked: true, reason: message };
|
|
}
|
|
throw error;
|
|
}
|
|
|
|
if (!res.ok) {
|
|
throw new Error(`HTTP ${res.status}`);
|
|
}
|
|
|
|
const type =
|
|
((res.headers && res.headers.get('content-type')) || '').split(';')[0].trim() || null;
|
|
return { buffer: Buffer.from(await res.arrayBuffer()), type };
|
|
}
|
|
|
|
/**
|
|
* Download one attachment of a Jira, Deck, OpenProject, Asana or GitLab
|
|
* import, with the import's sign-in where the service needs it
|
|
* (models/lib/importAttachmentAuth.js decides what is asked of whom).
|
|
*
|
|
* @returns {Promise<{buffer: Buffer, type: string|null} | {lost: string}>}
|
|
*/
|
|
export async function downloadImportAttachment(source, attachment, auth) {
|
|
// Embedded in the export as base64: nothing to fetch.
|
|
if (attachment && typeof attachment.file === 'string' && attachment.file) {
|
|
const buffer = Buffer.from(attachment.file, 'base64');
|
|
return buffer.length ? { buffer, type: null } : { lost: 'empty file in the export' };
|
|
}
|
|
const { attachmentRequest } = require('./importAttachmentAuth');
|
|
const request = attachmentRequest(source, attachment, auth);
|
|
if (request.lost) return { lost: request.lost };
|
|
let answer;
|
|
try {
|
|
answer = await fetchImportedAttachment(request.url, { headers: request.headers });
|
|
} catch (error) {
|
|
// The status only: an error never carries the address or the sign-in.
|
|
return { lost: `download failed (${(error && error.message) || 'error'})` };
|
|
}
|
|
if (answer.blocked) return { lost: `not downloaded (${answer.reason})` };
|
|
if (request.then !== 'asana') return answer;
|
|
// Asana: the attachment record names a signed, short-lived address.
|
|
let downloadUrl = null;
|
|
try {
|
|
const record = JSON.parse(answer.buffer.toString('utf8'));
|
|
downloadUrl = record && record.data && record.data.download_url;
|
|
} catch (e) {
|
|
downloadUrl = null;
|
|
}
|
|
if (typeof downloadUrl !== 'string' || !downloadUrl) {
|
|
return { lost: 'not downloaded: Asana gives no download address for it (a file hosted elsewhere)' };
|
|
}
|
|
try {
|
|
const file = await fetchImportedAttachment(downloadUrl);
|
|
return file.blocked ? { lost: `not downloaded (${file.reason})` } : file;
|
|
} catch (error) {
|
|
return { lost: `download failed (${(error && error.message) || 'error'})` };
|
|
}
|
|
}
|