New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

matrix-mxc-proxy

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

matrix-mxc-proxy

Convert Matrix MXC URIs to external URLs to allow access to files

  • 1.0.5
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

Matrix MXC proxy

To bridge another platform to Matrix, you must deal with attachments uploaded in that platform. The usual approach is to request the file and pipe the response to your Matrix homeserver's upload endpoint. Now, if you have quite some resources for your homeserver, then that's not really a problem. But what if:

  • You don't want to have the files in your homeserver.
    • Note that a homeserver will only request a remote file if a user explicitly asks for it, when eg. a client needs to render the attachment, etc.
  • You don't want to record the bindings between remote attachments and local MXC URIs.
  • You want to reduce exposure to the files.
    • In most situations (although not mandated by the spec), a remote homeserver will allow you to redirect the media request to somewhere else.
  • You want to have some sort of data forgetfulness.
    • Matrix does not delete media attached to a deleted message (at least for now, if MSC3911 will ever be implemented), but many other platforms do. Given that large homeservers likely purge remote media cache regularly, a deleted media will likely be forgotten for large parts of Matrix gradually.

This library creates a media proxy that converts MXC URIs to URLs, so that the homeservers can obtain the file directly from the URL.

WARNING

I need not to explain how an unsecured arbitrary proxy is a bad idea, so the scope of the proxy should be sufficiently restrictive!!!

Usage

import mxcProxy from 'matrix-mxc-proxy';

const port = 8080; // port to listen on
const domain = 'myproxy.example.com'; // name of the "homeserver"
const getUrl = (id: string) => {
    // eg. if a MXC URI is "mxc://myproxy.example.com/abcdefg"
    // then the id is "abcdefg"
    return `https://cdn.example.org/${id}`; // where the file is
    // return null if an URL cannot be determined
};

mxcProxy(port, domain, getUrl);

A reverse proxy for the domain with TLS should forward to the port.

API

There are two endpoints:

GET /.well-known/matrix/server

Returns the domain, plus :443. See Matrix spec.

GET /_matrix/media/v3/download/{serverName}/{mediaId}

See Matrix spec. Regarding this implementation:

  • The serverName MUST be the same domain as where the proxy is reachable at. Otherwise, error code ME.AUSTINHUANG.MXC_PROXY.NOT_SUPPORTED will be thrown with status code 403.
  • Given the specified getUrl function, if getUrl(mediaId) returns null, error code ME.AUSTINHUANG.MXC_PROXY.CANNOT_CONVERT will be thrown with status code 400.
  • The allow_redirect query parameter is respected. If true, a 308 redirect to the output of getUrl(mediaId) will be given. Otherwise, the file will be proxied which, if failed, will throw error code ME.AUSTINHUANG.MXC_PROXY.PROXY_ERROR with status code 504.
  • The allow_remote query parameter is ignored; the server acts as if it is always false, given its purpose, as well as expected homeserver behaviour.
  • The timeout_ms query parameter is respected as specified.

Keywords

FAQs

Package last updated on 12 Jun 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc