Skip to content

Using CSP nonce - enforce stricter CSP #2786

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 5 additions & 4 deletions packages/altair-app/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"buildTarget": "altair:build:development"
"buildTarget": "altair:build:development",
"headers": {
"Content-Security-Policy": "style-src 'self' 'nonce-change-me' 'sha256-47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU='"
}
},
"configurations": {
"production": {
Expand Down Expand Up @@ -123,9 +126,7 @@
}
},
"cli": {
"schematicCollections": [
"@angular-eslint/schematics"
],
"schematicCollections": ["@angular-eslint/schematics"],
"packageManager": "pnpm",
"analytics": false
}
Expand Down
7 changes: 7 additions & 0 deletions packages/altair-app/src/app/modules/altair/altair.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
APP_INITIALIZER,
ApplicationInitStatus,
ModuleWithProviders,
CSP_NONCE,
} from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import {
Expand Down Expand Up @@ -59,6 +60,7 @@ import { RootState } from 'altair-graphql-core/build/types/state/state.interface
import { AccountEffects } from './effects/account.effect';
import { WorkspaceEffects } from './effects/workspace.effect';
import { ElectronEffects } from './effects/electron.effect';
import { AltairConfig } from 'altair-graphql-core/build/config';

registerLocaleData(en);

Expand Down Expand Up @@ -203,6 +205,11 @@ export class AltairModule {
provide: reducerToken,
useValue: getReducer(),
},
{
provide: CSP_NONCE,
useFactory: (altairConfig: AltairConfig) => altairConfig.cspNonce,
deps: [AltairConfig],
},
],
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
import { tags as t } from '@lezer/highlight';
import { InternalEditorError } from '../../utils/errors';
import { debug } from '../../utils/logger';
import { AltairConfig } from 'altair-graphql-core/build/config';

@Component({
selector: 'app-codemirror',
Expand Down Expand Up @@ -85,7 +86,10 @@ export class CodemirrorComponent
private onTouched = () => {};
private onChange = (s: string) => {};

constructor(private zone: NgZone) {}
constructor(
private zone: NgZone,
private altairConfig: AltairConfig
) {}

ngAfterViewInit() {
this.zone.runOutsideAngular(() => {
Expand Down Expand Up @@ -407,6 +411,7 @@ export class CodemirrorComponent
this.wrapLines ? EditorView.lineWrapping : [], // TODO: Create own compartment
drawSelection(),
EditorState.allowMultipleSelections.of(true),
EditorView.cspNonce.of(this.altairConfig.cspNonce),
bracketMatching(),
closeBrackets(),
history(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[appTheme]="theme$ | async"
[appDarkTheme]="themeDark$ | async"
[appAccentColor]="accentColor$ | async"
[cspNonce]="cspNonce"
appFileDrop
(fileDroppedChange)="fileDropped($event)"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ export class AltairComponent {

appVersion = environment.version;

cspNonce = '';

sidebarPanels$: Observable<AltairPanel[]>;
headerPanels$: Observable<AltairPanel[]>;

Expand All @@ -146,6 +148,7 @@ export class AltairComponent {
) {
this.isWebApp = altairConfig.isWebApp;
this.authEnabled = !altairConfig.initialData.disableAccount;
this.cspNonce = altairConfig.cspNonce;
this.settings$ = this.store
.pipe(select('settings'))
.pipe(distinctUntilChanged());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import { Directive, Input, OnInit, OnChanges, SimpleChanges } from '@angular/core';
import {
createTheme,
hexToRgbStr,
ICustomTheme,
ITheme,
getCSS,
} from 'altair-graphql-core/build/theme';
import { ICustomTheme, getCSS } from 'altair-graphql-core/build/theme';

import { css } from '@emotion/css';
import { ThemeRegistryService } from '../../services';
import createEmotion, { Emotion } from '@emotion/css/create-instance';
import { NzConfigService } from 'ng-zorro-antd/core/config';

@Directive({
Expand All @@ -18,7 +11,9 @@ export class ThemeDirective implements OnInit, OnChanges {
@Input() appTheme: ICustomTheme = {};
@Input() appDarkTheme: ICustomTheme = {};
@Input() appAccentColor = '';
@Input() cspNonce = '';

private emotionInstance?: Emotion;
private className = '';

constructor(private nzConfigService: NzConfigService) {}
Expand Down Expand Up @@ -46,7 +41,9 @@ export class ThemeDirective implements OnInit, OnChanges {
appDarkTheme?: ICustomTheme,
accentColor?: string
) {
return css(getCSS(appTheme, appDarkTheme, accentColor));
return this.getEmotionInstance().css(
getCSS(appTheme, appDarkTheme, accentColor)
);
}

applyTheme(theme: ICustomTheme, darkTheme?: ICustomTheme, accentColor?: string) {
Expand All @@ -71,4 +68,14 @@ export class ThemeDirective implements OnInit, OnChanges {
this.className = this.getDynamicClassName(appTheme, appDarkTheme, accentColor);
document.documentElement.classList.add(this.className);
}

private getEmotionInstance() {
if (!this.emotionInstance) {
this.emotionInstance = createEmotion({
key: 'altair-theme',
nonce: this.cspNonce,
});
}
return this.emotionInstance;
}
}
3 changes: 3 additions & 0 deletions packages/altair-core/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export class AltairConfig {
themes = ['light', 'dark', 'dracula', 'system'];
isTranslateMode = isTranslateMode;
isWebApp = (window as TODO).__ALTAIR_WEB_APP__;
cspNonce = '';
initialData = {
url: '',
subscriptionsEndpoint: '',
Expand Down Expand Up @@ -105,7 +106,9 @@ export class AltairConfig {
initialWindows = [],
disableAccount = false,
initialAuthorization,
cspNonce = 'change-me',
}: AltairConfigOptions = {}) {
this.cspNonce = cspNonce;
this.initialData.url =
(window as TODO).__ALTAIR_ENDPOINT_URL__ ?? endpointURL ?? '';
this.initialData.subscriptionsEndpoint =
Expand Down
5 changes: 5 additions & 0 deletions packages/altair-core/src/config/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,9 @@ export interface AltairConfigOptions extends AltairWindowOptions {
* Disable the account and remote syncing functionality
*/
disableAccount?: boolean;

/**
* CSP nonce value to be used in Altair
*/
cspNonce?: string;
}
1 change: 1 addition & 0 deletions packages/altair-static/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const optionsProperties: AltairConfigOptionsObject = {
persistedSettings: undefined,
initialName: undefined,
initialAuthorization: undefined,
cspNonce: undefined,
};
const allowedProperties = Object.keys(
optionsProperties
Expand Down
Loading