playwriter 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bippy.js +5 -5
- package/dist/cdp-log.d.ts +4 -1
- package/dist/cdp-log.d.ts.map +1 -1
- package/dist/cdp-log.js +39 -2
- package/dist/cdp-log.js.map +1 -1
- package/dist/cdp-log.test.d.ts +2 -0
- package/dist/cdp-log.test.d.ts.map +1 -0
- package/dist/cdp-log.test.js +109 -0
- package/dist/cdp-log.test.js.map +1 -0
- package/dist/cdp-relay.d.ts.map +1 -1
- package/dist/cdp-relay.js +120 -11
- package/dist/cdp-relay.js.map +1 -1
- package/dist/cli-help.test.js +22 -0
- package/dist/cli-help.test.js.map +1 -1
- package/dist/cli.js +69 -25
- package/dist/cli.js.map +1 -1
- package/dist/executor.d.ts +4 -0
- package/dist/executor.d.ts.map +1 -1
- package/dist/executor.js +140 -33
- package/dist/executor.js.map +1 -1
- package/dist/extension/background.js +343 -62
- package/dist/extension/manifest.json +1 -1
- package/dist/mcp.d.ts.map +1 -1
- package/dist/mcp.js +6 -1
- package/dist/mcp.js.map +1 -1
- package/dist/performance-examples.d.ts +5 -0
- package/dist/performance-examples.d.ts.map +1 -0
- package/dist/performance-examples.js +112 -0
- package/dist/performance-examples.js.map +1 -0
- package/dist/performance-profiling.md +417 -0
- package/dist/prompt.md +51 -18
- package/dist/react-source.d.ts +44 -0
- package/dist/react-source.d.ts.map +1 -1
- package/dist/react-source.js +207 -20
- package/dist/react-source.js.map +1 -1
- package/dist/readability.js +1 -1
- package/dist/relay-client.d.ts +11 -0
- package/dist/relay-client.d.ts.map +1 -1
- package/dist/relay-client.js +46 -1
- package/dist/relay-client.js.map +1 -1
- package/dist/relay-core.test.js +10 -6
- package/dist/relay-core.test.js.map +1 -1
- package/dist/relay-session.test.js +43 -7
- package/dist/relay-session.test.js.map +1 -1
- package/dist/relay-state.test.js +57 -1
- package/dist/relay-state.test.js.map +1 -1
- package/dist/screen-recording.d.ts.map +1 -1
- package/dist/screen-recording.js +19 -4
- package/dist/screen-recording.js.map +1 -1
- package/dist/selector-generator.js +1 -1
- package/dist/start-relay-server.d.ts +1 -1
- package/dist/start-relay-server.d.ts.map +1 -1
- package/dist/start-relay-server.js +23 -1
- package/dist/start-relay-server.js.map +1 -1
- package/dist/utils.d.ts +2 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +4 -1
- package/dist/utils.js.map +1 -1
- package/package.json +3 -3
- package/src/cdp-log.test.ts +131 -0
- package/src/cdp-log.ts +44 -2
- package/src/cdp-relay.ts +127 -10
- package/src/cli-help.test.ts +22 -0
- package/src/cli.ts +74 -24
- package/src/executor.ts +166 -39
- package/src/mcp.ts +6 -1
- package/src/performance-examples.ts +186 -0
- package/src/react-source.ts +310 -24
- package/src/relay-client.ts +62 -5
- package/src/relay-core.test.ts +10 -6
- package/src/relay-session.test.ts +45 -11
- package/src/relay-state.test.ts +67 -1
- package/src/screen-recording.ts +20 -4
- package/src/skill.md +62 -19
- package/src/start-relay-server.ts +22 -1
- package/src/utils.ts +5 -0
package/dist/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAA;AACxB,OAAO,EAAE,MAAM,SAAS,CAAA;AACxB,OAAO,IAAI,MAAM,WAAW,CAAA;AAC5B,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAExC,0EAA0E;AAC1E,MAAM,CAAC,MAAM,aAAa,GAAG;IAC3B,kCAAkC,EAAE,gCAAgC;IACpE,kCAAkC,EAAE,8CAA8C;CACnF,CAAA;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,cAAc,CAAC,IAAY,EAAE,OAAe,KAAK;IAC/D,IAAI,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,IAAI,IAAI,CAAC,UAAU,CAAC,SA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IAAI,EAAE,IAAI,CAAC,CAAA;IAChD,OAAO,GAAG,SAAS,QAAQ,EAAE,GAAG,MAAM,EAAE,CAAA;AAC1C,CAAC;AAED,uHAAuH;AACvH,MAAM,YAAY,GAAG,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,EAAE,aAAa,CAAC,CAAA;AAC3D,MAAM,CAAC,MAAM,aAAa,GAAG,OAAO,CAAC,GAAG,CAAC,wBAAwB,IAAI,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE,kBAAkB,CAAC,CAAA;AAChH,MAAM,CAAC,MAAM,iBAAiB,GAC5B,OAAO,CAAC,GAAG,CAAC,4BAA4B,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,EAAE,WAAW,CAAC,CAAA;AAEjG,MAAM,eAAe,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,cAAc,CAAC,CAAA;AACrG,MAAM,CAAC,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,YAAY,CAAC,eAAe,EAAE,OAAO,CAAC,CAAC,CAAC,OAAiB,CAAA;AAE9F,MAAM,UAAU,KAAK,CAAC,EAAU;IAC9B,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAA;AAC1D,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "playwriter",
|
|
3
3
|
"description": "",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.3.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"async-sema": "^3.1.1",
|
|
41
41
|
"diff": "^8.0.2",
|
|
42
42
|
"dom-accessibility-api": "^0.7.1",
|
|
43
|
-
"goke": "^6.
|
|
43
|
+
"goke": "^6.12.1",
|
|
44
44
|
"hono": "^4.12.12",
|
|
45
45
|
"picocolors": "^1.1.1",
|
|
46
46
|
"posthtml": "^0.16.7",
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"build": "rm -rf dist *.tsbuildinfo && mkdir dist && bun scripts/build-client-bundles.ts && tsc && bun scripts/build-extension-bundle.ts && bun scripts/build-resources.ts",
|
|
60
60
|
"watch": "tsc -w",
|
|
61
61
|
"cli": "vite-node src/cli.ts",
|
|
62
|
-
"typecheck": "tsc
|
|
62
|
+
"typecheck": "tsc",
|
|
63
63
|
"mcp": "vite-node src/mcp.ts",
|
|
64
64
|
"test": "vitest run -u",
|
|
65
65
|
"test:watch": "vitest"
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
import fs from 'node:fs'
|
|
3
|
+
import path from 'node:path'
|
|
4
|
+
import os from 'node:os'
|
|
5
|
+
import { createCdpLogger, type CdpLogEntry } from './cdp-log.js'
|
|
6
|
+
|
|
7
|
+
function makeTmpDir() {
|
|
8
|
+
return fs.mkdtempSync(path.join(os.tmpdir(), 'cdp-log-test-'))
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function makeEntry(i: number): CdpLogEntry {
|
|
12
|
+
return {
|
|
13
|
+
timestamp: new Date().toISOString(),
|
|
14
|
+
direction: 'from-extension',
|
|
15
|
+
message: { method: `Test.method${i}`, id: i },
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function readIds(logFile: string): number[] {
|
|
20
|
+
return fs
|
|
21
|
+
.readFileSync(logFile, 'utf-8')
|
|
22
|
+
.trim()
|
|
23
|
+
.split('\n')
|
|
24
|
+
.filter((l) => {
|
|
25
|
+
return l.length > 0
|
|
26
|
+
})
|
|
27
|
+
.map((l) => {
|
|
28
|
+
return JSON.parse(l).message.id as number
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
describe('CDP log rotation', () => {
|
|
33
|
+
it('rotates when lineCount exceeds maxEntries, keeping last half', async () => {
|
|
34
|
+
const tmpDir = makeTmpDir()
|
|
35
|
+
const logFile = path.join(tmpDir, 'cdp.jsonl')
|
|
36
|
+
const logger = createCdpLogger({ logFilePath: logFile, maxEntries: 20 })
|
|
37
|
+
|
|
38
|
+
// Write 25 entries to trigger rotation (threshold is 20)
|
|
39
|
+
for (let i = 0; i < 25; i++) {
|
|
40
|
+
logger.log(makeEntry(i))
|
|
41
|
+
}
|
|
42
|
+
await logger.flush()
|
|
43
|
+
|
|
44
|
+
const ids = readIds(logFile)
|
|
45
|
+
|
|
46
|
+
// Rotation triggers after entry 20 is written (lineCount becomes 21 > 20).
|
|
47
|
+
// It keeps last 10 (entries 11-20), then entries 21-24 are appended.
|
|
48
|
+
expect(ids).toMatchInlineSnapshot(`
|
|
49
|
+
[
|
|
50
|
+
11,
|
|
51
|
+
12,
|
|
52
|
+
13,
|
|
53
|
+
14,
|
|
54
|
+
15,
|
|
55
|
+
16,
|
|
56
|
+
17,
|
|
57
|
+
18,
|
|
58
|
+
19,
|
|
59
|
+
20,
|
|
60
|
+
21,
|
|
61
|
+
22,
|
|
62
|
+
23,
|
|
63
|
+
24,
|
|
64
|
+
]
|
|
65
|
+
`)
|
|
66
|
+
|
|
67
|
+
fs.rmSync(tmpDir, { recursive: true })
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
it('does not rotate when under maxEntries', async () => {
|
|
71
|
+
const tmpDir = makeTmpDir()
|
|
72
|
+
const logFile = path.join(tmpDir, 'cdp.jsonl')
|
|
73
|
+
const logger = createCdpLogger({ logFilePath: logFile, maxEntries: 50 })
|
|
74
|
+
|
|
75
|
+
for (let i = 0; i < 30; i++) {
|
|
76
|
+
logger.log(makeEntry(i))
|
|
77
|
+
}
|
|
78
|
+
await logger.flush()
|
|
79
|
+
|
|
80
|
+
const ids = readIds(logFile)
|
|
81
|
+
expect(ids.length).toBe(30)
|
|
82
|
+
expect(ids[0]).toBe(0)
|
|
83
|
+
expect(ids[29]).toBe(29)
|
|
84
|
+
|
|
85
|
+
fs.rmSync(tmpDir, { recursive: true })
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
it('handles multiple rotations', async () => {
|
|
89
|
+
const tmpDir = makeTmpDir()
|
|
90
|
+
const logFile = path.join(tmpDir, 'cdp.jsonl')
|
|
91
|
+
const logger = createCdpLogger({ logFilePath: logFile, maxEntries: 10 })
|
|
92
|
+
|
|
93
|
+
// Write 35 entries, should trigger multiple rotations
|
|
94
|
+
for (let i = 0; i < 35; i++) {
|
|
95
|
+
logger.log(makeEntry(i))
|
|
96
|
+
}
|
|
97
|
+
await logger.flush()
|
|
98
|
+
|
|
99
|
+
const ids = readIds(logFile)
|
|
100
|
+
|
|
101
|
+
// File should never exceed maxEntries
|
|
102
|
+
expect(ids.length).toBeLessThanOrEqual(15)
|
|
103
|
+
expect(ids.length).toBeGreaterThanOrEqual(5)
|
|
104
|
+
|
|
105
|
+
// Last entry should always be the most recent
|
|
106
|
+
expect(ids[ids.length - 1]).toBe(34)
|
|
107
|
+
// No entries from the very beginning should survive multiple rotations
|
|
108
|
+
expect(ids[0]).toBeGreaterThan(10)
|
|
109
|
+
|
|
110
|
+
fs.rmSync(tmpDir, { recursive: true })
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
it('uses atomic rename for rotation', async () => {
|
|
114
|
+
const tmpDir = makeTmpDir()
|
|
115
|
+
const logFile = path.join(tmpDir, 'cdp.jsonl')
|
|
116
|
+
const logger = createCdpLogger({ logFilePath: logFile, maxEntries: 10 })
|
|
117
|
+
|
|
118
|
+
for (let i = 0; i < 15; i++) {
|
|
119
|
+
logger.log(makeEntry(i))
|
|
120
|
+
}
|
|
121
|
+
await logger.flush()
|
|
122
|
+
|
|
123
|
+
// Temp file should not remain after successful rotation
|
|
124
|
+
expect(fs.existsSync(`${logFile}.tmp`)).toBe(false)
|
|
125
|
+
|
|
126
|
+
const ids = readIds(logFile)
|
|
127
|
+
expect(ids[ids.length - 1]).toBe(14)
|
|
128
|
+
|
|
129
|
+
fs.rmSync(tmpDir, { recursive: true })
|
|
130
|
+
})
|
|
131
|
+
})
|
package/src/cdp-log.ts
CHANGED
|
@@ -12,6 +12,8 @@ export type CdpLogEntry = {
|
|
|
12
12
|
|
|
13
13
|
export type CdpLogger = {
|
|
14
14
|
log(entry: CdpLogEntry): void
|
|
15
|
+
/** Wait for all pending writes (and any in-flight rotation) to complete */
|
|
16
|
+
flush(): Promise<void>
|
|
15
17
|
logFilePath: string
|
|
16
18
|
}
|
|
17
19
|
|
|
@@ -41,10 +43,20 @@ function createTruncatingReplacer({ maxStringLength }: { maxStringLength: number
|
|
|
41
43
|
}
|
|
42
44
|
}
|
|
43
45
|
|
|
46
|
+
const DEFAULT_MAX_ENTRIES = 10_000
|
|
47
|
+
|
|
48
|
+
function resolvePositiveInt(value: number | undefined, fallback: number): number {
|
|
49
|
+
if (value == null || !Number.isFinite(value) || value < 2) {
|
|
50
|
+
return fallback
|
|
51
|
+
}
|
|
52
|
+
return Math.floor(value)
|
|
53
|
+
}
|
|
54
|
+
|
|
44
55
|
export function createCdpLogger({
|
|
45
56
|
logFilePath,
|
|
46
57
|
maxStringLength,
|
|
47
|
-
|
|
58
|
+
maxEntries,
|
|
59
|
+
}: { logFilePath?: string; maxStringLength?: number; maxEntries?: number } = {}): CdpLogger {
|
|
48
60
|
const resolvedLogFilePath = logFilePath || LOG_CDP_FILE_PATH
|
|
49
61
|
const logDir = path.dirname(resolvedLogFilePath)
|
|
50
62
|
if (!fs.existsSync(logDir)) {
|
|
@@ -53,16 +65,46 @@ export function createCdpLogger({
|
|
|
53
65
|
fs.writeFileSync(resolvedLogFilePath, '')
|
|
54
66
|
|
|
55
67
|
let queue: Promise<void> = Promise.resolve()
|
|
68
|
+
let lineCount = 0
|
|
56
69
|
const maxLength = maxStringLength ?? DEFAULT_MAX_STRING_LENGTH
|
|
70
|
+
const envMaxEntries = Number(process.env.PLAYWRITER_CDP_LOG_MAX_ENTRIES)
|
|
71
|
+
const resolvedMaxEntries = resolvePositiveInt(maxEntries, resolvePositiveInt(envMaxEntries, DEFAULT_MAX_ENTRIES))
|
|
72
|
+
// Keep half the entries after rotation so we don't rotate on every write
|
|
73
|
+
const keepAfterRotation = Math.floor(resolvedMaxEntries / 2)
|
|
74
|
+
|
|
75
|
+
// Atomic rotation: write to temp file then rename to avoid corruption on crash
|
|
76
|
+
const rotate = async (): Promise<void> => {
|
|
77
|
+
try {
|
|
78
|
+
const content = await fs.promises.readFile(resolvedLogFilePath, 'utf-8')
|
|
79
|
+
const lines = content.split('\n').filter((l) => {
|
|
80
|
+
return l.length > 0
|
|
81
|
+
})
|
|
82
|
+
const kept = lines.slice(-keepAfterRotation)
|
|
83
|
+
const tmpPath = `${resolvedLogFilePath}.tmp`
|
|
84
|
+
await fs.promises.writeFile(tmpPath, kept.join('\n') + '\n')
|
|
85
|
+
await fs.promises.rename(tmpPath, resolvedLogFilePath)
|
|
86
|
+
lineCount = kept.length
|
|
87
|
+
} catch {
|
|
88
|
+
// If rotation fails (disk error, permissions), keep logging without rotation.
|
|
89
|
+
// lineCount stays high so rotation will be retried on next write.
|
|
90
|
+
}
|
|
91
|
+
}
|
|
57
92
|
|
|
58
93
|
const log = (entry: CdpLogEntry): void => {
|
|
59
94
|
const replacer = createTruncatingReplacer({ maxStringLength: maxLength })
|
|
60
95
|
const line = JSON.stringify(entry, replacer)
|
|
61
|
-
queue = queue.then(() =>
|
|
96
|
+
queue = queue.then(async () => {
|
|
97
|
+
await fs.promises.appendFile(resolvedLogFilePath, `${line}\n`)
|
|
98
|
+
lineCount++
|
|
99
|
+
if (lineCount > resolvedMaxEntries) {
|
|
100
|
+
await rotate()
|
|
101
|
+
}
|
|
102
|
+
})
|
|
62
103
|
}
|
|
63
104
|
|
|
64
105
|
return {
|
|
65
106
|
log,
|
|
107
|
+
flush: () => queue,
|
|
66
108
|
logFilePath: resolvedLogFilePath,
|
|
67
109
|
}
|
|
68
110
|
}
|
package/src/cdp-relay.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Hono } from 'hono'
|
|
2
2
|
import { cors } from 'hono/cors'
|
|
3
|
-
import {
|
|
3
|
+
import { createAdaptorServer } from '@hono/node-server'
|
|
4
4
|
import { getConnInfo } from '@hono/node-server/conninfo'
|
|
5
5
|
import { createNodeWebSocket } from '@hono/node-ws'
|
|
6
6
|
import type { WSContext } from 'hono/ws'
|
|
@@ -523,8 +523,9 @@ export async function startPlayWriterCDPRelayServer({
|
|
|
523
523
|
|
|
524
524
|
// Auto-create initial tab when PLAYWRITER_AUTO_ENABLE is set and no targets exist.
|
|
525
525
|
// This allows Playwright to connect and immediately have a page to work with.
|
|
526
|
-
async function maybeAutoCreateInitialTab(extensionId: string): Promise<void> {
|
|
527
|
-
|
|
526
|
+
async function maybeAutoCreateInitialTab(options: { extensionId: string; autoEnable: boolean }): Promise<void> {
|
|
527
|
+
const { extensionId, autoEnable } = options
|
|
528
|
+
if (!autoEnable && !process.env.PLAYWRITER_AUTO_ENABLE) {
|
|
528
529
|
return
|
|
529
530
|
}
|
|
530
531
|
const conn = getExtensionConnection(extensionId)
|
|
@@ -654,12 +655,14 @@ export async function startPlayWriterCDPRelayServer({
|
|
|
654
655
|
params,
|
|
655
656
|
sessionId,
|
|
656
657
|
source,
|
|
658
|
+
autoEnable,
|
|
657
659
|
}: {
|
|
658
660
|
extensionId: string | null
|
|
659
661
|
method: CDPCommand['method'] | (string & {})
|
|
660
662
|
params: CDPCommand['params']
|
|
661
663
|
sessionId?: CDPCommand['sessionId']
|
|
662
664
|
source?: CDPCommand['source']
|
|
665
|
+
autoEnable: boolean
|
|
663
666
|
}) {
|
|
664
667
|
const conn = getExtensionConnection(extensionId)
|
|
665
668
|
const connectedTargets = conn?.connectedTargets || new Map<string, relayState.ConnectedTarget>()
|
|
@@ -699,7 +702,7 @@ export async function startPlayWriterCDPRelayServer({
|
|
|
699
702
|
break
|
|
700
703
|
}
|
|
701
704
|
if (conn) {
|
|
702
|
-
await maybeAutoCreateInitialTab(conn.id)
|
|
705
|
+
await maybeAutoCreateInitialTab({ extensionId: conn.id, autoEnable })
|
|
703
706
|
}
|
|
704
707
|
// Forward auto-attach so Chrome emits iframe Target.attachedToTarget events.
|
|
705
708
|
// Playwright relies on these (with parentFrameId) when reconnecting over CDP.
|
|
@@ -856,6 +859,87 @@ export async function startPlayWriterCDPRelayServer({
|
|
|
856
859
|
allowMethods: ['GET', 'POST', 'HEAD', 'OPTIONS'],
|
|
857
860
|
}),
|
|
858
861
|
)
|
|
862
|
+
// Host header validation to prevent DNS rebinding attacks.
|
|
863
|
+
// DNS rebinding is worse than a simple cross-origin request: the attacker
|
|
864
|
+
// serves a page from http://evil.com:19988, then rebinds the DNS to
|
|
865
|
+
// 127.0.0.1. The browser now considers requests to our relay as same-origin,
|
|
866
|
+
// so Sec-Fetch-Site is "same-origin", CORS doesn't apply, and JSON POSTs
|
|
867
|
+
// don't need preflight. This bypasses all our other defenses.
|
|
868
|
+
// By rejecting any Host that isn't a known localhost value we kill DNS
|
|
869
|
+
// rebinding at the root. When a valid token is provided (remote access), we
|
|
870
|
+
// allow through regardless of Host since remote clients use real hostnames.
|
|
871
|
+
const ALLOWED_HOSTS = new Set([
|
|
872
|
+
'localhost',
|
|
873
|
+
'127.0.0.1',
|
|
874
|
+
'[::1]',
|
|
875
|
+
'::1',
|
|
876
|
+
])
|
|
877
|
+
|
|
878
|
+
// Parse the Host header into just the hostname, handling IPv6 brackets and
|
|
879
|
+
// port suffixes. Returns null for missing or malformed values.
|
|
880
|
+
function parseHostname(hostHeader: string | undefined): string | null {
|
|
881
|
+
const value = hostHeader?.trim().toLowerCase()
|
|
882
|
+
if (!value) {
|
|
883
|
+
return null
|
|
884
|
+
}
|
|
885
|
+
// IPv6 in brackets: [::1] or [::1]:19988
|
|
886
|
+
if (value.startsWith('[')) {
|
|
887
|
+
const closingBracket = value.indexOf(']')
|
|
888
|
+
if (closingBracket === -1) {
|
|
889
|
+
return null
|
|
890
|
+
}
|
|
891
|
+
const host = value.slice(0, closingBracket + 1)
|
|
892
|
+
const rest = value.slice(closingBracket + 1)
|
|
893
|
+
if (rest && !/^:\d+$/.test(rest)) {
|
|
894
|
+
return null
|
|
895
|
+
}
|
|
896
|
+
return host
|
|
897
|
+
}
|
|
898
|
+
// Bare ::1 without brackets (uncommon but possible)
|
|
899
|
+
if (value === '::1') {
|
|
900
|
+
return '::1'
|
|
901
|
+
}
|
|
902
|
+
// hostname or hostname:port
|
|
903
|
+
const colonIndex = value.indexOf(':')
|
|
904
|
+
if (colonIndex === -1) {
|
|
905
|
+
return value
|
|
906
|
+
}
|
|
907
|
+
const host = value.slice(0, colonIndex)
|
|
908
|
+
const portPart = value.slice(colonIndex + 1)
|
|
909
|
+
if (!/^\d+$/.test(portPart)) {
|
|
910
|
+
return null
|
|
911
|
+
}
|
|
912
|
+
return host || null
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
function hasValidToken(c: { req: { header: (name: string) => string | undefined; url: string } }): boolean {
|
|
916
|
+
if (!token) {
|
|
917
|
+
return false
|
|
918
|
+
}
|
|
919
|
+
const authHeader = c.req.header('authorization') || ''
|
|
920
|
+
const bearerToken = authHeader.startsWith('Bearer ') ? authHeader.slice(7) : null
|
|
921
|
+
const queryToken = new URL(c.req.url, 'http://localhost').searchParams.get('token')
|
|
922
|
+
return bearerToken === token || queryToken === token
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
app.use('*', async (c, next) => {
|
|
926
|
+
const hostname = parseHostname(c.req.header('host'))
|
|
927
|
+
if (hostname && ALLOWED_HOSTS.has(hostname)) {
|
|
928
|
+
return next()
|
|
929
|
+
}
|
|
930
|
+
// Remote clients with a valid token are allowed regardless of Host
|
|
931
|
+
if (hasValidToken(c)) {
|
|
932
|
+
return next()
|
|
933
|
+
}
|
|
934
|
+
// Missing Host header from non-browser clients (curl without Host) is fine
|
|
935
|
+
// in local mode since they're not browser-based DNS rebinding attacks
|
|
936
|
+
if (!hostname && !token) {
|
|
937
|
+
return next()
|
|
938
|
+
}
|
|
939
|
+
logger?.log(pc.red(`Rejecting request with unexpected Host header: ${c.req.header('host')} (DNS rebinding protection)`))
|
|
940
|
+
return c.text('Forbidden - Invalid Host header', 403)
|
|
941
|
+
})
|
|
942
|
+
|
|
859
943
|
const { injectWebSocket, upgradeWebSocket } = createNodeWebSocket({ app })
|
|
860
944
|
|
|
861
945
|
const getCdpWsUrl = (c: { req: { header: (name: string) => string | undefined } }) => {
|
|
@@ -1028,6 +1112,7 @@ export async function startPlayWriterCDPRelayServer({
|
|
|
1028
1112
|
const clientId = c.req.param('clientId') || 'default'
|
|
1029
1113
|
const url = new URL(c.req.url, 'http://localhost')
|
|
1030
1114
|
const requestedExtensionId = url.searchParams.get('extensionId')
|
|
1115
|
+
const autoEnable = url.searchParams.get('autoEnable') === '1'
|
|
1031
1116
|
// When extensionId is explicit, resolve directly. Otherwise use fallback which
|
|
1032
1117
|
// handles single-extension and uniquely-active-extension cases (#52).
|
|
1033
1118
|
const resolvedExtension = requestedExtensionId
|
|
@@ -1119,6 +1204,7 @@ export async function startPlayWriterCDPRelayServer({
|
|
|
1119
1204
|
params,
|
|
1120
1205
|
sessionId,
|
|
1121
1206
|
source,
|
|
1207
|
+
autoEnable,
|
|
1122
1208
|
})
|
|
1123
1209
|
|
|
1124
1210
|
if (method === 'Target.setAutoAttach' && !sessionId) {
|
|
@@ -1755,7 +1841,7 @@ export async function startPlayWriterCDPRelayServer({
|
|
|
1755
1841
|
const { ExecutorManager } = await import('./executor.js')
|
|
1756
1842
|
// Pass config instead of URL so executor can generate unique client IDs for each connection
|
|
1757
1843
|
executorManager = new ExecutorManager({
|
|
1758
|
-
cdpConfig: { host: '127.0.0.1', port },
|
|
1844
|
+
cdpConfig: { host: '127.0.0.1', port, token },
|
|
1759
1845
|
logger: logger || { log: console.error, error: console.error },
|
|
1760
1846
|
})
|
|
1761
1847
|
}
|
|
@@ -1763,20 +1849,24 @@ export async function startPlayWriterCDPRelayServer({
|
|
|
1763
1849
|
}
|
|
1764
1850
|
|
|
1765
1851
|
// ============================================================================
|
|
1766
|
-
// Security middleware for privileged HTTP routes (/cli/*, /recording
|
|
1852
|
+
// Security middleware for privileged HTTP routes (/cli/*, /recording/*, /mcp-log)
|
|
1767
1853
|
//
|
|
1768
1854
|
// CORS alone does NOT prevent cross-origin POST attacks. Browsers skip the
|
|
1769
1855
|
// preflight for "simple" requests (POST + Content-Type: text/plain), so a
|
|
1770
1856
|
// malicious website can fire-and-forget a POST to localhost:19988/cli/execute
|
|
1771
1857
|
// and the code executes before CORS even enters the picture.
|
|
1772
1858
|
//
|
|
1773
|
-
//
|
|
1859
|
+
// Three layers of defense:
|
|
1774
1860
|
// 1. Sec-Fetch-Site: browsers set this forbidden header on every request.
|
|
1775
1861
|
// If present and not "same-origin"/"none", it's a cross-origin browser
|
|
1776
1862
|
// request → reject. Node.js clients don't send it → unaffected.
|
|
1777
1863
|
// 2. Content-Type must be application/json on POST. This forces a CORS
|
|
1778
1864
|
// preflight as a fallback, which our CORS policy already blocks.
|
|
1779
|
-
// 3. When token mode is enabled (remote access), require the token
|
|
1865
|
+
// 3. When token mode is enabled (remote access), require the token on EVERY
|
|
1866
|
+
// request, including loopback. Tunnel agents (traforo, ngrok, cloudflared)
|
|
1867
|
+
// forward public traffic from 127.0.0.1, so a loopback bypass would be
|
|
1868
|
+
// a full auth bypass. In-process callers attach the token themselves
|
|
1869
|
+
// via PLAYWRITER_TOKEN env (set by the `serve` command at startup).
|
|
1780
1870
|
// ============================================================================
|
|
1781
1871
|
const privilegedRouteMiddleware = async (
|
|
1782
1872
|
c: Parameters<Parameters<typeof app.use>[1]>[0],
|
|
@@ -1801,7 +1891,14 @@ export async function startPlayWriterCDPRelayServer({
|
|
|
1801
1891
|
}
|
|
1802
1892
|
}
|
|
1803
1893
|
|
|
1804
|
-
// When token mode is enabled (remote/serve mode), require authentication
|
|
1894
|
+
// When token mode is enabled (remote/serve mode), require authentication
|
|
1895
|
+
// on EVERY request, including loopback. Earlier versions bypassed the
|
|
1896
|
+
// check for 127.0.0.1/::1 to spare in-process callers, but that's unsafe:
|
|
1897
|
+
// when the relay is fronted by a tunnel agent (traforo, ngrok, cloudflared,
|
|
1898
|
+
// etc.) running as a local process, every public request reaches the relay
|
|
1899
|
+
// from 127.0.0.1 and would skip auth. In-process callers must instead
|
|
1900
|
+
// attach the token themselves — they read PLAYWRITER_TOKEN from env, which
|
|
1901
|
+
// the `serve` command sets at startup.
|
|
1805
1902
|
if (token) {
|
|
1806
1903
|
const authHeader = c.req.header('authorization') || ''
|
|
1807
1904
|
const bearerToken = authHeader.startsWith('Bearer ') ? authHeader.slice(7) : null
|
|
@@ -1818,6 +1915,7 @@ export async function startPlayWriterCDPRelayServer({
|
|
|
1818
1915
|
|
|
1819
1916
|
app.use('/cli/*', privilegedRouteMiddleware)
|
|
1820
1917
|
app.use('/recording/*', privilegedRouteMiddleware)
|
|
1918
|
+
app.use('/mcp-log', privilegedRouteMiddleware)
|
|
1821
1919
|
|
|
1822
1920
|
app.post('/cli/execute', async (c) => {
|
|
1823
1921
|
try {
|
|
@@ -1885,6 +1983,7 @@ export async function startPlayWriterCDPRelayServer({
|
|
|
1885
1983
|
app.post('/cli/session/new', async (c) => {
|
|
1886
1984
|
const body = (await c.req.json().catch(() => ({}))) as {
|
|
1887
1985
|
extensionId?: string | null
|
|
1986
|
+
autoEnable?: boolean
|
|
1888
1987
|
cwd?: string
|
|
1889
1988
|
/** Direct CDP WebSocket URL — bypasses extension, connects straight to Chrome */
|
|
1890
1989
|
cdpEndpoint?: string
|
|
@@ -1938,6 +2037,7 @@ export async function startPlayWriterCDPRelayServer({
|
|
|
1938
2037
|
const executor = manager.getExecutor({
|
|
1939
2038
|
sessionId,
|
|
1940
2039
|
cwd,
|
|
2040
|
+
cdpConfig: { host: '127.0.0.1', port, token, extensionId: conn.stableKey, autoEnable: body.autoEnable === true },
|
|
1941
2041
|
sessionMetadata: {
|
|
1942
2042
|
extensionId: conn.stableKey,
|
|
1943
2043
|
browser: conn.info.browser || null,
|
|
@@ -2053,9 +2153,26 @@ export async function startPlayWriterCDPRelayServer({
|
|
|
2053
2153
|
return c.json(result)
|
|
2054
2154
|
})
|
|
2055
2155
|
|
|
2056
|
-
|
|
2156
|
+
// Use createAdaptorServer instead of serve() so we control the listen()
|
|
2157
|
+
// timing. This lets us inject WebSocket upgrade handlers before binding and
|
|
2158
|
+
// await the bind to surface EADDRINUSE as a catchable error (issue #75).
|
|
2159
|
+
const server = createAdaptorServer({ fetch: app.fetch, hostname: host })
|
|
2057
2160
|
injectWebSocket(server)
|
|
2058
2161
|
|
|
2162
|
+
await new Promise<void>((resolve, reject) => {
|
|
2163
|
+
const onListening = () => {
|
|
2164
|
+
server.off('error', onError)
|
|
2165
|
+
resolve()
|
|
2166
|
+
}
|
|
2167
|
+
const onError = (error: Error) => {
|
|
2168
|
+
server.off('listening', onListening)
|
|
2169
|
+
reject(error)
|
|
2170
|
+
}
|
|
2171
|
+
server.once('listening', onListening)
|
|
2172
|
+
server.once('error', onError)
|
|
2173
|
+
server.listen(port, host)
|
|
2174
|
+
})
|
|
2175
|
+
|
|
2059
2176
|
const wsHost = `ws://${host}:${port}`
|
|
2060
2177
|
const cdpEndpoint = `${wsHost}/cdp`
|
|
2061
2178
|
const extensionEndpoint = `${wsHost}/extension`
|
package/src/cli-help.test.ts
CHANGED
|
@@ -38,4 +38,26 @@ describe('playwriter cli help', () => {
|
|
|
38
38
|
expect(stdout).toContain('--replace')
|
|
39
39
|
expect(stderr).toBe('')
|
|
40
40
|
}, 30000)
|
|
41
|
+
|
|
42
|
+
test('unknown command exits with code 1', async () => {
|
|
43
|
+
try {
|
|
44
|
+
await runCli(['run'])
|
|
45
|
+
expect.unreachable('should have thrown')
|
|
46
|
+
} catch (error: any) {
|
|
47
|
+
expect(error.code).toBe(1)
|
|
48
|
+
expect(error.stderr).toContain('Unknown command: run')
|
|
49
|
+
expect(error.stderr).toContain('playwriter --help')
|
|
50
|
+
}
|
|
51
|
+
}, 30000)
|
|
52
|
+
|
|
53
|
+
test('unknown subcommand exits with code 1', async () => {
|
|
54
|
+
try {
|
|
55
|
+
await runCli(['session', 'nonexistent'])
|
|
56
|
+
expect.unreachable('should have thrown')
|
|
57
|
+
} catch (error: any) {
|
|
58
|
+
expect(error.code).toBe(1)
|
|
59
|
+
expect(error.stdout).toContain('Unknown command: session nonexistent')
|
|
60
|
+
expect(error.stdout).toContain('session new')
|
|
61
|
+
}
|
|
62
|
+
}, 30000)
|
|
41
63
|
})
|