Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django CSP #1303

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion accounts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def __init__(self, *args, **kwargs):
def widget_attrs(self, widget):
"""Override - used to update widget attrs in Field initializer."""
attrs = super().widget_attrs(widget)
return {**attrs, "placeholder": "123456", "style": "width: 50%;"}
return {**attrs, "placeholder": "123456", "class": "w-50"}


class TOTPCheckForm(forms.Form):
Expand Down
20 changes: 19 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"csp.middleware.CSPMiddleware",
]

if DEBUG:
Expand Down Expand Up @@ -387,3 +388,14 @@
# https://docs.djangoproject.com/en/4.2/ref/settings/#std:setting-SESSION_COOKIE_HTTPONLY
# Per the above documentation setting SESSION_COOKIE_HTTPONLY might break JavaScript.
SESSION_COOKIE_HTTPONLY = True


CSP_DEFAULT_SRC = (
"'self' data:",
"fonts.googleapis.com",
"cdnjs.cloudflare.com",
"cdn.datatables.net",
"cdn.jsdelivr.net",
"fonts.gstatic.com",
"www.youtube.com",
)
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "lookit-api"
version = "0.1.0"
description = ""
authors = [""]
authors = ["John Smith <[email protected]>"]

[tool.poetry.dependencies]
bcrypt = "3.2.0"
Expand Down Expand Up @@ -52,6 +52,7 @@ uWSGI = "2.0.19.1"
pillow = "9.4.0"
django-bootstrap-icons = "0.8.2"
js2py = "0.74"
django-csp = "^3.7"

[tool.poetry.group.dev.dependencies]
coverage = "^7.2"
Expand Down
31 changes: 20 additions & 11 deletions web/static/js/study-detail-web.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
$('.text-warning').hide();
$("#child-dropdown").val("none");
childSelected(document.getElementById('child-dropdown'));

function childSelected(selectElement) {
var participateButton = document.getElementById('participate-button');
function childSelected() {
const selectElement = document.getElementById('child-dropdown');
const participateButton = document.getElementById('participate-button');
if (selectElement.value === 'none') {
participateButton.disabled = true;
document.getElementById('too-old').classList.add('d-none');
Expand Down Expand Up @@ -42,13 +39,13 @@ function calculateAgeInDays(birthday) {

function ageCheck(age) {
// Adapted from experiment model in exp-addons
var minDays;
var maxDays;
var study_age_criteria = document.getElementById('child-dropdown').dataset;
let minDays;
let maxDays;
const study_age_criteria = document.getElementById('child-dropdown').dataset;
// These are now hard-coded to avoid unpredictable behavior from moment.duration().asDays()
// e.g. 1 year = 365 days, 1 month = 30 days, and 1 year + 1 month = 396 days.
minDays = parseInt(study_age_criteria.studyMinAgeDays,10) + 30 * parseInt(study_age_criteria.studyMinAgeMonths,10) + 365 * parseInt(study_age_criteria.studyMinAgeYears,10);
maxDays = parseInt(study_age_criteria.studyMaxAgeDays,10) + 30 * parseInt(study_age_criteria.studyMaxAgeMonths,10) + 365 * parseInt(study_age_criteria.studyMaxAgeYears,10);
minDays = parseInt(study_age_criteria.studyMinAgeDays, 10) + 30 * parseInt(study_age_criteria.studyMinAgeMonths, 10) + 365 * parseInt(study_age_criteria.studyMinAgeYears, 10);
maxDays = parseInt(study_age_criteria.studyMaxAgeDays, 10) + 30 * parseInt(study_age_criteria.studyMaxAgeMonths, 10) + 365 * parseInt(study_age_criteria.studyMaxAgeYears, 10);

minDays = minDays || -1;
maxDays = maxDays || Number.MAX_SAFE_INTEGER;
Expand All @@ -61,3 +58,15 @@ function ageCheck(age) {
return 0;
}
}

/**
* On Page load
*/
$('.text-warning').hide();
$("#child-dropdown").val("none");
childSelected();

/**
* Event listeners
*/
document.querySelector('#child-dropdown')?.addEventListener('change', childSelected)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
</p>
{% for section, institutions in institution_sections %}
<p>{{ section.name }}:</p>
<ul style="columns:3">
<ul class="three-column-list">
{% for institution in institutions %}<li>{{ institution.name }}</li>{% endfor %}
</ul>
{% endfor %}
Expand Down
8 changes: 3 additions & 5 deletions web/templates/web/study-detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,11 @@ <h4 class="mt-4 mb-3">{% trans "Would you like to participate in this study?" %}
data-study-min-age-years="{{ study.min_age_years }}"
data-study-max-age-days="{{ study.max_age_days }}"
data-study-max-age-months="{{ study.max_age_months }}"
data-study-max-age-years="{{ study.max_age_years }}"
onchange="childSelected(this)">
<option value=none>{% trans "None Selected" %}</option>
data-study-max-age-years="{{ study.max_age_years }}">
<option value="none">{% trans "None Selected" %}</option>
{% for child in children %}
{% child_is_valid_for_study_criteria child object as child_is_eligible %}
<option onemptied=""
value="{{ child.uuid }}"
<option value="{{ child.uuid }}"
data-birthdate="{{ child.birthday|date:'c' }}"
data-eligible-participation="{{ child_is_eligible.participation_eligibility }}"
data-eligible-criteria="{{ child_is_eligible.criteria_expression_eligibility }}">
Expand Down