Skip to content

Commit

Permalink
#5839 Skip “message/global” noname attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
sosnovsky committed Sep 19, 2024
1 parent f20e37d commit a4964c3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 32 deletions.
53 changes: 26 additions & 27 deletions extension/js/common/api/email-provider/gmail/gmail-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,43 +154,42 @@ export class GmailParser {
internalResults: Attachment[] = [],
{ pgpEncryptedIndex }: { pgpEncryptedIndex?: number } = {}
) => {
if (msgOrPayloadOrPart.hasOwnProperty('payload')) {
internalMsgId = (msgOrPayloadOrPart as GmailRes.GmailMsg).id;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
GmailParser.findAttachments((msgOrPayloadOrPart as GmailRes.GmailMsg).payload!, internalMsgId, internalResults);
if ('payload' in msgOrPayloadOrPart && msgOrPayloadOrPart.payload) {
internalMsgId = msgOrPayloadOrPart.id;
GmailParser.findAttachments(msgOrPayloadOrPart.payload, internalMsgId, internalResults);
}
if (msgOrPayloadOrPart.hasOwnProperty('parts')) {
const payload = msgOrPayloadOrPart as GmailRes.GmailMsg$payload;
const contentType = payload.headers?.find(x => x.name.toLowerCase() === 'content-type');
const parts = payload.parts!; // eslint-disable-line @typescript-eslint/no-non-null-assertion
// are we dealing with a PGP/MIME encrypted message?
const pgpEncrypted = Boolean(
parts.length === 2 &&
contentType?.value?.startsWith('multipart/encrypted') &&
(contentType.value.includes('protocol="application/pgp-encrypted"') || parts[0].mimeType === 'application/pgp-encrypted')
);
for (const [i, part] of parts.entries()) {
GmailParser.findAttachments(part, internalMsgId, internalResults, {
pgpEncryptedIndex: pgpEncrypted ? i : undefined,
});
if ('parts' in msgOrPayloadOrPart) {
const contentType = msgOrPayloadOrPart.headers?.find(x => x.name.toLowerCase() === 'content-type');
if (contentType && !contentType.value.startsWith('multipart/related;')) {
const parts = msgOrPayloadOrPart.parts ?? [];
// are we dealing with a PGP/MIME encrypted message?
const pgpEncrypted = Boolean(
parts.length === 2 &&
contentType.value.startsWith('multipart/encrypted') &&
(contentType.value.includes('protocol="application/pgp-encrypted"') || parts[0].mimeType === 'application/pgp-encrypted')
);
for (const [i, part] of parts.entries()) {
GmailParser.findAttachments(part, internalMsgId, internalResults, {
pgpEncryptedIndex: pgpEncrypted ? i : undefined,
});
}
}
}
/* eslint-disable @typescript-eslint/no-non-null-assertion */
if (msgOrPayloadOrPart.hasOwnProperty('body') && (msgOrPayloadOrPart as GmailRes.GmailMsg$payload$part).body!.hasOwnProperty('attachmentId')) {
const payload = msgOrPayloadOrPart as GmailRes.GmailMsg$payload;
if ('body' in msgOrPayloadOrPart && msgOrPayloadOrPart.body?.hasOwnProperty('attachmentId')) {
const payload = msgOrPayloadOrPart as GmailRes.GmailMsg$payload$part;
const treatAs = Attachment.treatAsForPgpEncryptedAttachments(payload.mimeType, pgpEncryptedIndex);
const inline = (GmailParser.findHeader(payload, 'content-disposition') || '').toLowerCase().startsWith('inline');
internalResults.push(
new Attachment({
msgId: internalMsgId,
id: (msgOrPayloadOrPart as GmailRes.GmailMsg$payload$part).body!.attachmentId,
length: (msgOrPayloadOrPart as GmailRes.GmailMsg$payload$part).body!.size,
name: (msgOrPayloadOrPart as GmailRes.GmailMsg$payload$part).filename,
type: (msgOrPayloadOrPart as GmailRes.GmailMsg$payload$part).mimeType,
id: payload.body?.attachmentId ?? '',
length: payload.body?.size ?? 0,
name: payload.filename,
type: payload.mimeType,
treatAs,
inline: (GmailParser.findHeader(msgOrPayloadOrPart, 'content-disposition') || '').toLowerCase().startsWith('inline'),
inline,
})
);
/* eslint-enable @typescript-eslint/no-non-null-assertion */
}
return internalResults;
};
Expand Down
8 changes: 4 additions & 4 deletions extension/js/common/core/attachment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export class Attachment {
throw new Error('Attachment has no data set');
};

public isAttachmentAnImage = () => {
public isImage = () => {
return this.type.startsWith('image/') || this.type.startsWith('img/');
};

Expand All @@ -204,9 +204,9 @@ export class Attachment {
}
}
return 'signature';
} else if (this.inline && this.isAttachmentAnImage()) {
} else if (this.inline && this.isImage()) {
return 'inlineImage';
} else if (!this.name && !this.isAttachmentAnImage() && this.type !== 'application/octet-stream' && this.type !== 'multipart/mixed') {
} else if (!this.name && !this.isImage() && !['application/octet-stream', 'multipart/mixed', 'message/global'].includes(this.type)) {
// this.name may be '' or undefined - catch either
return this.length < 100 ? 'hidden' : 'encryptedMsg';
} else if (this.name === 'msg.asc' && this.length < 100 && this.type === 'application/pgp-encrypted') {
Expand All @@ -228,7 +228,7 @@ export class Attachment {
// && !Attachment.encryptedMsgNames.includes(this.name) -- already checked above
const isAmbiguousAscFile = this.name.endsWith('.asc'); // ambiguous .asc name
const isAmbiguousNonameFile = !this.name || this.name === 'noname'; // may not even be OpenPGP related
if (!this.inline && this.length < 100000 && (isAmbiguousAscFile || isAmbiguousNonameFile) && !this.isAttachmentAnImage()) {
if (!this.inline && this.length < 100000 && (isAmbiguousAscFile || isAmbiguousNonameFile) && !this.isImage()) {
if (isAmbiguousNonameFile && this.type === 'application/octet-stream') {
return 'plainFile';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ export class GmailElementReplacer extends WebmailElementReplacer {
settingsBtnContainer: 'div.aeH > div > .fY',
standardComposeRecipient: 'div.az9 span[email][data-hovercard-id]',
numberOfAttachments: '.aVW',
numberOfAttachmentsDigit: '.aVW span:first-child',
attachmentsButtons: '.aZi',
draftsList: '.ae4',
};
Expand Down

0 comments on commit a4964c3

Please sign in to comment.