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

fix: CORS error while using iframes #2176

Merged
merged 1 commit into from
Mar 4, 2024
Merged

Conversation

Dhruwang
Copy link
Contributor

@Dhruwang Dhruwang commented Mar 4, 2024

What does this PR do?

Fixes iFrame issue #2172

The error is a result of the same-origin policy, a critical security mechanism in web browsers. This policy restricts how a document or script loaded from one origin can interact with resources from another origin. In our case, the script is trying to access a frame (window.top) that is not of the same origin.

For our onboarding it works well because the iframe in embedded in a parent with the same origin as of the onboarding survey (https://app.formbricks.com), but it throws an error when iframes are embedded on pages with origin different from https://app.formbricks.com

So to solve this we now use Window: postMessage(). The window.postMessage() method safely enables cross-origin communication between window objects; e.g. between a page and an iframe embedded within it.

Read about window.postMessage()

How should this be tested?

Testing this would require creating a survey locally (not on cloud), and embed it using iFrames on

  1. onboarding
  2. a random html page

Checklist

Required

  • Filled out the "How to test" section in this PR
  • Read How we Code at Formbricks
  • Self-reviewed my own code
  • Commented on my code in hard-to-understand bits
  • Ran pnpm build
  • Checked for warnings, there are none
  • Removed all console.logs
  • Merged the latest changes from main onto my branch with git pull origin main
  • My changes don't cause any responsiveness issues

Appreciated

  • If a UI change was made: Added a screen recording or screenshots to this PR
  • Updated the Formbricks Docs if changes were necessary

Copy link

vercel bot commented Mar 4, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Ignored Deployments
Name Status Preview Updated (UTC)
formbricks-cloud ⬜️ Ignored (Inspect) Mar 4, 2024 5:16am
formbricks-com ⬜️ Ignored (Inspect) Mar 4, 2024 5:16am

Copy link
Contributor

github-actions bot commented Mar 4, 2024

Thank you for following the naming conventions for pull request titles! 🙏

Copy link
Contributor

packages/surveys/src/components/general/Survey.tsx

Consider specifying the target origin in the postMessage call to ensure the message is only sent to the intended domain. This will prevent any potential data leaks if there are other iframes from different origins on the page.
Create Issue
See the diff
Checkout the fix

    // Replace "*" with the target origin
    window.parent.postMessage("formbricksSurveyCompleted", "https://target-origin.com");
git fetch origin && git checkout -b ReviewBot/The-c-j40obhs origin/ReviewBot/The-c-j40obhs

const event = new CustomEvent("formbricksSurveyCompleted", { detail: { surveyId: survey.id } });
window.top?.dispatchEvent(event);
// Post a message to the parent window indicating that the survey is completed.
window.parent.postMessage("formbricksSurveyCompleted", "*");
Copy link
Contributor

Choose a reason for hiding this comment

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

Replace '*' with the target origin in the postMessage call to ensure the message is only sent to the intended domain.

Suggested change
window.parent.postMessage("formbricksSurveyCompleted", "*");
window.parent.postMessage("formbricksSurveyCompleted", "https://target-origin.com");

Copy link
Member

@mattinannt mattinannt left a comment

Choose a reason for hiding this comment

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

@Dhruwang thanks for the PR; looks great 😊💪

@mattinannt mattinannt added this pull request to the merge queue Mar 4, 2024
Merged via the queue into main with commit 673832a Mar 4, 2024
18 checks passed
@mattinannt mattinannt deleted the event-listener-issue branch March 4, 2024 11:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants