Files
wekan/server/lib/apiImportRequest.js
Lauri Ojansivu 46e30f01e4 Import: Planka, Microsoft To Do, KanbanFlow, Basecamp, Google Tasks and Google Keep from their APIs.
Decided on 2026-10-14. On the import page each of the six asks for what
its service needs - a self-hosted Planka its address, username and
password; the others an access token (Basecamp also its account id) -
lists what that account can import, and fetches the chosen board, list
or project. The credentials go with each call and are used for that
call's requests only; they are never stored or logged, and the page
clears secret fields once a fetch has used them. The fetched JSON fills
the import text box and the import goes on as for a pasted export: the
service's parser (models/lib/<key>ApiFormat.js) into the generalized
importer, with the members step and the loss report. The same JSON can
be pasted, or posted to POST /api/boards/import/<key>.

- server/apiImport.js: apiImportList and apiImportFetch, signed in only,
  off with the Admin Panel's import switch, not in Sandstorm, rate
  limited; credentials checked against the service's own fields
  (models/lib/apiImportServices.js).
- server/lib/apiImportRequest.js: every request through the SSRF guard -
  pinned address, no redirects, bounded in time and size - so a Planka
  address cannot point the server at its own network; an administrator
  can allow such a host with API_IMPORT_ALLOWED_PRIVATE_HOSTS. An error
  names only the origin and the status.
- models/lib/apiImport/<key>.js: each service's client, plain Node with
  the request injected, paging bounded and the token only in a header.
- Each has a docs page under docs/Features/ImportExport/, listed in the
  coverage index and External-Tools.md.

Tests: tests/<key>ApiImport.test.cjs (client with a fake request, every
parsed field, wrong shapes), tests/apiImport.test.cjs (credentials, the
gate, the wiring, the shapes refused), api-import.e2e.js (the form for
every service, fetched JSON imported through the page, refusals incl. a
private address). Live tests, read only, run from build.sh / build.bat ->
Tests -> Live tests with your own accounts (releases/live-tests/README.md);
they were not run here. New strings translated in all locales.

Also: the release risk baseline allows the services' API addresses, and
the guards that count import creators, list the import sources and pin
the platform fetch() calls know the new ones.

Thanks to xet7 !
2026-10-14 22:57:15 +03:00

54 lines
2.8 KiB
JavaScript

// The `request` the API importers' clients (models/lib/apiImport/<key>.js)
// are given on the server: one JSON request through the SSRF guard - the
// address checked and pinned, no redirects, bounded in time and size - so a
// self-hosted address (Planka) cannot point the server at its own network.
// Resolves to the parsed body; a non-2xx answer rejects with `.status` and a
// message that names only the origin and the status, never the answer.
import { fetchSafe } from '/server/lib/ssrfGuard';
const TIMEOUT_MS = 30000;
const MAX_RESPONSE_BYTES = 32 * 1024 * 1024;
// A self-hosted service on the server's own network (a Planka at
// 192.168.1.20) is refused by the guard; only the server's administrator may
// allow its host, as LIST_SYNC_ALLOWED_PRIVATE_HOSTS does for List Sync.
export function operatorAllowedImportHost(url, env = process.env) {
const allowed = String(env.API_IMPORT_ALLOWED_PRIVATE_HOSTS || '').split(',')
.map(host => host.trim().toLowerCase()).filter(Boolean);
return allowed.length > 0 && allowed.includes(new URL(url).hostname.toLowerCase());
}
export async function apiImportRequest(url, { method = 'GET', headers = {}, body } = {}) {
const origin = new URL(url).origin;
const payload = body === undefined ? undefined : JSON.stringify(body);
let response;
try {
if (operatorAllowedImportHost(url)) {
response = await fetch(url, { method, redirect: 'error', signal: AbortSignal.timeout(TIMEOUT_MS),
headers: { Accept: 'application/json', ...headers, ...(payload === undefined ? {} : { 'Content-Type': 'application/json' }) },
...(payload === undefined ? {} : { body: payload }) });
const length = Number(response.headers.get('content-length'));
if (Number.isFinite(length) && length > MAX_RESPONSE_BYTES) throw new Error('too large');
} else response = await fetchSafe(url, {
method,
headers: { Accept: 'application/json', ...headers,
...(payload === undefined ? {} : { 'Content-Type': 'application/json', 'Content-Length': String(Buffer.byteLength(payload)) }) },
...(payload === undefined ? {} : { body: payload }),
timeoutMs: TIMEOUT_MS, totalTimeoutMs: TIMEOUT_MS, maxRedirects: 0, maxResponseBytes: MAX_RESPONSE_BYTES,
});
} catch (error) {
const failed = new Error(/^SSRF_GUARD:/.test(error && error.message) ? `${origin} is not an address WeKan may reach` : `${origin} could not be reached`);
failed.status = 0;
throw failed;
}
if (!response.ok) {
const error = new Error(`${origin} answered HTTP ${response.status}`);
error.status = response.status;
throw error;
}
if (response.status === 204) return null;
try { return await response.json(); } catch (error) { throw new Error(`${origin} did not answer with JSON`); }
}
export default apiImportRequest;