Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow file attachments of any type #6939

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -7328,6 +7328,10 @@
"messageformat": "To change this setting, set “Who can see my number” to “Nobody”.",
"description": "A toast displayed when user clicks disabled option in settings window"
},
"icu:AllowAnyFileType": {
"messageformat": "Allow sending and downloading any file types",
"description": "Title for the allow any file type setting"
},
"icu:WhatsNew__modal-title": {
"messageformat": "What's New",
"description": "Title for the whats new modal"
Expand Down
11 changes: 11 additions & 0 deletions ts/components/Preferences.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export type PropsDataType = {
customColors: Record<string, CustomColorType>;
defaultConversationColor: DefaultConversationColorType;
deviceName?: string;
hasAnyFileTypeAllowed: boolean;
hasAudioNotifications?: boolean;
hasAutoConvertEmoji: boolean;
hasAutoDownloadUpdate: boolean;
Expand Down Expand Up @@ -160,6 +161,7 @@ type PropsFunctionType = {
) => unknown;

// Change handlers
onAnyFileTypeAllowedChange: CheckboxChangeHandlerType;
onAudioNotificationsChange: CheckboxChangeHandlerType;
onAutoConvertEmojiChange: CheckboxChangeHandlerType;
onAutoDownloadUpdateChange: CheckboxChangeHandlerType;
Expand Down Expand Up @@ -273,6 +275,7 @@ export function Preferences({
hasMessageAudio,
hasMinimizeToAndStartInSystemTray,
hasMinimizeToSystemTray,
hasAnyFileTypeAllowed,
hasNotificationAttention,
hasNotifications,
hasReadReceipts,
Expand All @@ -293,6 +296,7 @@ export function Preferences({
lastSyncTime,
makeSyncRequest,
notificationContent,
onAnyFileTypeAllowedChange,
onAudioNotificationsChange,
onAutoConvertEmojiChange,
onAutoDownloadUpdateChange,
Expand Down Expand Up @@ -896,6 +900,13 @@ export function Preferences({
/>
}
/>
<Checkbox
checked={hasAnyFileTypeAllowed}
label={i18n('icu:AllowAnyFileType')}
moduleClassName="Preferences__checkbox"
name="allow-any-file-type"
onChange={onAnyFileTypeAllowedChange}
/>
</SettingsRow>
{isSyncSupported && (
<SettingsRow>
Expand Down
16 changes: 13 additions & 3 deletions ts/components/conversation/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,8 @@ type State = {
showOutgoingGiftBadgeModal: boolean;

hasDeleteForEveryoneTimerExpired: boolean;

isThisFileDangerous: boolean;
};

export class Message extends React.PureComponent<Props, State> {
Expand Down Expand Up @@ -428,6 +430,8 @@ export class Message extends React.PureComponent<Props, State> {

hasDeleteForEveryoneTimerExpired:
this.getTimeRemainingForDeleteForEveryone() <= 0,

isThisFileDangerous: true,
};
}

Expand Down Expand Up @@ -493,7 +497,7 @@ export class Message extends React.PureComponent<Props, State> {
}
};

public override componentDidMount(): void {
public override async componentDidMount(): Promise<void> {
const { conversationId } = this.props;
window.ConversationController?.onConvoMessageMount(conversationId);

Expand Down Expand Up @@ -524,6 +528,13 @@ export class Message extends React.PureComponent<Props, State> {
}

document.addEventListener('selectionchange', this.handleSelectionChange);

const { attachments } = this.props;
if (attachments) {
const { fileName } = attachments[0];
const isDangerous = await isFileDangerous(fileName || '');
this.setState({ isThisFileDangerous: isDangerous });
}
}

public override componentWillUnmount(): void {
Expand Down Expand Up @@ -1058,7 +1069,6 @@ export class Message extends React.PureComponent<Props, State> {
}
const { pending, fileName, fileSize, contentType } = firstAttachment;
const extension = getExtensionForDisplay({ contentType, fileName });
const isDangerous = isFileDangerous(fileName || '');

return (
<button
Expand Down Expand Up @@ -1104,7 +1114,7 @@ export class Message extends React.PureComponent<Props, State> {
</div>
) : null}
</div>
{isDangerous ? (
{this.state.isThisFileDangerous ? (
<div className="module-message__generic-attachment__icon-dangerous-container">
<div className="module-message__generic-attachment__icon-dangerous" />
</div>
Expand Down
6 changes: 6 additions & 0 deletions ts/main/settingsChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,18 @@ export class SettingsChannel extends EventEmitter {

// These ones are different because its single source of truth is userConfig,
// not IndexedDB
ipc.handle('settings:get:allowAnyFileType', () => {
return userConfig.get('allowAnyFileType') || false;
});
ipc.handle('settings:get:mediaPermissions', () => {
return userConfig.get('mediaPermissions') || false;
});
ipc.handle('settings:get:mediaCameraPermissions', () => {
return userConfig.get('mediaCameraPermissions') || false;
});
ipc.handle('settings:set:allowAnyFileType', (_event, value) => {
userConfig.set('allowAnyFileType', value);
});
ipc.handle('settings:set:mediaPermissions', (_event, value) => {
userConfig.set('mediaPermissions', value);

Expand Down
8 changes: 4 additions & 4 deletions ts/state/ducks/composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1039,7 +1039,7 @@ function processAttachments({
const filesToProcess: Array<File> = [];
for (let i = 0; i < files.length; i += 1) {
const file = files[i];
const processingResult = preProcessAttachment(file, nextDraftAttachments);
const processingResult = await preProcessAttachment(file, nextDraftAttachments);
if (processingResult != null) {
toastToShow = processingResult;
} else {
Expand Down Expand Up @@ -1108,15 +1108,15 @@ function processAttachments({
};
}

function preProcessAttachment(
async function preProcessAttachment(
file: File,
draftAttachments: Array<AttachmentDraftType>
): AnyToast | undefined {
): Promise<AnyToast | undefined> {
if (!file) {
return;
}

if (isFileDangerous(file.name)) {
if (await isFileDangerous(file.name)) {
return { toastType: ToastType.DangerousFileType };
}

Expand Down
2 changes: 1 addition & 1 deletion ts/state/ducks/conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3728,7 +3728,7 @@ function saveAttachment(
return async dispatch => {
const { fileName = '' } = attachment;

const isDangerous = isFileDangerous(fileName);
const isDangerous = await isFileDangerous(fileName);

if (isDangerous) {
dispatch({
Expand Down
51 changes: 51 additions & 0 deletions ts/test-electron/util/isFileDangerous_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2018 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only

import { assert } from 'chai';

import { isFileDangerous } from '../../util/isFileDangerous';

describe('isFileDangerous', () => {
it('returns false for images', async () => {
assert.strictEqual(await isFileDangerous('dog.gif'), false);
assert.strictEqual(await isFileDangerous('cat.jpg'), false);
});

it('returns false for documents', async () => {
assert.strictEqual(await isFileDangerous('resume.docx'), false);
assert.strictEqual(await isFileDangerous('price_list.pdf'), false);
});

it('returns true for executable files', async () => {
assert.strictEqual(await isFileDangerous('run.exe'), true);
assert.strictEqual(await isFileDangerous('install.pif'), true);
});

it('returns true for Microsoft settings files', async () => {
assert.strictEqual(await isFileDangerous('downl.SettingContent-ms'), true);
});

it('returns false for non-dangerous files that end in ".", which can happen on Windows', async () => {
assert.strictEqual(await isFileDangerous('dog.png.'), false);
assert.strictEqual(await isFileDangerous('resume.docx.'), false);
});

it('returns true for dangerous files that end in ".", which can happen on Windows', async () => {
assert.strictEqual(await isFileDangerous('run.exe.'), true);
assert.strictEqual(await isFileDangerous('install.pif.'), true);
});

it('returns false for empty filename', async () => {
assert.strictEqual(await isFileDangerous(''), false);
});

it('returns false for exe at various parts of filename', async () => {
assert.strictEqual(await isFileDangerous('.exemanifesto.txt'), false);
assert.strictEqual(await isFileDangerous('runexe'), false);
assert.strictEqual(await isFileDangerous('run_exe'), false);
});

it('returns true for upper-case EXE', async () => {
assert.strictEqual(await isFileDangerous('run.EXE'), true);
});
});
51 changes: 0 additions & 51 deletions ts/test-node/util/isFileDangerous_test.ts

This file was deleted.

10 changes: 9 additions & 1 deletion ts/util/createIPCEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type SentMediaQualityType = 'standard' | 'high';
type NotificationSettingType = 'message' | 'name' | 'count' | 'off';

export type IPCEventsValuesType = {
allowAnyFileType: boolean;
alwaysRelayCalls: boolean | undefined;
audioNotification: boolean | undefined;
audioMessage: boolean;
Expand Down Expand Up @@ -148,6 +149,7 @@ export type IPCEventsCallbacksType = {
type ValuesWithGetters = Omit<
SettingsValuesType,
// Async
| 'allowAnyFileType'
| 'zoomFactor'
| 'localeOverride'
| 'spellCheck'
Expand Down Expand Up @@ -198,6 +200,7 @@ export type IPCEventsGettersType = {
[Key in keyof ValuesWithGetters as IPCEventGetterType<Key>]: () => ValuesWithGetters[Key];
} & {
// Async
getAllowAnyFileType: () => Promise<boolean>;
getZoomFactor: () => Promise<ZoomFactorType>;
getLocaleOverride: () => Promise<string | null>;
getSpellCheck: () => Promise<boolean>;
Expand Down Expand Up @@ -241,6 +244,9 @@ export function createIPCEvents(
};

return {
getAllowAnyFileType: () => {
return ipcRenderer.invoke('settings:get:allowAnyFileType')
},
getDeviceName: () => window.textsecure.storage.user.getDeviceName(),
getPhoneNumber: () => {
try {
Expand All @@ -267,7 +273,9 @@ export function createIPCEvents(
callback(zoomFactor);
});
},

setAllowAnyFileType: () => {
return ipcRenderer.invoke('settings:set:allowAnyFileType')
},
setPhoneNumberDiscoverabilitySetting,
setPhoneNumberSharingSetting: async (newValue: PhoneNumberSharingMode) => {
const account = window.ConversationController.getOurConversationOrThrow();
Expand Down
9 changes: 8 additions & 1 deletion ts/util/isFileDangerous.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
// Copyright 2018 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only

import { ipcRenderer } from 'electron';

const DANGEROUS_FILE_TYPES =
/\.(ADE|ADP|APK|BAT|CAB|CHM|CMD|COM|CPL|DIAGCAB|DLL|DMG|EXE|HTA|INF|INS|ISP|JAR|JS|JSE|LIB|LNK|MDE|MHT|MSC|MSI|MSP|MST|NSH|PIF|PS1|PSC1|PSM1|PSRC|REG|SCR|SCT|SETTINGCONTENT-MS|SHB|SYS|VB|VBE|VBS|VXD|WSC|WSF|WSH)\.?$/i;

export function isFileDangerous(fileName: string): boolean {
export async function isFileDangerous(fileName: string): Promise<boolean> {

const allowAnyFileType = await ipcRenderer.invoke("settings:get:allowAnyFileType");

if (allowAnyFileType) return false;

return DANGEROUS_FILE_TYPES.test(fileName);
}
4 changes: 4 additions & 0 deletions ts/windows/settings/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ SettingsWindowProps.onRender(
hasCallRingtoneNotification,
hasCountMutedConversations,
hasHideMenuBar,
hasAnyFileTypeAllowed,
hasIncomingCallNotifications,
hasLinkPreviews,
hasMediaCameraPermissions,
Expand Down Expand Up @@ -88,6 +89,7 @@ SettingsWindowProps.onRender(
onMediaCameraPermissionsChange,
onMediaPermissionsChange,
onMessageAudioChange,
onAnyFileTypeAllowedChange,
onMinimizeToAndStartInSystemTrayChange,
onMinimizeToSystemTrayChange,
onNotificationAttentionChange,
Expand Down Expand Up @@ -140,6 +142,7 @@ SettingsWindowProps.onRender(
doneRendering={doneRendering}
editCustomColor={editCustomColor}
getConversationsWithCustomColor={getConversationsWithCustomColor}
hasAnyFileTypeAllowed={hasAnyFileTypeAllowed}
hasAudioNotifications={hasAudioNotifications}
hasAutoConvertEmoji={hasAutoConvertEmoji}
hasAutoDownloadUpdate={hasAutoDownloadUpdate}
Expand Down Expand Up @@ -178,6 +181,7 @@ SettingsWindowProps.onRender(
localeOverride={localeOverride}
makeSyncRequest={makeSyncRequest}
notificationContent={notificationContent}
onAnyFileTypeAllowedChange={onAnyFileTypeAllowedChange}
onAudioNotificationsChange={onAudioNotificationsChange}
onAutoConvertEmojiChange={onAutoConvertEmojiChange}
onAutoDownloadUpdateChange={onAutoDownloadUpdateChange}
Expand Down
Loading