GithubHelp home page GithubHelp logo

hapytex / django-antipatterns Goto Github PK

View Code? Open in Web Editor NEW
130.0 9.0 21.0 565 KB

A set of (anti)patterns found over the years.

Home Page: https://www.django-antipatterns.com/

License: MIT License

Shell 21.27% Makefile 13.92% HTML 41.76% CSS 19.84% JavaScript 3.20%
django patterns antipatterns book django-antipatterns

django-antipatterns's People

Contributors

barribarri20 avatar bernardoduarte avatar kommusoft avatar nicksonlangat avatar sharpseeer avatar suspiciousraccoon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

django-antipatterns's Issues

[Antipattern]

% Rendering into JavaScript

severity: 4
type: antipattern
tags: []
layers: [templates, views]
related_packages: []

Often one wants to pass some data to JavaScript and to do that one renders into JavaScript using Django. for example:

<body>
Body Content
<script>
    let a = {{ var_1 }};
    let b = "{{ var_2|safe }}";
</script>
<script>
    let c = {{ var_3|safe }};
</script>
</body>

Why is it a problem?

This is very unsafe and it makes one vulnerable to XSS attacks. Let us consider that the values of the variables rendered above are provided from the user. For demonstration purposes below is some view code with these variables set to such values that each of them will cause an alert:

import json


def test_xss(request):
    context = {
        'var_1': 'alert(1)',
        'var_2': '";\nalert(2);\n"',
        'var_3': json.dumps('</script><script>alert(3);</script><script>')
    }
    return render(request, 'test.html', context)

This snippet might not cause much damage, but it can be much more dangerous. It can also be seen that even using json.dumps is not very safe here.

What can be done to resolve the problem?

Don't render from Django into JavaScript, instead use the json_script template filter [Django-doc] and parse it's results using JSON.parse:

<body>
Body Content
{{ var_1|json_script:"var-1-json" }}
{{ var_2|json_script:"var-2-json" }}
{{ var_3|json_script:"var-3-json" }}
<script>
    let a = JSON.parse(document.getElementById('var-1-json').textContent);;
    let b = JSON.parse(document.getElementById('var-2-json').textContent);;
</script>
<script>
    let c = JSON.parse(document.getElementById('var-3-json').textContent);;
</script>
</body>

Using regular html comments instead of Django template comment.

% Using regular html comment instead of Django template comment.

author: Alex Deathway
severity: 1
type: antipattern
typefa: "fas fa-ban"
tags: [templates]
layers:templates, app_name/template/app_name/
related_packages: []
solinks: []

Why is it a problem?

Using regular comments in templates causes django to render comments with html,which may be intended for developers only.

< !-- exclude dashboard for
-not authenticated users
-users with not enough privilege

-->

{% comment %}
	  exclude dashboard for 
		  -unauthenticated users
		  -users with not enough privileges 
{% endcomment %}

What can be done to resolve the problem?

Using comment block

for single line:

{# your comment here  #}

for multi line:

{% comment %}
	your 
	multiline 
	comment 
	here
{% endcomment %}

How to run?

Can you provide a documentation to describe how to run this project on local machine? I want to add some frontend fixes If I can run.

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.