OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Prevent using withCredentials: false by default
Oliver Günther1be9e42c21ab
diff --git a/frontend/src/app/core/file-upload/op-direct-file-upload.service.ts b/frontend/src/app/core/file-upload/op-direct-file-upload.service.tsindex 50e7953bd70..24be704f210 100644--- a/frontend/src/app/core/file-upload/op-direct-file-upload.service.ts+++ b/frontend/src/app/core/file-upload/op-direct-file-upload.service.ts@@ -35,6 +35,7 @@ import { getType } from 'mime';import {OpenProjectFileUploadService, UploadBlob, UploadFile, UploadInProgress,} from './op-file-upload.service';+import { EXTERNAL_REQUEST_HEADER } from "core-app/features/hal/http/openproject-header-interceptor";interface PrepareUploadResult {url:string;@@ -79,7 +80,9 @@ export class OpenProjectDirectFileUploadService extends OpenProjectFileUploadSerobserve: 'events',// This is important as the CORS policy for the bucket is * and you can't use credentals then,// besides we don't need them here anyway.- withCredentials: false,+ headers: {+ [EXTERNAL_REQUEST_HEADER]: 'true',+ },responseType: responseType as 'json',// Subscribe to progress events. subscribe() will fire multiple times!reportProgress: true,diff --git a/frontend/src/app/features/enterprise/enterprise-active-trial/ee-active-trial.component.ts b/frontend/src/app/features/enterprise/enterprise-active-trial/ee-active-trial.component.tsindex 60802805f58..01e5a47abaf 100644--- a/frontend/src/app/features/enterprise/enterprise-active-trial/ee-active-trial.component.ts+++ b/frontend/src/app/features/enterprise/enterprise-active-trial/ee-active-trial.component.ts@@ -27,14 +27,21 @@//++import {- ChangeDetectorRef, Component, ElementRef, OnInit,+ ChangeDetectorRef,+ Component,+ ElementRef,+ OnInit,} from '@angular/core';import { distinctUntilChanged } from 'rxjs/operators';import { I18nService } from 'core-app/core/i18n/i18n.service';import { EnterpriseTrialService } from 'core-app/features/enterprise/enterprise-trial.service';-import { HttpClient, HttpErrorResponse } from '@angular/common/http';+import {+ HttpClient,+ HttpErrorResponse,+} from '@angular/common/http';import { EEActiveTrialBase } from 'core-app/features/enterprise/enterprise-active-trial/ee-active-trial.base';import { GonService } from 'core-app/core/gon/gon.service';+import { EXTERNAL_REQUEST_HEADER } from 'core-app/features/hal/http/openproject-header-interceptor';@Component({selector: 'enterprise-active-trial',@@ -95,8 +102,16 @@ export class EEActiveTrialComponent extends EEActiveTrialBase implements OnInit// use the trial key saved in the db// to get the user data from Augurprivate getUserDataFromAugur() {- this.http- .get<any>(`${this.eeTrialService.trialLink}/details`)+ this+ .http+ .get<any>(+ `${this.eeTrialService.trialLink}/details`,+ {+ headers: {+ [EXTERNAL_REQUEST_HEADER]: 'true',+ },+ },+ ).toPromise().then((userForm:any) => {this.eeTrialService.userData$.putValue(userForm);diff --git a/frontend/src/app/features/enterprise/enterprise-trial-waiting/ee-trial-waiting.component.ts b/frontend/src/app/features/enterprise/enterprise-trial-waiting/ee-trial-waiting.component.tsindex 7aec5cfa2ee..eaf493b84e5 100644--- a/frontend/src/app/features/enterprise/enterprise-trial-waiting/ee-trial-waiting.component.ts+++ b/frontend/src/app/features/enterprise/enterprise-trial-waiting/ee-trial-waiting.component.ts@@ -26,13 +26,18 @@// See COPYRIGHT and LICENSE files for more details.//++-import { Component, ElementRef, OnInit } from '@angular/core';+import {+ Component,+ ElementRef,+ OnInit,+} from '@angular/core';import { I18nService } from 'core-app/core/i18n/i18n.service';import { EnterpriseTrialService } from 'core-app/features/enterprise/enterprise-trial.service';import { HttpClient } from '@angular/common/http';import { ToastService } from 'core-app/shared/components/toaster/toast.service';import { distinctUntilChanged } from 'rxjs/operators';import { TimezoneService } from 'core-app/core/datetime/timezone.service';+import { EXTERNAL_REQUEST_HEADER } from 'core-app/features/hal/http/openproject-header-interceptor';@Component({selector: 'enterprise-trial-waiting',@@ -86,7 +91,15 @@ export class EETrialWaitingComponent implements OnInit {// resend mail if resend link has been clickedpublic resendMail() {this.eeTrialService.cancelled = false;- this.http.post(this.eeTrialService.resendLink, {})+ this.http.post(+ this.eeTrialService.resendLink,+ {},+ {+ headers: {+ [EXTERNAL_REQUEST_HEADER]: 'true',+ },+ },+ ).toPromise().then(() => {this.toastService.addSuccess(this.text.resend_success);diff --git a/frontend/src/app/features/enterprise/enterprise-trial.service.ts b/frontend/src/app/features/enterprise/enterprise-trial.service.tsindex 964e20df71b..b75f5dd9d7e 100644--- a/frontend/src/app/features/enterprise/enterprise-trial.service.ts+++ b/frontend/src/app/features/enterprise/enterprise-trial.service.ts@@ -1,10 +1,14 @@import { Injectable } from '@angular/core';import { I18nService } from 'core-app/core/i18n/i18n.service';-import { HttpClient, HttpErrorResponse } from '@angular/common/http';+import {+ HttpClient,+ HttpErrorResponse,+} from '@angular/common/http';import { PathHelperService } from 'core-app/core/path-helper/path-helper.service';import { ToastService } from 'core-app/shared/components/toaster/toast.service';import { FormGroup } from '@angular/forms';import { input } from 'reactivestates';+import { EXTERNAL_REQUEST_HEADER } from 'core-app/features/hal/http/openproject-header-interceptor';export interface EnterpriseTrialData {id?:string;@@ -65,7 +69,16 @@ export class EnterpriseTrialService {// receive an enterprise trial link to access a tokenpublic sendForm(form:FormGroup) {const request = { ...form.value, token_version: this.tokenVersion };- this.http.post(`${this.baseUrlAugur}/public/v1/trials`, request)+ this.http+ .post(+ `${this.baseUrlAugur}/public/v1/trials`,+ request,+ {+ headers: {+ [EXTERNAL_REQUEST_HEADER]: 'true',+ },+ },+ ).toPromise().then((enterpriseTrial:any) => {this.userData$.putValue(form.value);@@ -89,8 +102,14 @@ export class EnterpriseTrialService {// get a token from the trial link if user confirmed mailpublic getToken() {// 2) GET /public/v1/trials/:id- this.http- .get<any>(this.trialLink)+ this.http.get<any>(+ this.trialLink,+ {+ headers: {+ [EXTERNAL_REQUEST_HEADER]: 'true',+ },+ },+ ).toPromise().then(async (res:any) => {// show confirmed status and enable continue btn@@ -210,7 +229,8 @@ export class EnterpriseTrialService {public get emailError():boolean {if (this.emailInvalid) {return true;- } if (this.error) {+ }+ if (this.error) {return this.emailTaken;}return false;diff --git a/frontend/src/app/features/hal/http/openproject-header-interceptor.ts b/frontend/src/app/features/hal/http/openproject-header-interceptor.tsindex 058cbda20cd..3a37ca19de7 100644--- a/frontend/src/app/features/hal/http/openproject-header-interceptor.ts+++ b/frontend/src/app/features/hal/http/openproject-header-interceptor.ts@@ -4,30 +4,47 @@ import {import { Observable } from 'rxjs';import { Injectable } from '@angular/core';+export const EXTERNAL_REQUEST_HEADER = 'X-External-Request';+@Injectable()export class OpenProjectHeaderInterceptor implements HttpInterceptor {intercept(req:HttpRequest<any>, next:HttpHandler):Observable<HttpEvent<any>> {- const csrf_token:string|undefined = jQuery('meta[name=csrf-token]').attr('content');+ const withCredentials = req.headers.get(EXTERNAL_REQUEST_HEADER) !== 'true';+ if (withCredentials) {+ return this.handleAuthenticatedRequest(req, next);+ } else {+ return this.handleExternalRequest(req, next);+ }+ }- if (req.withCredentials !== false) {- let newHeaders = req.headers- .set('X-Authentication-Scheme', 'Session')- .set('X-Requested-With', 'XMLHttpRequest');+ private handleExternalRequest(req:HttpRequest<any>, next:HttpHandler):Observable<HttpEvent<any>> {+ // Clone the request to add the new header+ const clonedRequest = req.clone({+ withCredentials: false,+ headers: req.headers.delete(EXTERNAL_REQUEST_HEADER),+ });- if (csrf_token) {- newHeaders = newHeaders.set('X-CSRF-TOKEN', csrf_token);- }+ return next.handle(clonedRequest);+ }++ private handleAuthenticatedRequest(req:HttpRequest<any>, next:HttpHandler):Observable<HttpEvent<any>> {+ const csrf_token:string|undefined = jQuery('meta[name=csrf-token]').attr('content');- // Clone the request to add the new header- const clonedRequest = req.clone({- withCredentials: true,- headers: newHeaders,- });+ let newHeaders = req.headers+ .set('X-Authentication-Scheme', 'Session')+ .set('X-Requested-With', 'XMLHttpRequest');- // Pass the cloned request instead of the original request to the next handle- return next.handle(clonedRequest);+ if (csrf_token) {+ newHeaders = newHeaders.set('X-CSRF-TOKEN', csrf_token);}- return next.handle(req);+ // Clone the request to add the new header+ const clonedRequest = req.clone({+ withCredentials: true,+ headers: newHeaders,+ });++ // Pass the cloned request instead of the original request to the next handle+ return next.handle(clonedRequest);}}