Skip to content

Commit 18c0f96

Browse files
committed
[feat] Automatic title obfuscation for identical email and title in link method
1 parent f26cdb1 commit 18c0f96

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ $muddle = new Muddle(
8585
$muddle->link('[email protected]');
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="[email protected]" title="[email protected]" />`), 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:
@@ -122,8 +127,8 @@ composer test
122127

123128
## Todo
124129

125-
- [ ] Add Dusk tests
126-
- [ ] Make loading components dynamic
130+
- [ ] Add Dusk tests
131+
- [ ] Make loading components dynamic
127132

128133
## Contributing
129134

@@ -135,9 +140,9 @@ Please review [our security policy](../../security/policy) on how to report secu
135140

136141
## Credits
137142

138-
- [Mo Khosh](https://github.com/mokhosh)
139-
- [Joe Tannenbaum](https://github.com/joetannenbaum/obfuscate) for the inspiration.
140-
- [Spencer Mortensen](https://spencermortensen.com/articles/email-obfuscation) for the information.
143+
- [Mo Khosh](https://github.com/mokhosh)
144+
- [Joe Tannenbaum](https://github.com/joetannenbaum/obfuscate) for the inspiration.
145+
- [Spencer Mortensen](https://spencermortensen.com/articles/email-obfuscation) for the information.
141146

142147
## License
143148

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:[email protected]" 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 = '[email protected]';
41+
$title = '[email protected]';
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)