GithubHelp home page GithubHelp logo

flea89 / django-angular-protect Goto Github PK

View Code? Open in Web Editor NEW

This project forked from marcoazza/django-angular-protect

0.0 2.0 0.0 22 KB

A Django application designed to protect against XSS issues when using Angular with Django

License: MIT License

Python 82.25% HTML 17.75%

django-angular-protect's Introduction

This is a work in progress and not suitable for production (or any other) use

Django Angular Protect

This is a Django app which helps protect against potential XSS issues when using both Angular's client-side templates alongside Django's server-side template system.

High-level Overview

This app provides a new function (angular.shortcuts.render()) for rendering Django templates. If you use this function to render a template then all Django template variable substitutions are disabled by default.

For example this template:

<span>{{user.first_name}}</span>

Would output the following when using angular.shortcuts.render()

<span></span>

That's not that useful when taken on its own. But it's safe for Angular. To get access to Django variables you must now be explicit:

{% djangoblock %}
<span>{{user.first_name}}</span>
{% enddjangoblock %}

This will output the following:

<div ng-non-bindable>
<span>{{user.first_name}}</span>
</div>

The {% djangoblock %} tag enables Angular's protections by wrapping Django content in ng-non-bindable which disables Angular directives, while temporarily allowing access to the Django context.

There are occasions though where you need to use data from the Django context which are "safe" (as in they are not sourced from user input), and you need them for an Angular directive.

In that situation, you can use the ng_mark_safe template filter while outside a {% djangoblock %}:

<div data-my-attribute="{{safe_var|ng_mark_safe}}">

Which would output the value of safe_var, of course, you must ensure the variable does not contain data which could potentially be malicious.

ng_escape is does the same thing as ng_mark_safe except it will disrupt any Angular closing tags by inserting a slash in them. (e.g. ]] would become ]/]) which prevents Angular expanding them.

Configuration

  • Add the 'angular' app to your INSTALLED_APPS setting.
  • Add 'angular.middleware.EnsureAngularProtectionMiddleware' to your MIDDLEWARE_CLASSES setting.
  • Set settings.NG_CLOSING_TAG and settings.NG_OPENING_TAG setting. This has no default as it's essential that it is set to match your $interpolateProvider.startSymbol('[[').endSymbol(']]'); setting.

USE WITH CAUTION!

Using ng_escape and ng_mark_safe with user submitted data can expose you to XSS attacks!

In particular, you should not use them inside Angular directives (such as ng-if) or HTML tag attributes unless you are certain the variable does not contain any data coming from a user.

ng_escape will not protect you at all inside HTML attributes as all it does is escape the endSymbol and malicious Javascript will not need those (e.g. ng-if="{{user_var|ng_escape}}" could evaulate to ng-if="alert(0)").

django-angular-protect's People

Contributors

kazade avatar willsb3 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.