Skip to content

Commit

Permalink
set csrf cookie name from env var (#1420)
Browse files Browse the repository at this point in the history
* set csrf cookie name from env var

* foken token
  • Loading branch information
ChristopherChudzicki authored Aug 15, 2024
1 parent c143154 commit 97f72e0
Show file tree
Hide file tree
Showing 10 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ jobs:
POSTHOG_PROJECT_API_KEY: ${{ secrets.POSTHOG_PROJECT_API_KEY_PROD }}
SENTRY_DSN: ${{ secrets.SENTRY_DSN_PROD }}
SENTRY_ENV: ${{ secrets.MITOPEN_ENV_PROD }}
CSRF_COOKIE_NAME: ${{ secrets.CSRF_COOKIE_NAME_PROD }}
MITOL_AXIOS_WITH_CREDENTIALS: true
MITOL_API_BASE_URL: https://api.learn.mit.edu
MITOL_SUPPORT_EMAIL: [email protected]
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/release-candidate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ jobs:
POSTHOG_PROJECT_API_KEY: ${{ secrets.POSTHOG_PROJECT_API_KEY_RC }}
SENTRY_DSN: ${{ secrets.SENTRY_DSN_RC }}
SENTRY_ENV: ${{ secrets.MITOPEN_ENV_RC }}
CSRF_COOKIE_NAME: ${{ secrets.CSRF_COOKIE_NAME_RC }}
MITOL_AXIOS_WITH_CREDENTIALS: true
MITOL_API_BASE_URL: https://api.rc.learn.mit.edu
MITOL_SUPPORT_EMAIL: [email protected]
Expand Down
1 change: 1 addition & 0 deletions env/shared.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
MITOL_API_BASE_URL=http://api.open.odl.local:8063
MITOL_APP_BASE_URL=http://open.odl.local:8062
MITOL_SUPPORT_EMAIL=support@localhost
CSRF_COOKIE_NAME=csrftoken-local

POSTHOG_TIMEOUT_MS=1500
1 change: 1 addition & 0 deletions frontends/api/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const config: Config.InitialOptions = {
APP_SETTINGS: {
MITOL_AXIOS_WITH_CREDENTIALS: false,
MITOL_API_BASE_URL: "https://api.test.learn.mit.edu",
CSRF_COOKIE_NAME: "csrftoken-test",
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion frontends/api/src/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import axios from "axios"
* Our axios instance with default baseURL, headers, etc.
*/
const instance = axios.create({
xsrfCookieName: "csrftoken",
xsrfCookieName: APP_SETTINGS.CSRF_COOKIE_NAME,
xsrfHeaderName: "X-CSRFToken",
withXSRFToken: true,
withCredentials: APP_SETTINGS.MITOL_AXIOS_WITH_CREDENTIALS,
Expand Down
1 change: 1 addition & 0 deletions frontends/api/src/types/settings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export declare global {
const APP_SETTINGS: {
MITOL_AXIOS_WITH_CREDENTIALS?: boolean
MITOL_API_BASE_URL?: string
CSRF_COOKIE_NAME: string
}
}
2 changes: 1 addition & 1 deletion frontends/mit-learn/src/services/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import axios from "axios"
*/
const instance = axios.create({
baseURL: "/api/v0",
xsrfCookieName: "csrftoken",
xsrfCookieName: APP_SETTINGS.CSRF_COOKIE_NAME,
xsrfHeaderName: "X-CSRFToken",
})

Expand Down
6 changes: 6 additions & 0 deletions frontends/mit-learn/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const {
EMBEDLY_KEY,
CKEDITOR_UPLOAD_URL,
SENTRY_DSN,
CSRF_COOKIE_NAME,
} = cleanEnv(process.env, {
NODE_ENV: str({
choices: ["development", "production", "test"],
Expand Down Expand Up @@ -97,6 +98,10 @@ const {
desc: "Sentry Data Source Name",
default: "",
}),
CSRF_COOKIE_NAME: str({
desc: "Name of the CSRF cookie",
default: "csrftoken",
}),
})

const MITOL_FEATURES_PREFIX = "FEATURE_"
Expand Down Expand Up @@ -221,6 +226,7 @@ module.exports = (env, argv) => {
SITE_NAME: JSON.stringify(SITE_NAME),
MITOL_SUPPORT_EMAIL: JSON.stringify(MITOL_SUPPORT_EMAIL),
PUBLIC_URL: JSON.stringify(PUBLIC_URL),
CSRF_COOKIE_NAME: JSON.stringify(CSRF_COOKIE_NAME),
},
}),
]
Expand Down
1 change: 1 addition & 0 deletions frontends/ol-utilities/src/types/settings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export declare global {
declare const APP_SETTINGS: {
MITOL_AXIOS_WITH_CREDENTIALS?: boolean
MITOL_API_BASE_URL: string
CSRF_COOKIE_NAME: string
EMBEDLY_KEY: string
CKEDITOR_UPLOAD_URL?: string
SENTRY_DSN?: string
Expand Down
1 change: 1 addition & 0 deletions main/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@

CSRF_COOKIE_SECURE = get_bool("CSRF_COOKIE_SECURE", True) # noqa: FBT003
CSRF_COOKIE_DOMAIN = get_string("CSRF_COOKIE_DOMAIN", None)
CSRF_COOKIE_NAME = get_string("CSRF_COOKIE_NAME", "csrftoken")

CSRF_HEADER_NAME = get_string("CSRF_HEADER_NAME", "HTTP_X_CSRFTOKEN")

Expand Down

0 comments on commit 97f72e0

Please sign in to comment.