Skip to content

Make default iframe sandbox only require :blob csp permission #1290

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 17 commits 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
5 changes: 5 additions & 0 deletions .changeset/early-comics-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-signals-runtime': minor
---

Update ProcessSignal type for experimental "constants" object
5 changes: 5 additions & 0 deletions .changeset/three-oranges-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-signals': minor
---

Update iframe sandbox so CSP only requires :blob permission
17 changes: 15 additions & 2 deletions packages/signals/signals-example/src/lib/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
// You only want to instantiate SignalsPlugin in a browser context, otherwise you'll get an error.

import { AnalyticsBrowser } from '@segment/analytics-next'
import { SignalsPlugin, ProcessSignal } from '@segment/analytics-signals'
import {
SignalsPlugin,
SignalsPluginSettingsConfig,
ProcessSignal,
} from '@segment/analytics-signals'

export const analytics = new AnalyticsBrowser()
if (!process.env.WRITEKEY) {
Expand All @@ -29,11 +33,20 @@ const processSignalExample: ProcessSignal = (
}
}

const getQueryParams = () => {
const params = new URLSearchParams()
const sandboxStrategy = params.get('sandboxStrategy')
return {
sandboxStrategy:
sandboxStrategy as SignalsPluginSettingsConfig['sandboxStrategy'],
}
}
const isStage = process.env.STAGE === 'true'

const queryParams = getQueryParams()
const signalsPlugin = new SignalsPlugin({
...(isStage ? { apiHost: 'signals.segment.build/v1' } : {}),
sandboxStrategy: 'global',
sandboxStrategy: queryParams.sandboxStrategy ?? 'iframe',
// processSignal: processSignalExample,
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { waitForCondition } from '../../helpers/playwright-utils'
import { IndexPage } from './index-page'
import type { SegmentEvent } from '@segment/analytics-next'

const basicEdgeFn = `globalThis.processSignal = (signal) => {}`
const basicEdgeFn = `function processSignal(signal) {}`

test('Collecting signals whenever a user selects an item', async ({ page }) => {
const indexPage = await new IndexPage().loadAndWait(page, basicEdgeFn, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { test, expect } from '@playwright/test'
import { waitForCondition } from '../../helpers/playwright-utils'
import { IndexPage } from './index-page'

const basicEdgeFn = `globalThis.processSignal = (signal) => {}`
const basicEdgeFn = `function processSignal(signal) {}`

test('Collecting signals whenever a user enters text input and focuses out', async ({
page,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ declare global {

const basicEdgeFn = `
// this is a process signal function
globalThis.processSignal = (signal) => {
function processSignal(signal) {
if (signal.type === 'interaction') {
const eventName = signal.data.eventType + ' ' + '[' + signal.type + ']'
analytics.track(eventName, signal.data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { SegmentEvent } from '@segment/analytics-next'
/**
* This test ensures that
*/
const indexPage = new IndexPage()

const normalizeSnapshotEvent = (el: SegmentEvent) => {
return {
Expand Down Expand Up @@ -35,9 +34,10 @@ const snapshot = (
).map(normalizeSnapshotEvent)

test('Segment events', async ({ page }) => {
const indexPage = new IndexPage()
const basicEdgeFn = `
// this is a process signal function
globalThis.processSignal = (signal) => {
function processSignal(signal) {
if (signal.type === 'interaction' && signal.data.eventType === 'click') {
analytics.identify('john', { found: true })
analytics.group('foo', { hello: 'world' })
Expand All @@ -64,8 +64,9 @@ test('Segment events', async ({ page }) => {
test('Should dispatch events from signals that occurred before analytics was instantiated', async ({
page,
}) => {
const indexPage = new IndexPage()
const edgeFn = `
globalThis.processSignal = (signal) => {
function processSignal(signal) {
if (signal.type === 'navigation' && signal.data.action === 'pageLoad') {
analytics.page('dispatched from signals - navigation')
}
Expand All @@ -76,23 +77,25 @@ test('Should dispatch events from signals that occurred before analytics was ins

await indexPage.load(page, edgeFn)
const flush = Promise.all([
indexPage.addUserDefinedSignal(),
indexPage.waitForSignalsApiFlush(),
indexPage.waitForTrackingApiFlush(),
])

// add a user defined signal before analytics is instantiated
void indexPage.addUserDefinedSignal()
await flush

const trackingApiReqs = indexPage.trackingAPI.getEvents()
expect(trackingApiReqs).toHaveLength(2)

const pageEvents = trackingApiReqs.find((el) => el.type === 'page')!

expect(pageEvents).toBeTruthy()
expect(pageEvents.name).toEqual('dispatched from signals - navigation')

const userDefinedEvents = trackingApiReqs.find((el) => el.type === 'track')!
expect(userDefinedEvents).toBeTruthy()
if (!userDefinedEvents) {
console.warn('invariant', trackingApiReqs)
}
expect(userDefinedEvents.event).toEqual(
'dispatched from signals - userDefined'
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IndexPage } from './index-page'

const basicEdgeFn = `
// this is a process signal function
globalThis.processSignal = (signal) => {
function processSignal(signal) {
if (signal.type === 'interaction') {
const eventName = signal.data.eventType + ' ' + '[' + signal.type + ']'
analytics.track(eventName, signal.data)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { test, expect } from '@playwright/test'
import { IndexPage } from './index-page'

const basicEdgeFn = `globalThis.processSignal = (signal) => {}`
const basicEdgeFn = `function processSignal(signal) {}`
let indexPage: IndexPage
test.beforeEach(async ({ page }) => {
indexPage = await new IndexPage().loadAndWait(page, basicEdgeFn)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { test, expect } from '@playwright/test'
import { waitForCondition } from '../../helpers/playwright-utils'
import { IndexPage } from './index-page'

const basicEdgeFn = `globalThis.processSignal = (signal) => {}`
const basicEdgeFn = `function processSignal(signal) {}`

test('Collecting signals whenever a user enters text input', async ({
page,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { test, expect } from '@playwright/test'
import { IndexPage } from './index-page'

const basicEdgeFn = `globalThis.processSignal = (signal) => {}`
const basicEdgeFn = `function processSignal(signal) {}`

let indexPage: IndexPage

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { test, expect } from '@playwright/test'
import { IndexPage } from './index-page'

const basicEdgeFn = `globalThis.processSignal = (signal) => {}`
const basicEdgeFn = `function processSignal(signal) {}`

test('network signals allow and disallow list', async ({ page }) => {
const indexPage = await new IndexPage().loadAndWait(page, basicEdgeFn, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { test, expect } from '@playwright/test'
import { commonSignalData } from '../../helpers/fixtures'
import { IndexPage } from './index-page'

const basicEdgeFn = `globalThis.processSignal = (signal) => {}`
const basicEdgeFn = `function processSignal(signal) {}`

test.describe('network signals - fetch', () => {
let indexPage: IndexPage
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { test, expect } from '@playwright/test'
import { IndexPage } from './index-page'

const basicEdgeFn = `globalThis.processSignal = (signal) => {}`
const basicEdgeFn = `function processSignal(signal) {}`

test.describe('network signals - XHR', () => {
let indexPage: IndexPage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { pTimeout } from '@segment/analytics-core'
* If a signal is generated, the signal buffer should be reset
* when the user clicks on the complex button.
*/
const edgeFn = `globalThis.processSignal = (signal) => {
const edgeFn = `function processSignal(signal) {
// create a custom signal to echo out the current signal buffer
if (signal.type === 'userDefined') {
analytics.track('current signal buffer', { signalBuffer: signals.signalBuffer })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { test, expect } from '@playwright/test'
import { IndexPage } from './index-page'

const basicEdgeFn = `
globalThis.processSignal = (signal) => {
function processSignal(signal) {
// test that constants are properly injected
if (typeof EventType !== 'object') {
throw new Error('EventType is missing?')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IndexPage } from './index-page'
const indexPage = new IndexPage()

test('should find the most recent signal', async ({ page }) => {
const basicEdgeFn = `globalThis.processSignal = (signal) => {
const basicEdgeFn = `function processSignal(signal) {
if (signal.type === 'interaction' && signal.data.target.id === 'complex-button') {
const mostRecentSignal = signals.find(signal, 'userDefined')
if (mostRecentSignal.data.num === 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { waitForCondition } from '../../helpers/playwright-utils'

const indexPage = new IndexPage()

const basicEdgeFn = `globalThis.processSignal = (signal) => {}`
const basicEdgeFn = `function processSignal(signal) {}`

test('debug ingestion disabled and sample rate 0 -> will not send the signal', async ({
page,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { test, expect } from '@playwright/test'
import { waitForCondition } from '../../helpers/playwright-utils'
import { IndexPage } from './index-page'

const basicEdgeFn = `globalThis.processSignal = (signal) => {}`
const basicEdgeFn = `function processSignal(signal) {}`

test('redaction enabled -> will XXX the value of text input', async ({
page,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IndexPage } from './index-page'

const basicEdgeFn = `
// this is a process signal function
globalThis.processSignal = (signal) => {
function processSignal(signal) {
if (signal.type === 'interaction') {
analytics.track('hello', { myAnonId: signal.anonymousId, myTimestamp: signal.timestamp })
}
Expand Down
1 change: 0 additions & 1 deletion packages/signals/signals-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"build:esm": "yarn tsc -p tsconfig.build.json",
"build:cjs": "yarn tsc -p tsconfig.build.json --outDir ./dist/cjs --module commonjs",
"build:global": "node build-signals-runtime-global.js",
"assert-generated": "bash scripts/assert-generated.sh",
"watch": "rm -rf dist/esm && yarn build:esm && yarn build:esm --watch",
"watch:test": "yarn test --watch",
"tsc": "yarn run -T tsc",
Expand Down
17 changes: 0 additions & 17 deletions packages/signals/signals-runtime/scripts/assert-generated.sh

This file was deleted.

6 changes: 2 additions & 4 deletions packages/signals/signals/.lintstagedrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module.exports = {
...require("@internal/config").lintStagedConfig,
'src/lib/workerbox/*.{js,ts,html}': ['yarn workerbox']
}
module.exports = require("@internal/config").lintStagedConfig


2 changes: 0 additions & 2 deletions packages/signals/signals/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
"build:esm": "yarn tsc -p tsconfig.build.json",
"build:cjs": "yarn tsc -p tsconfig.build.json --outDir ./dist/cjs --module commonjs",
"build:bundle": "NODE_ENV=production yarn run webpack",
"workerbox": "node scripts/build-workerbox.js",
"assert-generated": "sh scripts/assert-workerbox-built.sh",
"watch": "rm -rf dist && yarn concurrently 'yarn build:bundle --watch' 'yarn build:esm --watch'",
"version": "sh scripts/version.sh",
"watch:test": "yarn test --watch",
Expand Down
16 changes: 0 additions & 16 deletions packages/signals/signals/scripts/assert-workerbox-built.sh

This file was deleted.

64 changes: 0 additions & 64 deletions packages/signals/signals/scripts/build-workerbox.js

This file was deleted.

Loading