2017 © Pedro Peláez
 

library safe-translations

Automatically escape variables when using Symfony Translations

image

arthens/safe-translations

Automatically escape variables when using Symfony Translations

  • Thursday, October 19, 2017
  • by arthens
  • Repository
  • 3 Watchers
  • 3 Stars
  • 23,599 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 11 Versions
  • 2 % Grown

The README.md

Safe Translations

arthens/safe-translations is an extra security layer on top of Symfony Translations., (*1)

Build Status, (*2)

Compatibility

Version 1.0 is PHP 7.* and Twig 2.* only., (*3)

Version 0.4 is the last version supporting PHP 5.* and Twig 1.*, (*4)

The problem

Twig is a great rendering library, and it's also awesome for protecting against XSS because all input is automatically escaped. For example, if you have the following template:, (*5)

Hello %username%, (*6)

and the user sets their username to, (*7)

<script>alert();</script>, (*8)

you can sleep safe, because Twig will automatically escape to:, (*9)

Hello &lt;script&gt;alert();&lt;/script&gt;, (*10)

which is harmless., (*11)

So what's the problem?

The problem is that when using Symfony Translations you lose this protection. The Twig template:, (*12)

{% trans %}Hello %username%{% endtrans %}, (*13)

is not safe to use because username will not be automatically escaped. You have to escape it yourself:, (*14)

{% trans with {'%username%': username|e} %}Hello %username%{% endtrans %}, (*15)

which means that your templates are unsecure by default, and it's now your responsability to remember to escape variables every time you use them. Not the end of the world, but wouldn't it be better if variables were automatically escaped like in Twig?, (*16)

Note: this problem only applies to tokens. If you use the |trans filter then you are ok, because everything is escaped (unless you also use |raw, in that case you have a problem). See documentation note., (*17)

My solution

arthens/safe-translations defines 2 new Twig tokens: {% safetrans %} and {% safetranschoice %}. They work exactly like {% trans %} and {% transchoice %}, but variables are automatically escaped:, (*18)

{% safetrans %}Hello %username%{% endsafetrans %}, (*19)

will once again produce, (*20)

Hello &lt;script&gt;alert();&lt;/script&gt;, (*21)

But what if I need to mix escaped and unescaped variables (e.g. inject HTML)?

You can, you just have to use |unescaped:, (*22)

{% trans with {'%message%': message|unescaped} %}Hello %username%, admin says: %message%{% endtrans %}, (*23)

In this case username is escaped, and message is not., (*24)

Installation

  • Add arthens/safe-translations to your composer.json.
  • Register Arthens\SafeTranslation\Extension\SafeTransExtension in your Twig_Enviroment.

and you should be good to go (assuming that you have already configured Symfony Translations)., (*25)

FAQ

1. How can I automatically escape the variables when using Symfony Translations with Twig?

Use {% safetrans %} and {% safetranschoice %}., (*26)

2. What options do safetrans and safetranschoice support?

They are built on top of Symfony Translations, and they support iexactly the same options. See Symfony Translations, (*27)

3. How do I extract the strings from my template?

Use the standard Symfony Extractor. (Under the hood arthens/safe-translations extends Symfony's TransNode, which means that from the point of view of the extractor there's no different between trans and safetrans)., (*28)

4. Why do I need to use |unescaped? Can't it guess it from the context?

Not yet. Symfony Translations and Twig are quite different, and I couldn't find a way to do it automagically. This might change with future versions. Pull requests are welcome., (*29)

5. Is this production ready?

99designs has been using it in production since 2013., (*30)

The Versions

19/10 2017

dev-master

9999999-dev

Automatically escape variables when using Symfony Translations

  Sources   Download

MIT

The Requires

 

The Development Requires

19/10 2017

1.1.1

1.1.1.0

Automatically escape variables when using Symfony Translations

  Sources   Download

MIT

The Requires

 

The Development Requires

19/10 2017

dev-symtony-3-support

dev-symtony-3-support

Automatically escape variables when using Symfony Translations

  Sources   Download

MIT

The Requires

 

The Development Requires

06/03 2017

1.0

1.0.0.0

Automatically escape variables when using Symfony Translations

  Sources   Download

MIT

The Requires

 

The Development Requires

06/03 2017

1.1

1.1.0.0

Automatically escape variables when using Symfony Translations

  Sources   Download

MIT

The Requires

 

The Development Requires

06/03 2017

dev-support-twig-2

dev-support-twig-2

Automatically escape variables when using Symfony Translations

  Sources   Download

MIT

The Requires

 

The Development Requires

28/10 2016

0.4

0.4.0.0

Automatically escape variables when using Symfony Translations

  Sources   Download

MIT

The Requires

 

The Development Requires

13/11 2015

0.3

0.3.0.0

Automatically escape variables when using Symfony Translations

  Sources   Download

MIT

The Requires

 

The Development Requires

13/11 2015

dev-fix-deprecation-warnings

dev-fix-deprecation-warnings

Automatically escape variables when using Symfony Translations

  Sources   Download

MIT

The Requires

 

The Development Requires

13/06 2014

0.2

0.2.0.0

Automatically escape variables when using Symfony Translations

  Sources   Download

MIT

The Requires

 

The Development Requires

11/05 2014

0.1

0.1.0.0

Automatically escape variables when using Symfony Translations

  Sources   Download

The Requires

 

The Development Requires