Files
wekan/models/lib/apiImport/mstodo.js
T
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

88 lines
3.6 KiB
JavaScript

// Microsoft To Do through Microsoft Graph (models/lib/apiImportServices.js):
// the member's lists, and the tasks of one list or of all of them, as the JSON
// models/lib/mstodoApiFormat.js reads. Plain Node; `request` is passed in.
//
// The token is a Microsoft Graph access token with Tasks.Read (Graph Explorer
// shows one). It goes only into the Authorization header of requests to
// graph.microsoft.com.
const GRAPH = 'https://graph.microsoft.com/v1.0';
const GRAPH_HOST = 'graph.microsoft.com';
export const ALL_LISTS = 'all';
export const MAX_MSTODO_PAGES = 1000;
export const MAX_MSTODO_TASKS = 50000;
function headersFor(credentials) {
const token = credentials && typeof credentials.token === 'string' ? credentials.token.trim() : '';
if (!token) throw new Error('token is required');
return { Authorization: `Bearer ${token}`, Accept: 'application/json' };
}
// Graph pages with @odata.nextLink; a next link that leaves Graph would take
// the token with it, so it is refused.
function graphNext(link) {
let url;
try { url = new URL(link); } catch (error) { throw new Error('Microsoft Graph sent a next page link that is not a URL'); }
if (url.protocol !== 'https:' || url.hostname !== GRAPH_HOST || url.username || url.password) {
throw new Error('Microsoft Graph sent a next page link to another host');
}
return url.href;
}
async function getAll(url, headers, request, state, what) {
const items = [];
let next = url;
while (next) {
state.pages += 1;
if (state.pages > MAX_MSTODO_PAGES) throw new Error(`Microsoft To Do has more than ${MAX_MSTODO_PAGES} pages of ${what}`);
const page = await request(next, { method: 'GET', headers });
if (!page || typeof page !== 'object' || !Array.isArray(page.value)) {
throw new Error(`Microsoft Graph did not answer with a list of ${what}`);
}
items.push(...page.value);
if (what === 'tasks' && state.tasks + items.length > MAX_MSTODO_TASKS) {
throw new Error(`Microsoft To Do has more than ${MAX_MSTODO_TASKS} tasks`);
}
next = page['@odata.nextLink'] ? graphNext(page['@odata.nextLink']) : null;
}
return items;
}
async function todoLists(headers, request, state) {
const lists = await getAll(`${GRAPH}/me/todo/lists`, headers, request, state, 'lists');
return lists.filter(list => list && typeof list.id === 'string' && list.id);
}
export async function list(credentials, request) {
const headers = headersFor(credentials);
const lists = await todoLists(headers, request, { pages: 0, tasks: 0 });
return [
{ id: ALL_LISTS, name: 'Microsoft To Do (all lists)' },
...lists.map(item => ({ id: item.id, name: String(item.displayName || item.id) })),
];
}
export async function fetch(credentials, id, request) {
const headers = headersFor(credentials);
if (typeof id !== 'string' || !id) throw new Error('Choose what to import');
const state = { pages: 0, tasks: 0 };
let lists = await todoLists(headers, request, state);
if (id !== ALL_LISTS) {
lists = lists.filter(item => item.id === id);
if (!lists.length) throw new Error('Microsoft To Do has no such list');
}
const out = [];
for (const item of lists) {
const tasks = await getAll(`${GRAPH}/me/todo/lists/${encodeURIComponent(item.id)}/tasks?$expand=checklistItems`,
headers, request, state, 'tasks');
state.tasks += tasks.length;
out.push({
id: item.id,
displayName: typeof item.displayName === 'string' ? item.displayName : '',
wellknownListName: typeof item.wellknownListName === 'string' ? item.wellknownListName : 'none',
tasks,
});
}
return { mstodo: true, lists: out };
}