mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
Following the same upgrade as #6583, profile pictures came back as broken images: "I can see Profile Pics from the user in the Admin/User Pannel if i open a User. But on the boards i see only the 'missing Picture' Icon" "initials are OK, but there is broken avatar image for those that have image" Nothing was lost, and the migration is not at fault. The avatar files migrate, and the Meteor-Files record made from a CollectionFS filerecord even reuses its _id (cfsRecordToMeteorFile), so a 6.x URL still names the right object. What broke is the request for it. A 6.x install stores profile.avatarUrl as /cfs/files/avatars/<id>. That route serves the legacy CollectionFS bytes if they are still there, and otherwise redirects to /cdn/storage/avatars/<id> - which asked who was asking like this: const userId = Meteor.userId(); if (!userId) { res.writeHead(401); ... } Meteor.userId() reads the current DDP invocation's environment. That exists inside a method or a publication and NOT in a WebApp handler, where it does not return "nobody" - it THROWS "Meteor.userId can only be invoked in method calls or publications". The handler wraps its body in a try/catch that answers 500, so the throw was swallowed into a broken image and no avatar served through that route ever reached anybody, on any install. The upgrade did not cause it; it moved every avatar URL onto the route where it already applied. server/routes/legacyAttachments.js had the identical call, so legacy attachment URLs failed the same way and for the same reason. Initials were unaffected because they are rendered client-side from the username and fetch nothing, which is why the report describes them as fine. The other half of the report - the Admin Panel showing a picture that a board did not - is NOT explained by this. Both templates end at the same route: peopleBody.jade puts profile.avatarUrl into an img src, and a board goes through +userAvatar, which appends ?boardId=. That parameter is the difference, and it has a cause of its own; the commit that follows this one deals with it. An HTTP request carries its identity in the request: a bearer token, an X-Auth-Token header, an ?authToken= parameter, or the login cookie - and on Sandstorm, a platform-injected user id and no Meteor token at all. server/routes/universalFileServer.js has always resolved it that way and serves attachments correctly today. server/lib/requestUser.js lifts that resolution out so the two routes that were guessing share it rather than grow a third copy, and so the next file route has something to call. It never throws: a caller deciding whether to serve a file wants an answer, not an exception its own catch will turn back into a 500. universalFileServer keeps its own inline copy for now. Rewiring a working file server is a separate change from fixing a broken one, and this commit should be readable as the fix. tests/requestUserAuth.test.cjs pins that neither route calls Meteor.userId(), that both await the resolver (an unawaited Promise is truthy and would authorise everybody), that all four token carriers and the Sandstorm path are handled, and that the migration still reuses the id the old URL names. Verified by reading and by the source guard; confirming the served image needs an upgraded instance, which this sandbox has no Docker to run. Thanks to markusst1982 and xet7 ! Fixes #6583,
134 lines
4.6 KiB
JavaScript
134 lines
4.6 KiB
JavaScript
// Who is making this plain HTTP request?
|
|
//
|
|
// `Meteor.userId()` CANNOT ANSWER THAT. It reads the current DDP invocation's
|
|
// environment, which exists inside a method or a publication and nowhere else. In
|
|
// a WebApp/connect handler there is no such environment, so it does not return
|
|
// "nobody" - it throws:
|
|
//
|
|
// Meteor.userId can only be invoked in method calls or publications.
|
|
//
|
|
// Two file routes called it anyway, and because both wrap their body in a
|
|
// try/catch that turns any error into a 500, the failure looked like a broken
|
|
// file rather than broken authentication:
|
|
//
|
|
// server/routes/avatarServer.js /cdn/storage/avatars/:fileName
|
|
// server/routes/legacyAttachments.js
|
|
//
|
|
// That is why profile pictures came back as broken images after upgrading from an
|
|
// old MongoDB snap: the stored `profile.avatarUrl` of a 6.x install is
|
|
// `/cfs/files/avatars/<id>`, the legacy route redirects it to
|
|
// `/cdn/storage/avatars/<id>`, and that route could never serve anybody. The
|
|
// avatar files themselves migrated fine - the request for them was answered 500
|
|
// before a byte was read.
|
|
//
|
|
// An HTTP request carries its identity in the request: a bearer token, an
|
|
// X-Auth-Token header, an ?authToken= parameter, or the login cookie. This is
|
|
// the same resolution server/routes/universalFileServer.js has always done -
|
|
// lifted here so the routes that were guessing can share it rather than grow a
|
|
// third copy.
|
|
|
|
import { Meteor } from 'meteor/meteor';
|
|
import { Accounts } from 'meteor/accounts-base';
|
|
|
|
export function parseCookies(req) {
|
|
const header = req && req.headers && req.headers.cookie;
|
|
const out = {};
|
|
if (!header) return out;
|
|
for (const part of header.split(';')) {
|
|
const idx = part.indexOf('=');
|
|
if (idx === -1) continue;
|
|
try {
|
|
out[decodeURIComponent(part.slice(0, idx).trim())] =
|
|
decodeURIComponent(part.slice(idx + 1).trim());
|
|
} catch (_) {
|
|
// A cookie we cannot decode is not a reason to fail the request.
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
export function parseQuery(req) {
|
|
const out = {};
|
|
const q = ((req && req.url) || '').split('?')[1] || '';
|
|
if (!q) return out;
|
|
for (const pair of q.split('&')) {
|
|
if (!pair) continue;
|
|
const [rawK, rawV] = pair.split('=');
|
|
try {
|
|
out[decodeURIComponent((rawK || '').trim())] =
|
|
decodeURIComponent((rawV || '').trim());
|
|
} catch (_) {
|
|
// ditto
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
/** The login token this request carries, or null. Priority order matches
|
|
* universalFileServer's, so a request that works for an attachment works for an
|
|
* avatar. */
|
|
export function extractLoginToken(req) {
|
|
const authz = req && req.headers &&
|
|
(req.headers.authorization || req.headers.Authorization);
|
|
if (authz && typeof authz === 'string') {
|
|
const m = authz.match(/^Bearer\s+(.+)$/i);
|
|
if (m && m[1]) return m[1].trim();
|
|
}
|
|
|
|
const xAuth = req && req.headers &&
|
|
(req.headers['x-auth-token'] || req.headers['X-Auth-Token']);
|
|
if (xAuth && typeof xAuth === 'string') return xAuth.trim();
|
|
|
|
const q = parseQuery(req);
|
|
if (q.authToken && typeof q.authToken === 'string') return q.authToken.trim();
|
|
|
|
const cookies = parseCookies(req);
|
|
if (cookies.meteor_login_token) return cookies.meteor_login_token.trim();
|
|
if (cookies.wekan_login_token) return cookies.wekan_login_token.trim();
|
|
|
|
return null;
|
|
}
|
|
|
|
/** On Sandstorm the platform has already authorized the request and injects the
|
|
* user id as a header; there is no Meteor login token in that model at all. */
|
|
export function isSandstormRequest(req) {
|
|
return !!(
|
|
Meteor.settings &&
|
|
Meteor.settings.public &&
|
|
Meteor.settings.public.sandstorm &&
|
|
req && req.headers && req.headers['x-sandstorm-user-id']
|
|
);
|
|
}
|
|
|
|
/** The user id behind this request, or null. Never throws: a caller deciding
|
|
* whether to serve a file wants an answer, not an exception that its own
|
|
* try/catch will turn into a 500. */
|
|
export async function getUserIdFromRequest(req) {
|
|
try {
|
|
if (isSandstormRequest(req)) {
|
|
return req.headers['x-sandstorm-user-id'];
|
|
}
|
|
const raw = extractLoginToken(req);
|
|
if (!raw || typeof raw !== 'string' || raw.length < 10) return null;
|
|
const hashed = Accounts._hashLoginToken(raw);
|
|
const user = await Meteor.users.findOneAsync(
|
|
{ 'services.resume.loginTokens.hashedToken': hashed },
|
|
{ fields: { _id: 1 } },
|
|
);
|
|
return user ? user._id : null;
|
|
} catch (error) {
|
|
if (process.env.DEBUG === 'true') {
|
|
console.warn('requestUser: could not resolve the request user:', error);
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export default {
|
|
parseCookies,
|
|
parseQuery,
|
|
extractLoginToken,
|
|
isSandstormRequest,
|
|
getUserIdFromRequest,
|
|
};
|