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

KK-1417 | Fix SonarCloud security issues & some of the hotspots #483

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

karisal-anders
Copy link
Contributor

@karisal-anders karisal-anders commented Feb 21, 2025

Description

Please read commit messages for details about what has been done.

Fix SonarCloud security issues & some of the hotspots.

Related

@terovirtanen
Copy link
Contributor

KUKKUU-API branch is deployed to platta: https://kukkuu-pr483.api.dev.hel.ninja 🚀🚀🚀

@terovirtanen
Copy link
Contributor

TestCafe result is success for https://kukkuu-pr483.api.dev.hel.ninja 😆🎉🎉🎉

@terovirtanen
Copy link
Contributor

KUKKUU-API branch is deployed to platta: https://kukkuu-pr483.api.dev.hel.ninja 🚀🚀🚀

@terovirtanen
Copy link
Contributor

TestCafe result is success for https://kukkuu-pr483.api.dev.hel.ninja 😆🎉🎉🎉

@karisal-anders karisal-anders force-pushed the KK-1417-fix-security-issues branch from d9c1271 to 58b50c1 Compare February 24, 2025 15:11
@terovirtanen
Copy link
Contributor

KUKKUU-API branch is deployed to platta: https://kukkuu-pr483.api.dev.hel.ninja 🚀🚀🚀

@terovirtanen
Copy link
Contributor

TestCafe result is success for https://kukkuu-pr483.api.dev.hel.ninja 😆🎉🎉🎉

@karisal-anders karisal-anders force-pushed the KK-1417-fix-security-issues branch 2 times, most recently from 5cacee4 to 7736ae0 Compare February 25, 2025 14:37
@terovirtanen
Copy link
Contributor

KUKKUU-API branch is deployed to platta: https://kukkuu-pr483.api.dev.hel.ninja 🚀🚀🚀

@terovirtanen
Copy link
Contributor

TestCafe result is success for https://kukkuu-pr483.api.dev.hel.ninja 😆🎉🎉🎉

@karisal-anders karisal-anders force-pushed the KK-1417-fix-security-issues branch from 7736ae0 to 849fc40 Compare February 26, 2025 07:24
@terovirtanen
Copy link
Contributor

KUKKUU-API branch is deployed to platta: https://kukkuu-pr483.api.dev.hel.ninja 🚀🚀🚀

@terovirtanen
Copy link
Contributor

TestCafe result is success for https://kukkuu-pr483.api.dev.hel.ninja 😆🎉🎉🎉

@terovirtanen
Copy link
Contributor

KUKKUU-API branch is deployed to platta: https://kukkuu-pr483.api.dev.hel.ninja 🚀🚀🚀

1 similar comment
@terovirtanen
Copy link
Contributor

KUKKUU-API branch is deployed to platta: https://kukkuu-pr483.api.dev.hel.ninja 🚀🚀🚀

@terovirtanen
Copy link
Contributor

TestCafe result is success for https://kukkuu-pr483.api.dev.hel.ninja 😆🎉🎉🎉

@karisal-anders karisal-anders force-pushed the KK-1417-fix-security-issues branch from 0916d26 to 9648bb7 Compare February 26, 2025 14:45
@terovirtanen
Copy link
Contributor

KUKKUU-API branch is deployed to platta: https://kukkuu-pr483.api.dev.hel.ninja 🚀🚀🚀

@terovirtanen
Copy link
Contributor

TestCafe result is success for https://kukkuu-pr483.api.dev.hel.ninja 😆🎉🎉🎉

@karisal-anders karisal-anders force-pushed the KK-1417-fix-security-issues branch from 9648bb7 to 41ae1e7 Compare February 27, 2025 07:24
@karisal-anders karisal-anders changed the title KK-1417 | Fix SonarCloud security issues KK-1417 | Fix many of the SonarCloud security issues Feb 27, 2025
@karisal-anders karisal-anders changed the title KK-1417 | Fix many of the SonarCloud security issues KK-1417 | Fix most of SonarCloud security issues Feb 27, 2025
@karisal-anders karisal-anders force-pushed the KK-1417-fix-security-issues branch from 41ae1e7 to e7c4f80 Compare February 27, 2025 07:30
@karisal-anders karisal-anders marked this pull request as ready for review February 27, 2025 07:37
@karisal-anders karisal-anders changed the title KK-1417 | Fix most of SonarCloud security issues KK-1417 | Fix most of SonarCloud security issues & hotspots Feb 27, 2025
@terovirtanen
Copy link
Contributor

KUKKUU-API branch is deployed to platta: https://kukkuu-pr483.api.dev.hel.ninja 🚀🚀🚀

@terovirtanen
Copy link
Contributor

TestCafe result is success for https://kukkuu-pr483.api.dev.hel.ninja 😆🎉🎉🎉

@karisal-anders karisal-anders marked this pull request as draft February 27, 2025 09:23
@karisal-anders karisal-anders force-pushed the KK-1417-fix-security-issues branch from e7c4f80 to 93fe9c8 Compare February 27, 2025 11:51
@karisal-anders karisal-anders changed the title KK-1417 | Fix most of SonarCloud security issues & hotspots KK-1417 | Fix many of the SonarCloud security issues & hotspots Feb 27, 2025
@terovirtanen
Copy link
Contributor

KUKKUU-API branch is deployed to platta: https://kukkuu-pr483.api.dev.hel.ninja 🚀🚀🚀

@terovirtanen
Copy link
Contributor

TestCafe result is success for https://kukkuu-pr483.api.dev.hel.ninja 😆🎉🎉🎉

@karisal-anders karisal-anders marked this pull request as ready for review February 27, 2025 12:03
Copy link
Contributor

@nikomakela nikomakela left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work so far, but I did left some improvement proposals and comments

Dockerfile Outdated
Comment on lines 10 to 13
# chmod=755 = rwxr-xr-x i.e. owner can read, write and execute, group and others can read and execute
COPY --chown=root:root --chmod=755 requirements.txt /app/requirements.txt
COPY --chown=root:root --chmod=755 requirements-not-from-pypi.txt /app/requirements-not-from-pypi.txt
COPY --chown=root:root --chmod=755 requirements-prod.txt /app/requirements-prod.txt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every time a COPY is called, a new layer is created. This reduces the number of layers in Docker image, making it smaller and faster to build:

Suggested change
# chmod=755 = rwxr-xr-x i.e. owner can read, write and execute, group and others can read and execute
COPY --chown=root:root --chmod=755 requirements.txt /app/requirements.txt
COPY --chown=root:root --chmod=755 requirements-not-from-pypi.txt /app/requirements-not-from-pypi.txt
COPY --chown=root:root --chmod=755 requirements-prod.txt /app/requirements-prod.txt
# chmod=755 = rwxr-xr-x i.e. owner can read, write and execute, group and others can read and execute
COPY --chown=root:root --chmod=755 requirements.txt requirements-not-from-pypi.txt requirements-prod.txt /app/

All these 3 copies share a same theme: "copy the requirements".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I think it could help in some cases (e.g. when working inside pod locally), that also the requirements-dev would already be included in the base with other requirements. It's not a huge file, it can be helpful and it does not harm anybody. Then it's COPY could be removed from development stage.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dockerfile Outdated
# "Recursively copying context directories is security-sensitive" i.e.
# https://rules.sonarsource.com/docker/RSPEC-6470/
# see .dockerignore for info on what is not copied here:
COPY --chown=root:root --chmod=755 . /app/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this include also the dev requirements copied in the previous COPY command? Should the previous COPY be replaced with this one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the COPY command to the appbase stage and removed it from development and production stages.

README.md Outdated
Comment on lines 173 to 192
### Generating secret key for Django

Django needs a value for [SECRET_KEY](https://docs.djangoproject.com/en/4.2/ref/settings/#secret-key) to start.
You can generate a value for it using Python & Django:
```python
from django.core.management.utils import get_random_secret_key
get_random_secret_key()
```

or just Python (adapted from Django's
[get_random_secret_key](https://github.com/django/django/blob/5.1.6/django/core/management/utils.py#L79C5-L84) &
[get_random_string](https://github.com/django/django/blob/5.1.6/django/utils/crypto.py#L51-L62)):
```python
import secrets, string
allowed_chars = string.ascii_lowercase + string.digits + "!@#$%^&*(-_=+)"
"".join(secrets.choice(allowed_chars) for i in range(50))
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it's good to mention that these variables are really important in live server usage, I would say that this all is too much in local development use. While developing locally, the secret key can be pretty much anything, so this makes the process overwhelming and "scary". However, it's not good practise to have even a local SECRET_KEY in version control.

Could we add something like

If you prefer, you can use a shorter, manually generated key for local development. Just remember to use a strong, long, randomly generated key in production.

Copy link
Contributor Author

@karisal-anders karisal-anders Feb 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds reasonable, and I thought about something like this, but didn't come up with a good way to say this. I'll revamp this section a bit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplified and clarified the section in a force push.

@karisal-anders karisal-anders force-pushed the KK-1417-fix-security-issues branch from 93fe9c8 to d5779ce Compare March 3, 2025 08:51
also:
 - add explicit dependency for cryptography as it is now used in tests
 - update all dependencies to latest

refs KK-1417
@karisal-anders karisal-anders force-pushed the KK-1417-fix-security-issues branch from d5779ce to 24a5b21 Compare March 3, 2025 08:56
See SonarCloud rule "Django secret keys should not be disclosed" i.e.
https://rules.sonarsource.com/secrets/RSPEC-6687/

refs KK-1417
Fixes SonarCloud security hotspot python:S3752:
"Make sure allowing safe and unsafe HTTP methods is safe here.
Allowing both safe and unsafe HTTP methods is security-sensitive":
https://rules.sonarsource.com/python/RSPEC-3752/

refs KK-1417
Removes the SonarCloud security hotspot python:S2245:
"Make sure that using this pseudorandom number generator is safe here.
Using pseudorandom number generators (PRNGs) is security-sensitive":
https://rules.sonarsource.com/python/RSPEC-2245/

The bottom line here is that it's safe to use pseudorandom number
generator here as this is only for generating test data.

refs KK-1417
Fixes SonarCloud security hotspot python:S5443:
"Make sure publicly writable directories are used safely here.
Using publicly writable directories is security-sensitive":
https://rules.sonarsource.com/python/RSPEC-5443/

refs KK-1417
Fixes the following SonarCloud security hotspots in Dockerfile:

"Make sure no write permissions are assigned to the copied resource.
Allowing non-root users to modify resources copied to an image is
security-sensitive" i.e. SonarCloud rule docker:S6504 i.e.
https://rules.sonarsource.com/docker/RSPEC-6504/

Also:
 - Update `.dockerignore` file to be more general
 - Add comments to `Dockerfile` related to SonarCloud security hotspot
   docker:S6470 i.e. "Recursively copying context directories is
   security-sensitive" https://rules.sonarsource.com/docker/RSPEC-6470/

refs KK-1417
@karisal-anders karisal-anders force-pushed the KK-1417-fix-security-issues branch from 24a5b21 to 19d8ba2 Compare March 3, 2025 08:59
@terovirtanen
Copy link
Contributor

KUKKUU-API branch is deployed to platta: https://kukkuu-pr483.api.dev.hel.ninja 🚀🚀🚀

@terovirtanen
Copy link
Contributor

TestCafe result is success for https://kukkuu-pr483.api.dev.hel.ninja 😆🎉🎉🎉

@karisal-anders
Copy link
Contributor Author

@nikomakela @jakezu This is ready for code review, and the points raised in the comments so far have been addressed.

fix https://rules.sonarsource.com/secrets/RSPEC-6698/ by not using
a hardcoded DATABASE_URL in compose.yaml or settings.py but having a
default value for it in `.env.example` file

also:
 - combine `.env.keycloak.example` and `docker-compose.env.example`
   into `.env.example`
 - make Docker Compose use `.env` for environment variables
 - update README related to environment variable files
 - add more .env.* files explicitly into .gitignore
 - fix comment in docker-entrypoint.sh (was the other way around)

refs KK-1417
@karisal-anders
Copy link
Contributor Author

Added a single commit i.e. 1960eaa

@karisal-anders karisal-anders changed the title KK-1417 | Fix many of the SonarCloud security issues & hotspots KK-1417 | Fix SonarCloud security issues & some of the hotspots Mar 4, 2025
@terovirtanen
Copy link
Contributor

KUKKUU-API branch is deployed to platta: https://kukkuu-pr483.api.dev.hel.ninja 🚀🚀🚀

Copy link

sonarqubecloud bot commented Mar 4, 2025

Quality Gate Failed Quality Gate failed

Failed conditions
1 Security Hotspot

See analysis details on SonarQube Cloud

@terovirtanen
Copy link
Contributor

TestCafe result is failed for https://kukkuu-pr483.api.dev.hel.ninja 😿💢💥💥

@terovirtanen
Copy link
Contributor

TestCafe result is success for https://kukkuu-pr483.api.dev.hel.ninja 😆🎉🎉🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants