Skip to content

Commit 590a890

Browse files
IlyaBondarAlexander-Kezikdenys-kolomiitsev
authored
fix(chat): add security fixes/policies and fix stucking conversations wuth huge stages (Issue #4700, #4683, #4698) (#4719)
Co-authored-by: Alexander <98586297+Alexander-Kezik@users.noreply.github.com> Co-authored-by: Denys_Kolomiitsev <143205282+denys-kolomiitsev@users.noreply.github.com>
1 parent d7f2b38 commit 590a890

8 files changed

Lines changed: 3571 additions & 3749 deletions

File tree

apps/chat/next.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const nextConfig = {
7373
poweredByHeader: false,
7474
reactStrictMode: true,
7575
// @ts-ignore
76-
basePath: process.env.APP_BASE_PATH || '',
76+
basePath: process.env.NODE_ENV !== 'development' ? new BasePathResolver() : '',
7777

7878
async redirects() {
7979
return [

apps/chat/src/components/Chat/MessageStage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export const MessageStage = ({ stage }: Props) => {
113113
isOpened ? 'border-t border-secondary p-2' : 'h-0',
114114
)}
115115
>
116-
{stage.content && (
116+
{isOpened && stage.content && (
117117
<span className="inline-block overflow-auto">
118118
<ChatMDComponent
119119
isShowResponseLoader={false}

apps/chat/src/components/Chat/NotAllowedModel.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ import { UISelectors } from '@/src/store/selectors';
1414
import { ScrollDownButton } from '@/src/components/Common/ScrollDownButton';
1515

1616
const ICON_SIZE = 24;
17-
const BUTTON_CLASS_NAME =
18-
'underline underline-offset-2 hover:text-accent-primary transition-colors';
17+
const BUTTON_CLASS_NAME = 'underline underline-offset-2 transition-colors';
1918
const INTERNAL_CLICK_MARKER = '__INTERNAL_CLICK_ACTION_MARKER__';
2019

2120
interface NotAllowedModelProps {

apps/chat/src/middleware.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,18 @@ export function middleware(request: NextRequest) {
2121
process.env.ALLOWED_IFRAME_SOURCES,
2222
shouldIgnoreFrameOptions,
2323
);
24-
24+
const allowedScriptsSrc =
25+
`${process.env.ALLOWED_IFRAME_ORIGINS} ${process.env.ALLOWED_IFRAME_SOURCES}`.replace(
26+
"'self'",
27+
'',
28+
);
2529
const cspHeader = `
2630
object-src 'none';
2731
base-uri 'self';
28-
script-src ${process.env.ALLOWED_IFRAME_ORIGINS ?? "'self'"} ${process.env.ALLOWED_IFRAME_SOURCES ? process.env.ALLOWED_IFRAME_SOURCES.replace("'self'", '') : ''}
32+
script-src 'self' ${allowedScriptsSrc}
2933
https://cdn.jsdelivr.net/npm/monaco-editor@0.43.0/
3034
'nonce-${nonce}' 'wasm-unsafe-eval' ${isDev ? "'unsafe-eval'" : ''};
35+
worker-src 'self' blob:;
3136
${frameDirectives}
3237
`;
3338
// Replace newline characters and spaces

apps/chat/src/utils/server/headers-helpers.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export const cleanHeaderDirectives = (directives: string) =>
22
directives.replace(/\s{2,}/g, ' ').trim();
3+
34
/**
45
*
56
* @param frameAncestors sources for the 'frame-ancestors' directive from the 'process.env.ALLOWED_IFRAME_ORIGINS'
@@ -14,11 +15,13 @@ export const getFrameContentSecurityPolicyDirectives = (
1415
) => {
1516
const ancestorsDirective =
1617
frameAncestors && !disabled
17-
? 'frame-ancestors ' + frameAncestors
18+
? `frame-ancestors 'self' ${frameAncestors.replace("'self'", '')}`
1819
: "frame-ancestors 'none'";
1920

2021
const frameSrcDirective =
21-
frameSrc && !disabled ? 'frame-src ' + frameSrc : "frame-src 'none'";
22+
frameSrc && !disabled
23+
? `frame-src 'self' ${frameSrc.replace("'self'", '')}`
24+
: "frame-src 'none'";
2225

2326
return `${ancestorsDirective}; ${frameSrcDirective};`;
2427
};

0 commit comments

Comments
 (0)