Skip to content

Commit a9b03ab

Browse files
fix(react): harden image auth token handling
1 parent 62e3de6 commit a9b03ab

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

packages/react/src/views/AttachmentHandler/ImageAttachment.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,38 @@ const ImageAttachment = ({
1616
}) => {
1717
const { RCInstance } = useContext(RCContext);
1818
const [showGallery, setShowGallery] = useState(false);
19-
const [authParams, setAuthParams] = useState('');
19+
const [authParams, setAuthParams] = useState(null);
2020

2121
useEffect(() => {
2222
let cancelled = false;
2323
RCInstance.auth.getCurrentUser().then((user) => {
24-
if (!cancelled && user?.authToken && user?.userId) {
25-
setAuthParams(`rc_token=${user.authToken}&rc_uid=${user.userId}`);
24+
if (!cancelled) {
25+
setAuthParams(
26+
user?.authToken && user?.userId
27+
? `rc_token=${user.authToken}&rc_uid=${user.userId}`
28+
: ''
29+
);
2630
}
31+
}).catch(() => {
32+
if (!cancelled) setAuthParams('');
2733
});
2834
return () => { cancelled = true; };
2935
}, [RCInstance]);
3036

3137
const withAuth = (url) => {
32-
if (!url || !authParams) return url;
38+
if (!url) return url;
39+
// Only add auth to URLs served from our own RC host — never leak creds to 3rd parties
40+
try {
41+
const rcHostname = new URL(host).hostname;
42+
if (new URL(url).hostname !== rcHostname) return url;
43+
} catch {
44+
return url; // malformed URL — skip auth
45+
}
46+
if (!authParams) return url;
3347
const sep = url.includes('?') ? '&' : '?';
3448
return `${url}${sep}${authParams}`;
3549
};
50+
3651
const getUserAvatarUrl = (icon) => {
3752
const instanceHost = RCInstance.getHost();
3853
const URL = `${instanceHost}${icon}`;

0 commit comments

Comments
 (0)