Skip to content

Commit f533048

Browse files
committed
add middlewares on each route due to params visibility
1 parent 3c66363 commit f533048

File tree

6 files changed

+17
-7
lines changed

6 files changed

+17
-7
lines changed

ee/packages/federation-matrix/src/api/_matrix/invite.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,10 @@ async function startJoiningRoom(...opts: Parameters<typeof joinRoom>) {
297297
export const getMatrixInviteRoutes = (services: HomeserverServices) => {
298298
const { invite, state, room, federationAuth } = services;
299299

300-
return new Router('/federation').use(canAccessResourceMiddleware(federationAuth, 'room')).put(
300+
return new Router('/federation').put(
301301
'/v2/invite/:roomId/:eventId',
302302
{
303+
use: canAccessResourceMiddleware(federationAuth, 'room'),
303304
body: ajv.compile({ type: 'object' }), // TODO: add schema from room package.
304305
params: isProcessInviteParamsProps,
305306
response: {

ee/packages/federation-matrix/src/api/_matrix/media.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ async function getMediaFile(mediaId: string, serverName: string): Promise<{ file
7676
export const getMatrixMediaRoutes = (homeserverServices: HomeserverServices) => {
7777
const { config, federationAuth } = homeserverServices;
7878
return new Router('/federation')
79-
.use(canAccessResourceMiddleware(federationAuth, 'media'))
8079
.get(
8180
'/v1/media/download/:mediaId',
8281
{
82+
use: canAccessResourceMiddleware(federationAuth, 'media'),
8383
params: isMediaDownloadParamsProps,
8484
response: {
8585
200: isBufferResponseProps,
@@ -132,6 +132,7 @@ export const getMatrixMediaRoutes = (homeserverServices: HomeserverServices) =>
132132
.get(
133133
'/v1/media/thumbnail/:mediaId',
134134
{
135+
use: canAccessResourceMiddleware(federationAuth, 'media'),
135136
params: isMediaDownloadParamsProps,
136137
response: {
137138
404: isErrorResponseProps,

ee/packages/federation-matrix/src/api/_matrix/profiles.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,10 +420,10 @@ export const getMatrixProfilesRoutes = (services: HomeserverServices) => {
420420
};
421421
},
422422
)
423-
.use(canAccessResourceMiddleware(federationAuth, 'room'))
424423
.get(
425424
'/v1/make_join/:roomId/:userId',
426425
{
426+
use: canAccessResourceMiddleware(federationAuth, 'room'),
427427
params: isMakeJoinParamsProps,
428428
query: isMakeJoinQueryProps,
429429
response: {
@@ -451,6 +451,7 @@ export const getMatrixProfilesRoutes = (services: HomeserverServices) => {
451451
.post(
452452
'/v1/get_missing_events/:roomId',
453453
{
454+
use: canAccessResourceMiddleware(federationAuth, 'room'),
454455
params: isGetMissingEventsParamsProps,
455456
body: isGetMissingEventsBodyProps,
456457
response: {
@@ -474,6 +475,7 @@ export const getMatrixProfilesRoutes = (services: HomeserverServices) => {
474475
.get(
475476
'/v1/event_auth/:roomId/:eventId',
476477
{
478+
use: canAccessResourceMiddleware(federationAuth, 'room'),
477479
params: isEventAuthParamsProps,
478480
response: {
479481
200: isEventAuthResponseProps,

ee/packages/federation-matrix/src/api/_matrix/send-join.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,10 @@ const isSendJoinResponseProps = ajv.compile(SendJoinResponseSchema);
226226
export const getMatrixSendJoinRoutes = (services: HomeserverServices) => {
227227
const { sendJoin, federationAuth } = services;
228228

229-
return new Router('/federation').use(canAccessResourceMiddleware(federationAuth, 'room')).put(
229+
return new Router('/federation').put(
230230
'/v2/send_join/:roomId/:stateKey',
231231
{
232+
use: canAccessResourceMiddleware(federationAuth, 'room'),
232233
params: isSendJoinParamsProps,
233234
body: isSendJoinEventProps,
234235
response: {

ee/packages/federation-matrix/src/api/_matrix/transactions.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,10 @@ export const getMatrixTransactionsRoutes = (services: HomeserverServices) => {
306306
)
307307

308308
// GET /_matrix/federation/v1/state_ids/{roomId}
309-
.use(canAccessResourceMiddleware(federationAuth, 'room'))
310309
.get(
311310
'/v1/state_ids/:roomId',
312311
{
312+
use: canAccessResourceMiddleware(federationAuth, 'room'),
313313
params: isGetStateIdsParamsProps,
314314
response: {
315315
200: isGetStateIdsResponseProps,
@@ -340,6 +340,7 @@ export const getMatrixTransactionsRoutes = (services: HomeserverServices) => {
340340
.get(
341341
'/v1/state/:roomId',
342342
{
343+
use: canAccessResourceMiddleware(federationAuth, 'room'),
343344
params: isGetStateParamsProps,
344345
response: {
345346
200: isGetStateResponseProps,
@@ -366,10 +367,10 @@ export const getMatrixTransactionsRoutes = (services: HomeserverServices) => {
366367
},
367368
)
368369
// GET /_matrix/federation/v1/event/{eventId}
369-
.use(canAccessResourceMiddleware(federationAuth, 'event'))
370370
.get(
371371
'/v1/event/:eventId',
372372
{
373+
use: canAccessResourceMiddleware(federationAuth, 'event'),
373374
params: isGetEventParamsProps,
374375
response: {
375376
200: isGetEventResponseProps,

ee/packages/federation-matrix/src/api/middlewares/canAccessResource.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,13 @@ const canAccessResource = (federationAuth: EventAuthorizationService, entityType
4444
return c.json({ errcode: 'M_UNAUTHORIZED', error: 'Missing Authorization header' }, 401);
4545
}
4646

47+
const mediaId = c.req.param('mediaId');
48+
const eventId = c.req.param('eventId');
49+
const roomId = c.req.param('roomId');
50+
4751
const resourceAccess = await federationAuth.canAccessResource(
4852
entityType,
49-
extractEntityId(c.req.param(), entityType),
53+
extractEntityId({ mediaId, eventId, roomId }, entityType),
5054
authenticatedServer,
5155
);
5256
if (!resourceAccess) {

0 commit comments

Comments
 (0)