Skip to content

Commit 75605dc

Browse files
committed
Fix bad idea
1 parent 5c8f1fd commit 75605dc

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ To expose this to the public, you must use a reverse proxy, and should set up
7474
caching and some kind of rate limiting to prevent abuse. You can set the
7575
`media_proxy_url` keyword argument to the public proxy URL.
7676

77-
A HMAC is created based on the API token and URL to prevent using the proxy to
78-
fetch arbitrary attachment URLs.
77+
A HMAC is created based on a random key and URL to prevent using the proxy to
78+
fetch arbitrary attachment URLs. To make this value consistent across restarts,
79+
pass a bytes value to the `media_proxy_key` keyword argument.
7980

8081
## Installation
8182

miniirc_matrix.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
1010
from typing import Any, Optional, TypeVar, overload
1111
from urllib.parse import quote as _url_quote, urlparse as _urlparse
12-
import functools, hmac, html.parser, itertools, json, math, re, time, uuid
12+
import functools, hmac, html.parser, itertools, json, math, os, re, time, uuid
1313
import miniirc, requests, threading, traceback # type: ignore
1414

1515

16-
ver = (0, 0, 12)
16+
ver = (0, 0, 13)
1717
__version__ = '.'.join(map(str, ver))
1818

1919

@@ -440,6 +440,7 @@ def __init__(
440440
token: Optional[str] = None,
441441
media_proxy_port: Optional[int] = None,
442442
media_proxy_url: Optional[str] = None,
443+
media_proxy_key: Optional[bytes] = None,
443444
**kwargs
444445
) -> None:
445446
# Cache _get_room_url
@@ -463,9 +464,11 @@ def __init__(
463464

464465
self._media_proxy: Optional[ThreadingHTTPServer] = None
465466
self._media_proxy_port = media_proxy_port
466-
if media_proxy_port and not media_proxy_port:
467+
if media_proxy_port and not media_proxy_url:
467468
media_proxy_url = f'http://127.0.0.1:{media_proxy_port}'
468469
self._media_proxy_url = media_proxy_url and media_proxy_url.rstrip('/')
470+
if media_proxy_port is not None:
471+
self._media_proxy_key = media_proxy_key or os.urandom(32)
469472

470473
# Stop miniirc from trying to access the (non-existent) socket
471474
kwargs['ping_interval'] = kwargs['ping_timeout'] = None
@@ -541,11 +544,8 @@ def _get_room_url_no_cache(self, room_id: str) -> str:
541544
return f'rooms/{_url_quote(room_id)}'
542545

543546
def __make_url_digest(self, path: str) -> str:
544-
return hmac.digest(
545-
b'miniirc_matrix hmac v1 ' + self.token.encode('ascii'),
546-
path.encode('ascii'),
547-
'sha256'
548-
).hex()
547+
return hmac.digest(self._media_proxy_key, path.encode('ascii'),
548+
'sha256').hex()
549549

550550
def _download_media(self, url: str) -> requests.Response:
551551
url_base, _, key = url.partition('?key=')

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name='miniirc_matrix',
8-
version='0.0.12',
8+
version='0.0.13',
99
py_modules=['miniirc_matrix'],
1010
author='luk3yx',
1111
description='A Matrix wrapper for miniirc.',

0 commit comments

Comments
 (0)