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

CONSOLE-4430: Content Security Policy E2E testing with Puppeteer & Chrome #14675

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

vojtechszocs
Copy link
Contributor

This PR adds a script to allow automated testing of Console application for CSP violations.

Console currently implements CSP via Content-Security-Policy-Report-Only HTTP response header, reporting any CSP violations detected in production env. via SecurityPolicyViolationEvent handler to telemetry service.

Summary of changes

  • Use Puppeteer to install and manage a local Chrome for Testing browser instance. All files related to Puppeteer are placed under frontend/.puppeteer directory. Chrome browser is installed only when necessary, i.e. when running yarn test-puppeteer-csp script, so the typical yarn install dependency update flow is not affected.
  • Run yarn test-puppeteer-csp in frontend directory to launch the CSP test script. This script assumes that Console Bridge server is already running locally and will test http://localhost:9000/dashboards page for CSP violations using Chrome DevTools Protocol (CDP).

General test flow

  1. Use CDP to spy on page request (resourceType = Document). Modify the request by adding custom Test-CSP-Reporting-Endpoint header, instructing the server to use the given CSP reporting endpoint for testing purposes and then resume the modified request.
  2. Use CDP to spy on potential CSP violation report requests (resourceType = CSPViolationReport). Upon receiving such request, report the error and then fulfill the request with 200 status code (i.e. avoid the need to run a server that actually handles the requests for CSP violation reports).
  3. Load page and wait until there is no network activity for at least 2s. Treat non-OK HTTP status code as error.

As a follow-up, we should add a CI job that invokes test-csp.sh as the entry point.

@vojtechszocs vojtechszocs changed the title Content Security Policy E2E testing with Puppeteer & Chrome CONSOLE-4430: Content Security Policy E2E testing with Puppeteer & Chrome Jan 9, 2025
@openshift-ci-robot openshift-ci-robot added the jira/valid-reference Indicates that this PR references a valid Jira ticket of any type. label Jan 9, 2025
@openshift-ci-robot
Copy link
Contributor

openshift-ci-robot commented Jan 9, 2025

@vojtechszocs: This pull request references CONSOLE-4430 which is a valid jira issue.

Warning: The referenced jira issue has an invalid target version for the target branch this PR targets: expected the story to target the "4.19.0" version, but no target version was set.

In response to this:

This PR adds a script to allow automated testing of Console application for CSP violations.

Console currently implements CSP via Content-Security-Policy-Report-Only HTTP response header, reporting any CSP violations detected in production env. via SecurityPolicyViolationEvent handler to telemetry service.

Summary of changes

  • Use Puppeteer to install and manage a local Chrome for Testing browser instance. All files related to Puppeteer are placed under frontend/.puppeteer directory. Chrome browser is installed only when necessary, i.e. when running yarn test-puppeteer-csp script, so the typical yarn install dependency update flow is not affected.
  • Run yarn test-puppeteer-csp in frontend directory to launch the CSP test script. This script assumes that Console Bridge server is already running locally and will test http://localhost:9000/dashboards page for CSP violations using Chrome DevTools Protocol (CDP).

General test flow

  1. Use CDP to spy on page request (resourceType = Document). Modify the request by adding custom Test-CSP-Reporting-Endpoint header, instructing the server to use the given CSP reporting endpoint for testing purposes and then resume the modified request.
  2. Use CDP to spy on potential CSP violation report requests (resourceType = CSPViolationReport). Upon receiving such request, report the error and then fulfill the request with 200 status code (i.e. avoid the need to run a server that actually handles the requests for CSP violation reports).
  3. Load page and wait until there is no network activity for at least 2s. Treat non-OK HTTP status code as error.

As a follow-up, we should add a CI job that invokes test-csp.sh as the entry point.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository.

@openshift-ci openshift-ci bot requested review from kyoto and rhamilto January 9, 2025 17:13
@openshift-ci openshift-ci bot added the component/backend Related to backend label Jan 9, 2025
Copy link
Contributor

openshift-ci bot commented Jan 9, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: vojtechszocs
Once this PR has been reviewed and has the lgtm label, please assign spadgett for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@vojtechszocs vojtechszocs force-pushed the csp-e2e-testing-with-puppeteer branch from 42563ab to 4f210a5 Compare January 9, 2025 18:26
@vojtechszocs vojtechszocs force-pushed the csp-e2e-testing-with-puppeteer branch from 4f210a5 to aa52aeb Compare January 9, 2025 18:31
@vojtechszocs
Copy link
Contributor Author

/retest

Copy link
Contributor

openshift-ci bot commented Jan 10, 2025

@vojtechszocs: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
ci/prow/e2e-gcp-console aa52aeb link true /test e2e-gcp-console

Full PR test history. Your PR dashboard.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

Copy link
Member

@jhadvig jhadvig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @vojtechszocs for the PR. Adding couple of comments.

# Disable color codes in Cypress since they do not render well CI test logs.
# https://docs.cypress.io/guides/guides/continuous-integration.html#Colors
if [ "$OPENSHIFT_CI" = true ]; then
export NO_COLOR=1
fi

if [ "$(basename "$(pwd)")" != "frontend" ]; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that the change is actually not changing the logic, so not sure why the block got moved?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The block got moved so that OPENSHIFT_CI env. variable handling is located together.

Technically, this is init/config

if [ "$OPENSHIFT_CI" = true ]; then
  export NO_COLOR=1
fi

while this is validation

if [ "$(basename "$(pwd)")" != "frontend" ]; then
  echo "This script must be run from the frontend folder"
  exit 1
fi

and I think init/config should logically come before validation.

});

// Handle network requests that get paused through Fetch.enable command.
cdpSession.on('Fetch.requestPaused', ({ resourceType, request, requestId }) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets add more comments explaining non-obvious logic, especially for the Chrome DevTools Protocol configuration (Fetch.enable, Fetch.requestPaused).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK will do.

const headers = Object.entries(request.headers).map(([name, value]) => ({ name, value }));

headers.push({ name: 'Test-CSP-Reporting-Endpoint', value: cspReportURL });
cdpSession.send('Fetch.continueRequest', { requestId, headers });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cdpSession.send('Fetch.continueRequest', { requestId, headers });
await cdpSession.send('Fetch.continueRequest', { requestId, headers });

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have purposely omitted the await operator here.

CDP session emitted events are async in nature. This line is the last bit of code of an if block:

if (resourceType === 'Document' && request.url === pageURL) {
  const headers = Object.entries(request.headers).map(([name, value]) => ({ name, value }));

  headers.push({ name: 'Test-CSP-Reporting-Endpoint', value: cspReportURL });
  cdpSession.send('Fetch.continueRequest', { requestId, headers });
}

It makes very little sense to await the result of Fetch.continueRequest command here.

We simply call the command and let it run. We don't need to handle its result, nor do we need to wait for its completion.

console.error('CSP violation detected', request.postData);
errorCallback();

cdpSession.send('Fetch.fulfillRequest', { requestId, responseCode: 200 });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cdpSession.send('Fetch.fulfillRequest', { requestId, responseCode: 200 });
await cdpSession.send('Fetch.fulfillRequest', { requestId, responseCode: 200 });

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is effectively the same thing as #14675 (comment)


await testPage(
browser,
'http://localhost:9000/dashboards',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this param become eventually an array ?
asking so we keep in mind that we reuse the Browser's instance, without launching a new browser instance every time

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was originally an array of URLs that contains just this one URL.

When we come to a point where we want to test multiple pages, we can make it into an array.

The browser instance should be reused for all testPage function calls, as you mentioned.

The reason for a singular testPage function call is that different pages may need a custom pageLoadCallback beyond the standard waitForNetworkIdle - for example, when testing the dashboard page, wait for "Cluster API address" to be displayed. This is not currently implemented, we have discussed that this could be done as a follow-up to this PR.

(async () => {
let errorsDetected = false;

const cspReportURL = 'http://localhost:7777/';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid hardcoding URLs like http://localhost:9000/ or http://localhost:7777/.
Instead I would pass them as parameters or retrieve them from configuration/environment variables.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can change the code to work with env. variables that use localhost fallbacks.

The CI job could pass these env. variables, while developers running yarn test-puppeteer-csp could use fallbacks.

What do you think?

})().catch((e) => {
console.error(e);
process.exit(1);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets move closing the browser into finally block

Suggested change
});
finally {
await browser.close();
}

Copy link
Contributor Author

@vojtechszocs vojtechszocs Jan 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Top-level Node.js async code should be wrapped in an immediately-invoked async function expression (IIAFE).

Are you suggesting we should wrap the whole IIAFE body into a try/catch block?

(async () => {
  try {
    // ...
  } catch (e) {
    // ...
  }
})();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/backend Related to backend jira/valid-reference Indicates that this PR references a valid Jira ticket of any type.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants