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

[sitecore-jss-proxy][sitecore-jss-nextjs] Fix for getCSPHeader function #1972

Merged
merged 9 commits into from
Nov 20, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Our versioning strategy is as follows:
* `[sitecore-jss-angular]` Fix nested dynamic placeholders not being displayed in Pages ([#1947](https://github.com/Sitecore/jss/pull/1947))
* `[sitecore-jss-dev-tools]` getMetadata() now uses `npm query` command to get the list and exact versions of packages. this solution works for monorepo setups ([#1949](https://github.com/Sitecore/jss/pull/1949))
* `[templates/nextjs-sxa]` Fix an alignment issue where components using both `me-auto` and `ms-md-auto` classes resulted in inconsistent alignment of elements. ([#1946](https://github.com/Sitecore/jss/pull/1946)) ([#1950](https://github.com/Sitecore/jss/pull/1950)) ([#1955](https://github.com/Sitecore/jss/pull/1955))
* `[sitecore-jss-proxy]``[sitecore-jss-nextjs]` Fix for getCSPHeader so that it returns proper value for the CSP header when JSS_ALLOWED_ORIGINS lists multiple origins delimited with comma. ([#1972](https://github.com/Sitecore/jss/pull/1972))
* `[sitecore-jss-proxy]` Support Editing Host protection by handling OPTIONS preflight requests ([#1976](https://github.com/Sitecore/jss/pull/1976))

### 🎉 New Features & Improvements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,24 @@ describe('EditingRenderMiddleware', () => {
'__next_preview_data=6677889900; Path=/; SameSite=None; Secure',
]);
});

it('should set allowed origins when multiple allowed origins are provided in env variable', async () => {
process.env.JSS_ALLOWED_ORIGINS = 'https://allowed.com,https://anotherallowed.com';
const req = mockRequest(EE_BODY, query, 'GET');
const res = mockResponse();

const middleware = new EditingRenderMiddleware();
const handler = middleware.getHandler();

await handler(req, res);

expect(res.setHeader).to.have.been.calledWith(
'Content-Security-Policy',
`frame-ancestors 'self' https://allowed.com https://anotherallowed.com ${EDITING_ALLOWED_ORIGINS.join(
' '
)}`
);
});
});

describe('chromes handler', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,10 @@ export class MetadataHandler {
* @returns Content-Security-Policy header value
*/
getSCPHeader() {
return `frame-ancestors 'self' ${[getAllowedOriginsFromEnv(), ...EDITING_ALLOWED_ORIGINS].join(
' '
)}`;
return `frame-ancestors 'self' ${[
...getAllowedOriginsFromEnv(),
...EDITING_ALLOWED_ORIGINS,
].join(' ')}`;
}
}

Expand Down
26 changes: 26 additions & 0 deletions packages/sitecore-jss-proxy/src/middleware/editing/render.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
GraphQLEditingService,
LayoutKind,
RenderMetadataQueryParams,
EDITING_ALLOWED_ORIGINS,
} from '@sitecore-jss/sitecore-jss/editing';
import { EditingRenderEndpointOptions, getSCPHeader } from './render';
import { LayoutServiceData, LayoutServicePageState } from '@sitecore-jss/sitecore-jss/layout';
Expand Down Expand Up @@ -459,3 +460,28 @@ describe('editingRouter - /editing/render', () => {
.end(done);
});
});

describe('getSCPHeader', () => {
afterEach(() => {
delete process.env.JSS_ALLOWED_ORIGINS;
});

it('should return the value of the CSP header', () => {
process.env.JSS_ALLOWED_ORIGINS = 'https://pages-dev.sitecore-staging.cloud';
const expectedHeader = `frame-ancestors 'self' https://pages-dev.sitecore-staging.cloud ${EDITING_ALLOWED_ORIGINS.join(
' '
)}`;
const actualHeader = getSCPHeader();
expect(actualHeader).to.equal(expectedHeader);
});

it('should return the value of the CSP header when multiple origins are listed in JSS_ALLOWED_ORIGINS', () => {
process.env.JSS_ALLOWED_ORIGINS =
'https://pages-dev.sitecore-staging.cloud,https://pages-staging.sitecore-staging.cloud';
const expectedHeader = `frame-ancestors 'self' https://pages-dev.sitecore-staging.cloud https://pages-staging.sitecore-staging.cloud ${EDITING_ALLOWED_ORIGINS.join(
' '
)}`;
const actualHeader = getSCPHeader();
expect(actualHeader).to.equal(expectedHeader);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export const editingRenderMiddleware = (config: EditingRenderEndpointOptions) =>
* @returns {string} Content-Security-Policy header value
*/
export const getSCPHeader = () => {
return `frame-ancestors 'self' ${[getAllowedOriginsFromEnv(), ...EDITING_ALLOWED_ORIGINS].join(
return `frame-ancestors 'self' ${[...getAllowedOriginsFromEnv(), ...EDITING_ALLOWED_ORIGINS].join(
' '
)}`;
};
Expand Down