Skip to content

Commit 6619c98

Browse files
authored
[feat] Automatic title obfuscation for identical email and title in link method (#14)
1 parent f26cdb1 commit 6619c98

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ $muddle = new Muddle(
8585
$muddle->link('test@example.com');
8686
```
8787

88+
## Automatic Title Obfuscation
89+
90+
When using the `link` method or component, if the `email` and `title` attributes are identical (for example, `<x-muddle-link email="test@example.com" title="test@example.com" />`), the title will be automatically obfuscated using the text strategy. This prevents the email from being exposed in plain text.
91+
Please note that this automatic behavior only applies for the default strategy. If you are using a specific strategy you will need to handle this scenario manually.
92+
8893
## Configuration
8994

9095
You can publish the config file with:

src/Muddle.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ public function __construct(
1414

1515
public function link(string $email, string $title): string
1616
{
17+
if ($email === $title) {
18+
$title = $this->text($title);
19+
}
20+
1721
return $this->link->muddle($email, $title);
1822
}
1923

tests/Laravel/MuddleTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,22 @@
3232
->and(html_entity_decode($entitizedLink))
3333
->toBe('<a href="mailto:test@example.com" data-attributes>email</a>');
3434
});
35+
36+
it('muddles both email and title when email is also the title', function () {
37+
Config::set('muddle.strategy.text', Text\Append::class);
38+
Config::set('muddle.strategy.link', Link\Entities::class);
39+
40+
$email = 'test@example.com';
41+
$title = 'test@example.com';
42+
43+
$muddledLink = Muddle::link($email, $title);
44+
$muddledTitle = preg_replace('/^<a[^>]*>|<\/a>$/', '', $muddledLink);
45+
46+
expect($muddledLink)
47+
->not->toBe("<a href=\"mailto:{$email}\">{$title}</a>")
48+
->and((new Text\Append)->unmuddle($muddledTitle))
49+
->toBe($title)
50+
->and(html_entity_decode($muddledLink))
51+
->not->toBe("<a href=\"mailto:{$email}\" data-attributes>{$title}</a>")
52+
->toBe("<a href=\"mailto:{$email}\" data-attributes>{$muddledTitle}</a>");
53+
});

0 commit comments

Comments
 (0)