Skip to content

Commit

Permalink
Resolve attachments folder before using it
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny-signal committed Apr 27, 2022
1 parent 5010090 commit b7f0ec6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
45 changes: 29 additions & 16 deletions app/protocol_filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ import type {

import { isAbsolute, normalize } from 'path';
import { existsSync, realpathSync } from 'fs';
import {
getAvatarsPath,
getBadgesPath,
getDraftPath,
getPath,
getStickersPath,
getTempPath,
getUpdateCachePath,
} from '../ts/util/attachments';

type CallbackType = (response: string | ProtocolResponse) => void;

Expand Down Expand Up @@ -51,6 +60,17 @@ function _createFileHandler({
installPath: string;
isWindows: boolean;
}) {
const allowedRoots = [
userDataPath,
installPath,
getAvatarsPath(userDataPath),
getBadgesPath(userDataPath),
getDraftPath(userDataPath),
getPath(userDataPath),
getStickersPath(userDataPath),
getTempPath(userDataPath),
getUpdateCachePath(userDataPath),
];
return (request: ProtocolRequest, callback: CallbackType): void => {
let targetPath;

Expand Down Expand Up @@ -95,24 +115,17 @@ function _createFileHandler({
return;
}

if (
!properCasing.startsWith(
isWindows ? userDataPath.toLowerCase() : userDataPath
) &&
!properCasing.startsWith(
isWindows ? installPath.toLowerCase() : installPath
)
) {
console.log(
`Warning: denying request to path '${realPath}' (userDataPath: '${userDataPath}', installPath: '${installPath}')`
);
callback({ error: -10 });
return;
for (const root of allowedRoots) {
if (properCasing.startsWith(isWindows ? root.toLowerCase() : root)) {
callback({ path: realPath });
return;
}
}

callback({
path: realPath,
});
console.log(
`Warning: denying request to path '${realPath}' (allowedRoots: '${allowedRoots}')`
);
callback({ error: -10 });
};
}

Expand Down
8 changes: 7 additions & 1 deletion ts/util/attachments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ const createPathGetter =
if (!isString(userDataPath)) {
throw new TypeError("'userDataPath' must be a string");
}
return join(userDataPath, subpath);

const maybeSymlink = join(userDataPath, subpath);
if (fse.pathExistsSync(maybeSymlink)) {
return fse.realpathSync(maybeSymlink);
}

return maybeSymlink;
};

export const getAvatarsPath = createPathGetter(AVATAR_PATH);
Expand Down

0 comments on commit b7f0ec6

Please sign in to comment.