Problem/Motivation

This issue covers the case where the module/code that builds the email does not support attachments. The site owner can use this adjuster to scan the email body to attach files or embed images.

Proposed resolution

Rough starting point see code in this comment.

Remaining tasks

Needs changes to apply the following comments:

  1. Follow the standard method of __construct() and create()
  2. The cid doesn't need to be universally unique, just unique within this email. Probably we should get the Email object to handle assignment of a unique cid or else every caller would need to duplicate the same code.
  3. Call to realpath() is discouraged and only works for local filesystem. Probably it's unnecessary as embedFromPath() can handle it??
  4. Maybe add support for attaching files?
  5. Maybe add settings to control embedding - e.g. only local files or a max size.

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#43 3284140.patch7.25 KBconnbi
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

AdamPS created an issue. See original summary.

pstewart made their first commit to this issue’s fork.

pstewart’s picture

Status: Active » Needs review

I've done an initial implementation using the general approach from 3261807#20, but adapted to be closer to the way the SwiftMailer modules handles inline images:

* The inlining only applies to URIs starting with image:
* Paths are resolved in the same way as in SwiftMailer, i.e. ny leading slash is stripped, if the path parses as a URI then is passed through unchanged, otherwise the path is passed through realpath
* Responsibility for generating cids lies with the adjuster, as the interface for embedFromPath allows the cid to be specified, so adjuster and builder implementations should be free to implement in whatever way is appropriate for their use case, rather than cids being dictated by the Email class. I've therefore kept the UUID CID generation in the adjuster (maybe this could be spun out to a generateCid method in BaseEmailTrait to provide a general facility for implementations that don't want to manage CIDs themselves)
* The DOMDocument technique from the original comment does not cope with HTML5, so has been replaced by a preg_match_all + str_replace implementation derived from the SwiftMailer module's technique. This has the side-benefit of catching any double-quote delmited image: URI attribute, so should also work for responsive image srcsets etc.

I've tested this by adding the new adjuster to my default policy, and verified emails being rendered with a email.html.twig converted from a swiftmailer.html.twig with image: URIs behave as expected.

adamps’s picture

Status: Needs review » Needs work

Great many thanks for the patch. Here are some comments

  1. preg_match_all() is not the best way to parse HTML - please use $dom->getElementsByTagName('img') as in the patch you started from. Then use $img->setAttribute() instead of str_replace. Get rid of $processed_images variable.
  2. (=IS item 2) The cid doesn't need to be universally unique, just unique within this email. Probably we should get the Email object to handle assignment of a unique cid or else every caller would need to duplicate the same code. EDIT So the interface is embedFromPath($path) returning a CID. The function supports passing the same $path and returns the same CID. The CID counts up numerically it doesn't need to be a UUID.
  3. (=IS item 3) Call to realpath() is discouraged and only works for local filesystem. Probably it's unnecessary as embedFromPath() can handle it??
pstewart’s picture

Hi Adam,

Just want to make sure on a few things before I have another go at this patch:

1. While preg_match_all() is not ideal, the DOM Document technique has a deal breaker in that it quite definitely does not support HTML5, and none of the workarounds look to be viable (see https://stackoverflow.com/questions/10712503/how-to-make-html5-work-with... ). Perhaps there's a way we can interact with the render array before we render, but that might still present difficulties when embeds are being done directly in twig templates?

2. Are you recommending we change the interface so that embedFromPath returns the CID rather than this? I was working on the assumption that we were following the underlying API as closely as possible, so embedFromPath would return this to support method chaining. I see what you mean about UUIDs though, I'll refactor this to use a simple counter.

3. The realpath() usage here is only as a fallback for if the matched path doesn't parse as a URL (i.e. can definitely be handled by known stream wrappers). If the path can't be handled by known stream wrappers then it can only relate to the the filesystem, so realpath() is appropriate to use inside the if, as ultimately we're going to be passing off to Symfony directly after. However if we're confident we'll never get there then we could just drop the if statement entirely.

adamps’s picture

Thanks for the follow up.

1. Core uses \DOMDocument in 4 places including Html::transformRootRelativeUrlsToAbsolute This module uses CssToInlineStyles which uses \DOMDocument. So for the moment it seems to be most consistent with what we already do. preg_match_all() could be fooled by comments, whitespace, etc, so that's no good.

2. Yes that was my idea to change the interface - we have done this in other places hence BaseEmailTrait with a protected inner \Symfony\Component\Mime\Email object. You are right we would lose chaining however I can't see a better way. We can't safely put a counter in InlineImagesEmailAdjuster because the CID might clash with another class that also inserts inline images. However the Email class can use a simple counter because it can see all the CIDs and images, and it can even return the same CID if the same image is used to avoid transporting the embedded image twice.

3. I think we can drop both if statements and require a URL scheme. Mailing of local files without using stream wrappers seems like something we don't want to encourage for security as it makes it hard to prevent emailing of a sensitive/private image. When we later write security checking (I thought I raised an issue but I can't see it so maybe I didn't) it could be based on checking the URL scheme. Of course people can still use file: in their email, however we can write a policy to block it.

adamps’s picture

Interesting, /vendor/symfony/mime/Email.php uses preg_match_all(). I'm still not sure if it's robust in all unusual cases, and it feels safer to copy what Drupal does.

In existing attachFromPath(), I think the $name parameter seems to default to none. What we can say is that if there is a name, it must be unique among all attachments. Please can you fix to something like this?
* (optional) The file name, which if set must be unique among all attachments.

One way to avoid changing the interface of embedFromPath() is to add a new method getUniqueName(string $path). Downside is that it makes the calling code more complex adding a line of code.

Probably I still prefer to change the interface:
* (optional) The file name, which must be unique among all attachments. Leave blank to generate a unique name.
and
* @return string The file name.

metallized made their first commit to this issue’s fork.

metallized’s picture

Status: Needs work » Needs review

Hey i did rebase of this MR and did try to fix the comments made by @AdamPS.

I did't implement the CID counter and instead i relied on the getContentId() method from DataPart

adamps’s picture

Status: Needs review » Needs work

Great thanks. The tests are failing with a syntax error so this needs work.

metallized’s picture

Status: Needs work » Needs review

Fixed linter issues.

adamps’s picture

Status: Needs review » Needs work

Thanks @metallized, that's some more good ideas.

It's a good reminder to look at image:, which as far as I can see is specific to swiftmailer. It could be good to support it for back-compatibility however I would see it as deprecated. I propose we should get the main function of this issue working here, and raise a separate issue for image:.

This issue #1333730: [Meta] PHP DOM (libxml2) misinterprets HTML5 covers HTML5 support in core, and it proposes to use html5-php. The interface seems close to \DOMDocument, so sticking with \DOMDocument is likely the best direction for now.

Details comments are in the MR.

metallized’s picture

@AdamPS, i'll wait your answer to commit the changes.

adamps’s picture

The UTF-8 problem seems unpleasant, thanks for debugging it. There seems to a choice of exactly how to solve it, see discussion. Most of the options seem a bit hacky, with possible problems.

1) According to #3342481: Deprecated function: mb_convert_encoding(): Handling HTML entities via mbstring is deprecated, the mb_convert_encoding() option could be deprecated in PHP 8.2.

2) In tijsverkoyen/css-to-inline-styles (which is already a dependency of this module), in createDomDocumentFromHtml() it uses

mb_encode_numericentity($html, [0x80, 0x10FFFF, 0, 0x1FFFFF], 'UTF-8')

This seems even more hacky than most options however I guess there is a precedent for it.

3) We could potentially avoid all the problems and create a future-proof solution using html5-php, as mentioned in #13.

adamps’s picture

For the other question about this code:

public function embedFromPath(string $path, string $name = NULL, string $mimeType = NULL) {
    $contentId = bin2hex(random_bytes(16));
    $name = empty($name) ? $contentId . '@symfony_mailer' : $contentId . '@' . $name;
    $this->inner->embedFromPath($path, $name, $mimeType);

    return $name;
  }

Basically like that, however if the caller passed $name we should use it. So I would say more like this, I think better than random_bytes() is to use a hash of $path.

if (!$name) {
  $name = sha1($path);
}
adamps’s picture

I just spotted one more thing. The original patch had $processed_images to avoid embedding the same image twice. We still need that I think - it can be map from filename to cid. If the image was already processed, then we skip the call to embedFromPath() but still call setAttribute().

metallized’s picture

I think all gets fixed, about #17, i think is not necesary cause in first place we need to get the cid from uri so we can create a $processedImage, i think we need a helper method or function to create the cid string so we skip the call to Symfony\Component\Mime\Email::embedFromPath.

adamps’s picture

Status: Needs work » Needs review

Great thanks I'll take a look

adamps’s picture

So HTML5 solved all the problems??

Interesting, core has HTML::load() and Html::serialize. I guess that would be the alternative if we find problems with the HTML5 library.

metallized’s picture

Assigned: Unassigned » metallized
Status: Needs review » Needs work

Yeah solved all problems.

Let me replace the use of direct HTML5 for Core HTML.

metallized’s picture

Status: Needs work » Needs review

All done, also working encoding.

metallized’s picture

Assigned: metallized » Unassigned
metallized’s picture

@AdamPS do you need something more, can we update this?

adamps’s picture

Sorry I haven't forgotten, just been a bit busy. I'll get to it soon.

adamps’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Generally it's looking good thanks. This is a key issue and it's important that we do all we can to get it right😃. I read through the past comments and here's what I spotted:

1) #17/#18 seems to be still unresolved.

embedFromPath() can keep a map with key = path and value = name. If the key already exists then skip the call to $this->inner->embedFromPath.

2) Originally I suggested a CID counter. Unfortunately later I forgot about this idea, and suggested sha1. The counter seems better as it's faster and generates shorter names. Any reason why we can't use the original counter idea?

3) We should have tests for new features please (true, existing test coverage is poor, at least we can stop adding new code without tests😃).

  • Create a new functional test class AdjusterTest which should have a test for each adjuster. Of course you don't have to add the existing ones - just create one for the new adjuster.
  • Create a new function SymfonyMailerKernelTest::emailInterfaceTest() which should call each function on EmailInterface/BaseEmailInterface. Again just need to call the new function and try to find a way to test that it worked.

4) Security implications. This adjuster could potentially be used to bypass access checking. The file might be in the private file system, or it could even be settings.php. Perhaps security should be a separate issue - in which case this one might need to be postponed on the other.

metallized’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests

First of all, i want to apologize for sending so many commits (i couldn't get phpunit to work on my local, but finallly i did it), i did answer to your comments on gitlab.

1) #17/#18 seems to be still unresolved.

Don't get me wrong but why are you concerned about this, symfony do this on Email::prepareParts() on line 502, anyway i did a extra validation on BaseEmailTrait::embedFromPath() what do you think about this?

2) Originally I suggested a CID counter. Unfortunately later I forgot about this idea, and suggested sha1. The counter seems better as it's faster and generates shorter names. Any reason why we can't use the original counter idea?

See my previous answer.

3) We should have tests for new features please (true, existing test coverage is poor, at least we can stop adding new code without tests😃).

Create a new functional test class AdjusterTest which should have a test for each adjuster. Of course you don't have to add the existing ones - just create one for the new adjuster.
Create a new function SymfonyMailerKernelTest::emailInterfaceTest() which should call each function on EmailInterface/BaseEmailInterface. Again just need to call the new function and try to find a way to test that it worked.

What do you think about what i did?

4) Security implications. This adjuster could potentially be used to bypass access checking. The file might be in the private file system, or it could even be settings.php. Perhaps security should be a separate issue - in which case this one might need to be postponed on the other.

I add a validation to make sure is a image file, what do you think about this?

adamps’s picture

Status: Needs review » Needs work

Great many thanks

1)

Don't get me wrong but why are you concerned about this, symfony do this on Email::prepareParts() on line 502

I am concerned about the case that the email contains the the same URI in two places. If we call embedFromPath() twice then it will create 2 identical entries in $this->attachments(), see line 391 in Email.php.

3) Tests look good thanks.

4) Eventually the security should be similar to the existing Drupal access system - there would be hooks that return an access result, and an adjuster to configure policy in the GUI. In the beginning we can start with a simple rule that is safe. I made a suggestion in the MR based on testing the URL protocol.

metallized’s picture

Status: Needs work » Needs review

I made all your suggestions, please review.

adamps’s picture

Status: Needs review » Needs work

Great thanks. I made one comment on the new function.

metallized’s picture

Status: Needs work » Needs review

Please see my answer on gitlab, i think we should stay close to the swiftmailer implementation, we can even remove the absolutepath transformation and scheme/protocol validations.

adamps’s picture

Great I like the mime type validation, and being similar to swiftmailer.

In BaseEmailTrait::embedFromPath(), attachNoPath(), attachFromPath() there is a $mimeType parameter. This is passed through to the Symfony Mailer library methods which will guess a mime type if one hasn't been set. I propose that we should guess consistently and only do it once (rather than doing some guessing in the module, some in the library, and sometimes guessing the same file twice).

It seems better to guess in the module, using the same Drupal code used throughout the website, so your new code is good. We can guess the mime type in all three BaseEmailTrait methods (except if a value is passed already), and pass the answer to the library. We can use the same mime type for allowEmbed().

we can even remove the absolutepath transformation

I agree - we already have AbsoluteUrlEmailAdjuster. If necessary we can make the new adjuster run after AbsoluteUrlEmailAdjuster.

and remove the scheme/protocol validations.

I don't think so. If an email contains a link to a private image, this is safe - the recipient can only see the image if they log in as a user with the correct permissions. If an email contains an embedded private image, then the recipient can immediately see the image without any access checking. The InlineImagesEmailAdjuster tries to embed all images, so this creates a dangerous combination.

We should have tests to verify the access checking. Embedding should fail for a private:\\ image, settings.php, or an image denied by htaccess. Embedding should succeed for a public:\\ image, or core/themes/stark/screenshot.png.

metallized’s picture

Great I like the mime type validation, and being similar to swiftmailer.

In BaseEmailTrait::embedFromPath(), attachNoPath(), attachFromPath() there is a $mimeType parameter. This is passed through to the Symfony Mailer library methods which will guess a mime type if one hasn't been set. I propose that we should guess consistently and only do it once (rather than doing some guessing in the module, some in the library, and sometimes guessing the same file twice).

It seems better to guess in the module, using the same Drupal code used throughout the website, so your new code is good. We can guess the mime type in all three BaseEmailTrait methods (except if a value is passed already), and pass the answer to the library. We can use the same mime type for allowEmbed().

What you think about what i did?

I don't think so. If an email contains a link to a private image, this is safe - the recipient can only see the image if they log in as a user with the correct permissions. If an email contains an embedded private image, then the recipient can immediately see the image without any access checking. The InlineImagesEmailAdjuster tries to embed all images, so this creates a dangerous combination.

We should have tests to verify the access checking. Embedding should fail for a private:\\ image, settings.php, or an image denied by htaccess. Embedding should succeed for a public:\\ image, or core/themes/stark/screenshot.png.

And what if a site admin/module wants/needs to embed those files?, i think we should not restrict this, in the end is the admin/modules who decides what to embed.

adamps’s picture

And what if a site admin/module wants/needs to embed those files?, i think we should not restrict this, in the end is the admin/modules who decides what to embed.

But it is not only admins that can choose the content of an email. E.g. with simplenews, anyone with permission to edit a newsletter node can insert links to private images - even ones that they don't have permission to see. Maybe with contact module even anon users can insert links in emails.

This module must not break access control of images, that would be a security issue and I cannot commit it. If we restrict too much, then true it is limiting, but I can commit it. We can add ways to manage the access in detail in another issue. Still the default must be secure.

adamps’s picture

Status: Needs review » Needs work

Many thanks, the mime type code is good, I made some detailed comments.

I'm sorry but the security issue cannot be ignored. We have a stable release and the security team would insist on a fix or shut the module down. If the recipient of the email would not be allowed to see the image using a link then also they must not be allowed to see it using embed. We should have tests to verify this. Otherwise, an anonymous user who can guess the URL of a private image could email it to themself (e.g. using webform).

Certainly I don't insist on a solution using scheme/protocol validations - it could also be a different solution, which could even be better. Ideally we would email a private image to the email address of a user who has permission to see it. And we would email an image within /modules or /themes except if it was blocked by .htaccess. That would mean that we fetch the image in a way that runs through the normal Drupal access checking, using the permissions of the current user.

metallized’s picture

Status: Needs work » Needs review

Hi, i make all the fixes (i think), about the security issues i will wait to suggestions about how to do it, i did try with the Drupal path validator service, but doesn't works as need it.

We need to make some research about how other modules do it. Note that in SwiftMailer, they does not do any security validation about image paths.

If you want to mark this as posponed its OK.

dennis_meuwissen’s picture

Embedding images works well with the new email adjuster, except that the adjuster plugin runs before the email is wrapped. Any images from the wrapped HTML (from email-wrap.html.twig) will then not be embedded. Setting the plugin weight to 850 so that it runs after the mailer_wrap_and_convert plugin solves that.

adamps’s picture

Setting the plugin weight to 850 so that it runs after the mailer_wrap_and_convert plugin solves that.

That seems to make sense. It also makes this plug-in after AbsoluteUrlEmailAdjuster, which I think should be fine.

heddn’s picture

Status: Needs review » Reviewed & tested by the community

Even if this is only scoped to an API addition for mail plugins to call embedFromPath, this is a great improvement. I vote to get this merged. It works. If the UI components are still being debated, can we move those into a follow-up so we can get the basics of this feature added to the code base?

adamps’s picture

Status: Reviewed & tested by the community » Needs work

@heddn
This issue is for the UI components as indicated in the title. It's not ready to commit due to legitimate security concerns that are clearly explained in earlier comments.

However I agree it's a good idea to split the API change into a separate issue. It can take parts of this patch and likely could be committed.

heddn’s picture

I've rebased the MR here and moved the API additions over into #3382624: embed attachments API addition.

adamps’s picture

connbi’s picture

StatusFileSize
new7.25 KB

I referred to the code in the PR and created a patch to scan the pictures in the email and replace them with cid. It works normally for me. According to the code in the PR above, there may be a more elegant way to solve it. I don't have time to continue researching this issue at the moment.
my symfony mailer version is 1.2.2. This patch is only used to solve the problem that the embed image is broken in the email body.

ipa 🍺’s picture

Thanks for the patch. The patch is working except if I add an image to the body with a token then the image is not being processed because the token replace is being called later in the process. Any ideas in how to solve this?

colin.eininger’s picture

The weight of InlineImagesEmailAdjuster should be greater than the WrapAndConvertEmailAdjuster one. This way the inline images adjuster can be used in wrap templates too.

Maybe set it to 1000.

ipa 🍺’s picture

Thanks! I was using the latest patch in this issue, when I switch to the MR it works

metallized’s picture

Hey @AdamPS, do you have any ideas about resolving the security issue when embedding files?, what about a whitelist/blacklist files, also i think that developers can skip this restrictions maybe an embedFromPath and uncheckedEmbedFromPath?

Anything else i can help you?

adamps’s picture

@metallized Thanks please see #3382624: embed attachments API addition

adamps’s picture

The API changes are now checked in so work can continue here.

metallized’s picture

@adamps, Is there anything left here?

adamps’s picture

@metallized Yes we still need the adjuster and the test for it. The needs updating a little as the API is different. This issue no longer needs the calls to setAttribute() or setHtmlBody() because that's handled automatically in the Email class.

vacho made their first commit to this issue’s fork.

adamps’s picture

Thanks. I believe the fix shouldn't need to change Email.php or BaseEmailTrait.php now.

sorlov made their first commit to this issue’s fork.

sorlov’s picture

Status: Needs work » Needs review

Made rebase and also fixed missing config schema for EmailAdjuster plugin

adamps’s picture

Version: 1.x-dev » 2.x-dev
Status: Needs review » Needs work

Thanks. The MR still seems mixed up - please see #53. This MR should only contain the new adjuster, schema changes and tests.

New fixed need to go into 2.x please. We can backport to 1.x after.

adamps changed the visibility of the branch 1.x to hidden.

adamps changed the visibility of the branch 2.x to hidden.

adamps’s picture

Assigned: Unassigned » adamps

adamps changed the visibility of the branch 3284140-email-adjuster-to to hidden.

adamps’s picture

adamps’s picture

Status: Needs work » Needs review

Adding credit

  • adamps committed 188c5042 on 2.x
    Issue #3284140 by metallized, sorlov, adamps, pstewart, heddn, connbi,...
adamps’s picture

From #45

The weight of InlineImagesEmailAdjuster should be greater than the WrapAndConvertEmailAdjuster one. This way the inline images adjuster can be used in wrap templates too.

That's a good point. However in v2.x the weight has to be < 600 to run before AttachmentAccessEmailProcessor. I've raised a follow up #3527701: Allow inline of images in the wrapped template..

adamps’s picture

Status: Needs review » Fixed

Thanks everyone

adamps’s picture

Assigned: adamps » Unassigned

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

r.van.doorn changed the visibility of the branch 1.x to active.