@1outres/dsh-bot-connector-browser 0.3.0 → 0.4.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/README.md +12 -1
- package/lib/index.js +507 -41
- package/package.json +3 -3
- package/src/host/connector.ts +15 -7
- package/src/host/preparation.ts +68 -8
- package/src/host/session.ts +20 -5
- package/src/host/tools.ts +19 -10
- package/src/host/vault/failures.ts +31 -0
- package/src/host/vault/helper.ts +97 -0
- package/src/host/vault/marks.ts +22 -0
- package/src/host/vault/passkeys.ts +124 -0
- package/src/host/vault/protocol.ts +143 -0
- package/src/host/vault/tools.ts +218 -0
- package/src/host/vm/layout.ts +7 -1
- package/src/index.ts +22 -8
- package/vm/helper/bytes.mjs +74 -0
- package/vm/helper/cdp.mjs +259 -0
- package/vm/helper/channel.mjs +99 -0
- package/vm/helper/dsh-browser-fill.mjs +154 -0
- package/vm/helper/dsh-browser-passkey.mjs +192 -0
- package/vm/helper/page.mjs +135 -0
- package/vm/install-helper.sh +12 -0
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
// A DevTools Protocol client over a minimal WebSocket (RFC 6455) on node:net, so that a message
|
|
2
|
+
// holding a secret is written from bytes the caller wipes, never built as a JS string.
|
|
3
|
+
import { randomBytes } from "node:crypto"
|
|
4
|
+
import { connect } from "node:net"
|
|
5
|
+
|
|
6
|
+
const MAX_MESSAGE_BYTES = 64 << 20
|
|
7
|
+
const CALL_TIMEOUT_MS = 15_000
|
|
8
|
+
const encoder = new TextEncoder()
|
|
9
|
+
|
|
10
|
+
export class CdpError extends Error {
|
|
11
|
+
constructor(message) {
|
|
12
|
+
super(message)
|
|
13
|
+
this.name = "CdpError"
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** `127.0.0.1:<port>` only: DevTools on loopback. */
|
|
18
|
+
export function parseEndpoint(value) {
|
|
19
|
+
const match = typeof value === "string" ? /^127\.0\.0\.1:([0-9]{2,5})$/.exec(value) : null
|
|
20
|
+
if (match === null) throw new CdpError("the DevTools endpoint is not on loopback")
|
|
21
|
+
return { host: "127.0.0.1", port: Number(match[1]) }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async function browserPath(endpoint) {
|
|
25
|
+
const response = await fetch(`http://${endpoint.host}:${endpoint.port}/json/version`, {
|
|
26
|
+
signal: AbortSignal.timeout(CALL_TIMEOUT_MS),
|
|
27
|
+
})
|
|
28
|
+
const url = new URL((await response.json()).webSocketDebuggerUrl)
|
|
29
|
+
if (
|
|
30
|
+
url.protocol !== "ws:" ||
|
|
31
|
+
url.hostname !== endpoint.host ||
|
|
32
|
+
url.port !== String(endpoint.port)
|
|
33
|
+
) {
|
|
34
|
+
throw new CdpError("DevTools answered with another endpoint")
|
|
35
|
+
}
|
|
36
|
+
if (!/^\/devtools\/browser\/[A-Za-z0-9-]+$/.test(url.pathname)) {
|
|
37
|
+
throw new CdpError("DevTools answered with an unexpected browser path")
|
|
38
|
+
}
|
|
39
|
+
return url.pathname
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function frameHeader(opcode, length) {
|
|
43
|
+
if (length < 126) return Uint8Array.of(0x80 | opcode, 0x80 | length)
|
|
44
|
+
if (length < 0x10000) return Uint8Array.of(0x80 | opcode, 0x80 | 126, length >> 8, length & 0xff)
|
|
45
|
+
const header = new Uint8Array(10)
|
|
46
|
+
header[0] = 0x80 | opcode
|
|
47
|
+
header[1] = 0x80 | 127
|
|
48
|
+
new DataView(header.buffer).setBigUint64(2, BigInt(length))
|
|
49
|
+
return header
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** One masked frame of the parts; the frame is wiped once the socket wrote it. */
|
|
53
|
+
function writeFrame(socket, opcode, parts) {
|
|
54
|
+
const length = parts.reduce((sum, part) => sum + part.length, 0)
|
|
55
|
+
const header = frameHeader(opcode, length)
|
|
56
|
+
const mask = randomBytes(4)
|
|
57
|
+
const frame = Buffer.alloc(header.length + 4 + length)
|
|
58
|
+
frame.set(header, 0)
|
|
59
|
+
frame.set(mask, header.length)
|
|
60
|
+
let at = header.length + 4
|
|
61
|
+
let index = 0
|
|
62
|
+
for (const part of parts) {
|
|
63
|
+
for (const byte of part) frame[at++] = byte ^ mask[index++ & 3]
|
|
64
|
+
}
|
|
65
|
+
return new Promise((resolve, reject) => {
|
|
66
|
+
socket.write(frame, (error) => {
|
|
67
|
+
frame.fill(0)
|
|
68
|
+
if (error) reject(new CdpError("DevTools connection failed"))
|
|
69
|
+
else resolve()
|
|
70
|
+
})
|
|
71
|
+
})
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function handshake(socket, path, endpoint) {
|
|
75
|
+
const key = randomBytes(16).toString("base64")
|
|
76
|
+
socket.write(
|
|
77
|
+
`GET ${path} HTTP/1.1\r\nHost: ${endpoint.host}:${endpoint.port}\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Key: ${key}\r\nSec-WebSocket-Version: 13\r\n\r\n`,
|
|
78
|
+
)
|
|
79
|
+
return new Promise((resolve, reject) => {
|
|
80
|
+
let head = Buffer.alloc(0)
|
|
81
|
+
const onData = (chunk) => {
|
|
82
|
+
head = Buffer.concat([head, chunk])
|
|
83
|
+
const end = head.indexOf("\r\n\r\n")
|
|
84
|
+
if (end < 0) {
|
|
85
|
+
if (head.length > 16_384) fail(new CdpError("DevTools sent an oversized handshake"))
|
|
86
|
+
return
|
|
87
|
+
}
|
|
88
|
+
socket.off("data", onData)
|
|
89
|
+
socket.off("error", fail)
|
|
90
|
+
if (!/^HTTP\/1\.1 101 /.test(head.subarray(0, end).toString("latin1"))) {
|
|
91
|
+
reject(new CdpError("DevTools refused the WebSocket"))
|
|
92
|
+
return
|
|
93
|
+
}
|
|
94
|
+
resolve(head.subarray(end + 4))
|
|
95
|
+
}
|
|
96
|
+
const fail = (error) => {
|
|
97
|
+
socket.off("data", onData)
|
|
98
|
+
reject(error instanceof CdpError ? error : new CdpError("DevTools is not reachable"))
|
|
99
|
+
}
|
|
100
|
+
socket.on("data", onData)
|
|
101
|
+
socket.on("error", fail)
|
|
102
|
+
})
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Reads frames, answers pings, and hands each whole text message on. */
|
|
106
|
+
function frameReader(socket, onMessage, onClose) {
|
|
107
|
+
let buffer = Buffer.alloc(0)
|
|
108
|
+
let fragments = []
|
|
109
|
+
return (chunk) => {
|
|
110
|
+
buffer = buffer.length === 0 ? chunk : Buffer.concat([buffer, chunk])
|
|
111
|
+
for (;;) {
|
|
112
|
+
if (buffer.length < 2) return
|
|
113
|
+
const fin = (buffer[0] & 0x80) !== 0
|
|
114
|
+
const opcode = buffer[0] & 0x0f
|
|
115
|
+
let length = buffer[1] & 0x7f
|
|
116
|
+
let offset = 2
|
|
117
|
+
if (length === 126) {
|
|
118
|
+
if (buffer.length < 4) return
|
|
119
|
+
length = buffer.readUInt16BE(2)
|
|
120
|
+
offset = 4
|
|
121
|
+
} else if (length === 127) {
|
|
122
|
+
if (buffer.length < 10) return
|
|
123
|
+
length = Number(buffer.readBigUInt64BE(2))
|
|
124
|
+
offset = 10
|
|
125
|
+
}
|
|
126
|
+
if ((buffer[1] & 0x80) !== 0 || length > MAX_MESSAGE_BYTES) {
|
|
127
|
+
onClose(new CdpError("DevTools sent an invalid frame"))
|
|
128
|
+
return
|
|
129
|
+
}
|
|
130
|
+
if (buffer.length < offset + length) return
|
|
131
|
+
const payload = buffer.subarray(offset, offset + length)
|
|
132
|
+
buffer = buffer.subarray(offset + length)
|
|
133
|
+
if (opcode === 0x8) {
|
|
134
|
+
onClose(new CdpError("DevTools closed the connection"))
|
|
135
|
+
return
|
|
136
|
+
}
|
|
137
|
+
if (opcode === 0x9) {
|
|
138
|
+
void writeFrame(socket, 0xa, [payload]).catch(() => {})
|
|
139
|
+
continue
|
|
140
|
+
}
|
|
141
|
+
if (opcode === 0xa) continue
|
|
142
|
+
if (opcode === 0x1 || opcode === 0x0) {
|
|
143
|
+
fragments.push(payload)
|
|
144
|
+
if (fragments.reduce((sum, part) => sum + part.length, 0) > MAX_MESSAGE_BYTES) {
|
|
145
|
+
onClose(new CdpError("DevTools sent an oversized message"))
|
|
146
|
+
return
|
|
147
|
+
}
|
|
148
|
+
if (fin) {
|
|
149
|
+
const message = Buffer.concat(fragments)
|
|
150
|
+
fragments = []
|
|
151
|
+
onMessage(message)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* The browser's DevTools connection. `call` sends a command; `callWithSecret` sends one whose
|
|
160
|
+
* params are `before` + secret + `after` (JSON text), the secret already encoded as JSON
|
|
161
|
+
* string body bytes; the caller wipes it.
|
|
162
|
+
*/
|
|
163
|
+
export async function connectBrowser(endpoint) {
|
|
164
|
+
const path = await browserPath(endpoint)
|
|
165
|
+
const socket = connect(endpoint)
|
|
166
|
+
await new Promise((resolve, reject) => {
|
|
167
|
+
socket.once("connect", resolve)
|
|
168
|
+
socket.once("error", () => reject(new CdpError("DevTools is not reachable")))
|
|
169
|
+
})
|
|
170
|
+
const rest = await handshake(socket, path, endpoint)
|
|
171
|
+
const pending = new Map()
|
|
172
|
+
const listeners = new Set()
|
|
173
|
+
let nextId = 1
|
|
174
|
+
let failure
|
|
175
|
+
let closedResolve
|
|
176
|
+
const closed = new Promise((resolve) => {
|
|
177
|
+
closedResolve = resolve
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
const close = (error) => {
|
|
181
|
+
if (failure !== undefined) return
|
|
182
|
+
failure = error
|
|
183
|
+
for (const entry of pending.values()) entry.reject(error)
|
|
184
|
+
pending.clear()
|
|
185
|
+
socket.destroy()
|
|
186
|
+
closedResolve(error)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const onMessage = (bytes) => {
|
|
190
|
+
let message
|
|
191
|
+
try {
|
|
192
|
+
message = JSON.parse(bytes.toString("utf8"))
|
|
193
|
+
} catch {
|
|
194
|
+
close(new CdpError("DevTools sent a message that is not JSON"))
|
|
195
|
+
return
|
|
196
|
+
}
|
|
197
|
+
if (typeof message.id === "number") {
|
|
198
|
+
const entry = pending.get(message.id)
|
|
199
|
+
if (entry === undefined) return
|
|
200
|
+
pending.delete(message.id)
|
|
201
|
+
if (message.error !== undefined) {
|
|
202
|
+
entry.reject(
|
|
203
|
+
new CdpError(`${entry.method}: ${String(message.error.message).slice(0, 200)}`),
|
|
204
|
+
)
|
|
205
|
+
} else {
|
|
206
|
+
entry.resolve(message.result ?? {})
|
|
207
|
+
}
|
|
208
|
+
return
|
|
209
|
+
}
|
|
210
|
+
if (typeof message.method === "string") {
|
|
211
|
+
for (const listener of listeners) listener(message)
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const read = frameReader(socket, onMessage, close)
|
|
216
|
+
socket.on("data", read)
|
|
217
|
+
socket.on("error", () => close(new CdpError("DevTools connection failed")))
|
|
218
|
+
socket.on("close", () => close(new CdpError("DevTools connection ended")))
|
|
219
|
+
if (rest.length > 0) read(rest)
|
|
220
|
+
|
|
221
|
+
const send = async (method, sessionId, parts) => {
|
|
222
|
+
if (failure !== undefined) throw failure
|
|
223
|
+
const id = nextId++
|
|
224
|
+
const answer = new Promise((resolve, reject) => {
|
|
225
|
+
const timer = setTimeout(() => {
|
|
226
|
+
if (pending.delete(id)) reject(new CdpError(`${method} timed out`))
|
|
227
|
+
}, CALL_TIMEOUT_MS)
|
|
228
|
+
pending.set(id, {
|
|
229
|
+
method,
|
|
230
|
+
resolve: (value) => {
|
|
231
|
+
clearTimeout(timer)
|
|
232
|
+
resolve(value)
|
|
233
|
+
},
|
|
234
|
+
reject: (error) => {
|
|
235
|
+
clearTimeout(timer)
|
|
236
|
+
reject(error)
|
|
237
|
+
},
|
|
238
|
+
})
|
|
239
|
+
})
|
|
240
|
+
const head = encoder.encode(
|
|
241
|
+
`{"id":${id},${sessionId === undefined ? "" : `"sessionId":${JSON.stringify(sessionId)},`}"method":${JSON.stringify(method)},"params":`,
|
|
242
|
+
)
|
|
243
|
+
await writeFrame(socket, 0x1, [head, ...parts, encoder.encode("}")])
|
|
244
|
+
return answer
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return {
|
|
248
|
+
closed,
|
|
249
|
+
call: (method, params = {}, sessionId) =>
|
|
250
|
+
send(method, sessionId, [encoder.encode(JSON.stringify(params))]),
|
|
251
|
+
callWithSecret: (method, before, secret, after, sessionId) =>
|
|
252
|
+
send(method, sessionId, [encoder.encode(before), secret, encoder.encode(after)]),
|
|
253
|
+
on(listener) {
|
|
254
|
+
listeners.add(listener)
|
|
255
|
+
return () => listeners.delete(listener)
|
|
256
|
+
},
|
|
257
|
+
close: () => close(new CdpError("closed")),
|
|
258
|
+
}
|
|
259
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
// The helper's side of its standard input and output: JSON lines from the Host, a secret's raw
|
|
2
|
+
// bytes right after the line that announces their length, and JSON lines back. Every chunk read
|
|
3
|
+
// is wiped once copied.
|
|
4
|
+
|
|
5
|
+
const MAX_LINE_BYTES = 64 * 1024
|
|
6
|
+
const MAX_SECRET_BYTES = 64 * 1024
|
|
7
|
+
|
|
8
|
+
export class ChannelError extends Error {
|
|
9
|
+
constructor(message) {
|
|
10
|
+
super(message)
|
|
11
|
+
this.name = "ChannelError"
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function openChannel(input = process.stdin, output = process.stdout) {
|
|
16
|
+
let pending = new Uint8Array(0)
|
|
17
|
+
let ended = false
|
|
18
|
+
let wake
|
|
19
|
+
let endedResolve
|
|
20
|
+
const endedPromise = new Promise((resolve) => {
|
|
21
|
+
endedResolve = resolve
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
input.on("data", (chunk) => {
|
|
25
|
+
const joined = new Uint8Array(pending.length + chunk.length)
|
|
26
|
+
joined.set(pending)
|
|
27
|
+
joined.set(chunk, pending.length)
|
|
28
|
+
pending.fill(0)
|
|
29
|
+
chunk.fill(0)
|
|
30
|
+
pending = joined
|
|
31
|
+
wake?.()
|
|
32
|
+
})
|
|
33
|
+
input.on("end", () => {
|
|
34
|
+
ended = true
|
|
35
|
+
endedResolve()
|
|
36
|
+
wake?.()
|
|
37
|
+
})
|
|
38
|
+
input.on("error", () => {
|
|
39
|
+
ended = true
|
|
40
|
+
endedResolve()
|
|
41
|
+
wake?.()
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
const more = () =>
|
|
45
|
+
new Promise((resolve) => {
|
|
46
|
+
wake = () => {
|
|
47
|
+
wake = undefined
|
|
48
|
+
resolve()
|
|
49
|
+
}
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
const take = (length) => {
|
|
53
|
+
const head = pending.slice(0, length)
|
|
54
|
+
const rest = pending.slice(length)
|
|
55
|
+
pending.fill(0)
|
|
56
|
+
pending = rest
|
|
57
|
+
return head
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
ended: endedPromise,
|
|
62
|
+
/** The next JSON line as an object; undefined once the Host ended its side. */
|
|
63
|
+
async line() {
|
|
64
|
+
for (;;) {
|
|
65
|
+
const index = pending.indexOf(0x0a)
|
|
66
|
+
if (index >= 0) {
|
|
67
|
+
const line = take(index + 1)
|
|
68
|
+
let value
|
|
69
|
+
try {
|
|
70
|
+
value = JSON.parse(new TextDecoder().decode(line.subarray(0, index)))
|
|
71
|
+
} catch {
|
|
72
|
+
throw new ChannelError("a line that is not JSON")
|
|
73
|
+
}
|
|
74
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
75
|
+
throw new ChannelError("a line that is not an object")
|
|
76
|
+
}
|
|
77
|
+
return value
|
|
78
|
+
}
|
|
79
|
+
if (pending.length > MAX_LINE_BYTES) throw new ChannelError("a line too long")
|
|
80
|
+
if (ended) return undefined
|
|
81
|
+
await more()
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
/** Exactly `length` raw bytes; the caller wipes them. */
|
|
85
|
+
async bytes(length) {
|
|
86
|
+
if (!Number.isSafeInteger(length) || length < 0 || length > MAX_SECRET_BYTES) {
|
|
87
|
+
throw new ChannelError("a secret of an unexpected length")
|
|
88
|
+
}
|
|
89
|
+
while (pending.length < length) {
|
|
90
|
+
if (ended) throw new ChannelError("the input ended early")
|
|
91
|
+
await more()
|
|
92
|
+
}
|
|
93
|
+
return take(length)
|
|
94
|
+
},
|
|
95
|
+
reply(message) {
|
|
96
|
+
output.write(`${JSON.stringify(message)}\n`)
|
|
97
|
+
},
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { isUtf8, jsonStringBytes } from "./bytes.mjs"
|
|
3
|
+
// Types one login value into the element the Host marked (browser connector, browser_fill_credential).
|
|
4
|
+
// stdin: {"devtools","mark","field"}, then, after this answers {"located"}, {"fill":{"origin","length",
|
|
5
|
+
// "submit"}} followed by the value's bytes (or nothing: the Host refused). stdout: JSON lines with
|
|
6
|
+
// closed codes only. The value is never a JS string here, never printed and wiped after use.
|
|
7
|
+
import { connectBrowser, parseEndpoint } from "./cdp.mjs"
|
|
8
|
+
import { openChannel } from "./channel.mjs"
|
|
9
|
+
import {
|
|
10
|
+
attachTargets,
|
|
11
|
+
checkMark,
|
|
12
|
+
findMarked,
|
|
13
|
+
frameOrigin,
|
|
14
|
+
onNode,
|
|
15
|
+
PageError,
|
|
16
|
+
unmark,
|
|
17
|
+
webOrigin,
|
|
18
|
+
} from "./page.mjs"
|
|
19
|
+
|
|
20
|
+
const ATTRIBUTE = "data-dsh-fill"
|
|
21
|
+
const FIELDS = ["username", "password", "otp"]
|
|
22
|
+
const TEXT_TYPES = ["text", "email", "tel", "url", "search", "password", "number"]
|
|
23
|
+
|
|
24
|
+
const DESCRIBE = `function () {
|
|
25
|
+
const tag = this.localName;
|
|
26
|
+
const tokens = (this.getAttribute("autocomplete") || "").toLowerCase().split(/\\s+/);
|
|
27
|
+
return {
|
|
28
|
+
tag,
|
|
29
|
+
type: tag === "input" ? this.type : null,
|
|
30
|
+
editable: this.isContentEditable === true,
|
|
31
|
+
disabled: this.disabled === true || this.readOnly === true,
|
|
32
|
+
password: (tag === "input" && this.type === "password") || tokens.includes("current-password") || tokens.includes("new-password"),
|
|
33
|
+
};
|
|
34
|
+
}`
|
|
35
|
+
|
|
36
|
+
const CLEAR = `function () {
|
|
37
|
+
if (this.localName === "input" || this.localName === "textarea") {
|
|
38
|
+
this.focus();
|
|
39
|
+
this.select();
|
|
40
|
+
} else {
|
|
41
|
+
this.focus();
|
|
42
|
+
const selection = this.ownerDocument.getSelection();
|
|
43
|
+
selection.selectAllChildren(this);
|
|
44
|
+
}
|
|
45
|
+
const root = this.getRootNode();
|
|
46
|
+
return root.activeElement === this || this.contains(root.activeElement);
|
|
47
|
+
}`
|
|
48
|
+
|
|
49
|
+
const FOCUSED = `function () {
|
|
50
|
+
const root = this.getRootNode();
|
|
51
|
+
return this.isConnected && (root.activeElement === this || this.contains(root.activeElement));
|
|
52
|
+
}`
|
|
53
|
+
|
|
54
|
+
function elementKind(description, field) {
|
|
55
|
+
const { tag, type, editable, disabled, password } = description
|
|
56
|
+
const kind =
|
|
57
|
+
tag === "input" && TEXT_TYPES.includes(type)
|
|
58
|
+
? `input:${type}`
|
|
59
|
+
: tag === "textarea"
|
|
60
|
+
? "textarea"
|
|
61
|
+
: editable
|
|
62
|
+
? "contenteditable"
|
|
63
|
+
: undefined
|
|
64
|
+
if (kind === undefined || disabled) throw new PageError("not-editable")
|
|
65
|
+
if (field === "password" && !password) throw new PageError("not-password-field")
|
|
66
|
+
return kind
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async function main() {
|
|
70
|
+
const channel = openChannel()
|
|
71
|
+
const request = await channel.line()
|
|
72
|
+
if (request === undefined || !FIELDS.includes(request.field)) throw new PageError("protocol")
|
|
73
|
+
const mark = checkMark(request.mark)
|
|
74
|
+
let cdp
|
|
75
|
+
try {
|
|
76
|
+
cdp = await connectBrowser(parseEndpoint(request.devtools))
|
|
77
|
+
} catch {
|
|
78
|
+
throw new PageError("devtools")
|
|
79
|
+
}
|
|
80
|
+
let node
|
|
81
|
+
try {
|
|
82
|
+
const sessions = await attachTargets(cdp, ["page", "iframe"])
|
|
83
|
+
node = await findMarked(cdp, sessions, { attribute: ATTRIBUTE, mark, mainFrameOnly: false })
|
|
84
|
+
const element = elementKind(await onNode(cdp, node, DESCRIBE), request.field)
|
|
85
|
+
const origin = webOrigin(node.origin)
|
|
86
|
+
channel.reply({ located: { origin, element } })
|
|
87
|
+
|
|
88
|
+
const order = await channel.line()
|
|
89
|
+
if (order === undefined) {
|
|
90
|
+
channel.reply({ cancelled: true })
|
|
91
|
+
return
|
|
92
|
+
}
|
|
93
|
+
const fill = order.fill
|
|
94
|
+
if (
|
|
95
|
+
typeof fill !== "object" ||
|
|
96
|
+
fill === null ||
|
|
97
|
+
fill.origin !== origin ||
|
|
98
|
+
typeof fill.submit !== "boolean"
|
|
99
|
+
) {
|
|
100
|
+
throw new PageError("protocol")
|
|
101
|
+
}
|
|
102
|
+
const value = await channel.bytes(fill.length)
|
|
103
|
+
try {
|
|
104
|
+
if (!isUtf8(value)) throw new PageError("not-text")
|
|
105
|
+
if ((await frameOrigin(cdp, node.sessionId, node.frameId)) !== origin)
|
|
106
|
+
throw new PageError("origin-changed")
|
|
107
|
+
await cdp.call("DOM.enable", {}, node.sessionId)
|
|
108
|
+
await cdp.call("DOM.focus", { objectId: node.objectId }, node.sessionId)
|
|
109
|
+
if ((await onNode(cdp, node, CLEAR)) !== true) throw new PageError("focus-lost")
|
|
110
|
+
if ((await frameOrigin(cdp, node.sessionId, node.frameId)) !== origin)
|
|
111
|
+
throw new PageError("origin-changed")
|
|
112
|
+
const text = jsonStringBytes(value)
|
|
113
|
+
try {
|
|
114
|
+
await cdp.callWithSecret("Input.insertText", '{"text":"', text, '"}', node.sessionId)
|
|
115
|
+
} finally {
|
|
116
|
+
text.fill(0)
|
|
117
|
+
}
|
|
118
|
+
} finally {
|
|
119
|
+
value.fill(0)
|
|
120
|
+
}
|
|
121
|
+
const focused = (await onNode(cdp, node, FOCUSED)) === true
|
|
122
|
+
await unmark(cdp, node, ATTRIBUTE)
|
|
123
|
+
const { sessionId } = node
|
|
124
|
+
node = undefined
|
|
125
|
+
if (fill.submit) {
|
|
126
|
+
if (!focused) throw new PageError("focus-lost")
|
|
127
|
+
await pressEnter(cdp, sessionId)
|
|
128
|
+
}
|
|
129
|
+
channel.reply({ filled: { submitted: fill.submit } })
|
|
130
|
+
} finally {
|
|
131
|
+
if (node !== undefined) await unmark(cdp, node, ATTRIBUTE).catch(() => {})
|
|
132
|
+
cdp.close()
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
async function pressEnter(cdp, sessionId) {
|
|
137
|
+
const key = { key: "Enter", code: "Enter", windowsVirtualKeyCode: 13, nativeVirtualKeyCode: 13 }
|
|
138
|
+
await cdp.call(
|
|
139
|
+
"Input.dispatchKeyEvent",
|
|
140
|
+
{ type: "keyDown", ...key, text: "\r", unmodifiedText: "\r" },
|
|
141
|
+
sessionId,
|
|
142
|
+
)
|
|
143
|
+
await cdp.call("Input.dispatchKeyEvent", { type: "keyUp", ...key }, sessionId)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
main().then(
|
|
147
|
+
() => process.exit(0),
|
|
148
|
+
(error) => {
|
|
149
|
+
process.stdout.write(
|
|
150
|
+
`${JSON.stringify({ error: error instanceof PageError ? error.code : "failed" })}\n`,
|
|
151
|
+
)
|
|
152
|
+
process.exit(1)
|
|
153
|
+
},
|
|
154
|
+
)
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Offers one passkey to the tab the Host marked (browser connector, browser_use_passkey): a
|
|
3
|
+
// virtual authenticator on that tab's DevTools session, alive while this process is, until the
|
|
4
|
+
// site used it once, the time is up, the tab left the origin or closed, or the Host ended it.
|
|
5
|
+
// stdin: {"devtools","mark"}, then, after this answers {"located"}, {"passkey":{"origin","rpId",
|
|
6
|
+
// "credentialId","userHandle"?,"signCount","seconds","length"}} followed by the PKCS#8 key's bytes.
|
|
7
|
+
// stdout: JSON lines with closed codes only. The key is never a JS string here and is wiped.
|
|
8
|
+
import { base64Bytes } from "./bytes.mjs"
|
|
9
|
+
import { connectBrowser, parseEndpoint } from "./cdp.mjs"
|
|
10
|
+
import { openChannel } from "./channel.mjs"
|
|
11
|
+
import {
|
|
12
|
+
attachTargets,
|
|
13
|
+
checkMark,
|
|
14
|
+
findMarked,
|
|
15
|
+
frameOrigin,
|
|
16
|
+
PageError,
|
|
17
|
+
unmark,
|
|
18
|
+
webOrigin,
|
|
19
|
+
} from "./page.mjs"
|
|
20
|
+
|
|
21
|
+
const ATTRIBUTE = "data-dsh-passkey"
|
|
22
|
+
const BASE64 = /^[A-Za-z0-9+/]+={0,2}$/
|
|
23
|
+
const RP_ID = /^[a-z0-9.-]{1,253}$/
|
|
24
|
+
const MAX_SECONDS = 120
|
|
25
|
+
|
|
26
|
+
function checkOrder(order, origin) {
|
|
27
|
+
const passkey = order?.passkey
|
|
28
|
+
if (
|
|
29
|
+
typeof passkey !== "object" ||
|
|
30
|
+
passkey === null ||
|
|
31
|
+
passkey.origin !== origin ||
|
|
32
|
+
typeof passkey.rpId !== "string" ||
|
|
33
|
+
!RP_ID.test(passkey.rpId) ||
|
|
34
|
+
typeof passkey.credentialId !== "string" ||
|
|
35
|
+
!BASE64.test(passkey.credentialId) ||
|
|
36
|
+
(passkey.userHandle !== undefined &&
|
|
37
|
+
(typeof passkey.userHandle !== "string" || !BASE64.test(passkey.userHandle))) ||
|
|
38
|
+
!Number.isSafeInteger(passkey.signCount) ||
|
|
39
|
+
passkey.signCount < 0 ||
|
|
40
|
+
!Number.isSafeInteger(passkey.seconds) ||
|
|
41
|
+
passkey.seconds < 1 ||
|
|
42
|
+
passkey.seconds > MAX_SECONDS
|
|
43
|
+
) {
|
|
44
|
+
throw new PageError("protocol")
|
|
45
|
+
}
|
|
46
|
+
return passkey
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function main() {
|
|
50
|
+
const channel = openChannel()
|
|
51
|
+
const request = await channel.line()
|
|
52
|
+
if (request === undefined) throw new PageError("protocol")
|
|
53
|
+
const mark = checkMark(request.mark)
|
|
54
|
+
let cdp
|
|
55
|
+
try {
|
|
56
|
+
cdp = await connectBrowser(parseEndpoint(request.devtools))
|
|
57
|
+
} catch {
|
|
58
|
+
throw new PageError("devtools")
|
|
59
|
+
}
|
|
60
|
+
let authenticatorId
|
|
61
|
+
let session
|
|
62
|
+
try {
|
|
63
|
+
const tab = await findMarked(cdp, await attachTargets(cdp, ["page"]), {
|
|
64
|
+
attribute: ATTRIBUTE,
|
|
65
|
+
mark,
|
|
66
|
+
mainFrameOnly: true,
|
|
67
|
+
})
|
|
68
|
+
await unmark(cdp, tab, ATTRIBUTE)
|
|
69
|
+
session = tab.sessionId
|
|
70
|
+
const origin = webOrigin(tab.origin)
|
|
71
|
+
channel.reply({ located: { origin } })
|
|
72
|
+
|
|
73
|
+
const order = await channel.line()
|
|
74
|
+
if (order === undefined) {
|
|
75
|
+
channel.reply({ cancelled: true })
|
|
76
|
+
return
|
|
77
|
+
}
|
|
78
|
+
const passkey = checkOrder(order, origin)
|
|
79
|
+
const key = await channel.bytes(order.passkey.length)
|
|
80
|
+
try {
|
|
81
|
+
if ((await frameOrigin(cdp, session, tab.frameId)) !== origin)
|
|
82
|
+
throw new PageError("origin-changed")
|
|
83
|
+
await cdp.call("Page.enable", {}, session)
|
|
84
|
+
await cdp.call("WebAuthn.enable", { enableUI: false }, session)
|
|
85
|
+
authenticatorId = (
|
|
86
|
+
await cdp.call(
|
|
87
|
+
"WebAuthn.addVirtualAuthenticator",
|
|
88
|
+
{
|
|
89
|
+
options: {
|
|
90
|
+
protocol: "ctap2",
|
|
91
|
+
transport: "internal",
|
|
92
|
+
hasResidentKey: true,
|
|
93
|
+
hasUserVerification: true,
|
|
94
|
+
isUserVerified: true,
|
|
95
|
+
automaticPresenceSimulation: true,
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
session,
|
|
99
|
+
)
|
|
100
|
+
).authenticatorId
|
|
101
|
+
const encoded = base64Bytes(key)
|
|
102
|
+
try {
|
|
103
|
+
const before = JSON.stringify({
|
|
104
|
+
authenticatorId,
|
|
105
|
+
credential: {
|
|
106
|
+
credentialId: passkey.credentialId,
|
|
107
|
+
isResidentCredential: true,
|
|
108
|
+
rpId: passkey.rpId,
|
|
109
|
+
...(passkey.userHandle === undefined ? {} : { userHandle: passkey.userHandle }),
|
|
110
|
+
signCount: passkey.signCount,
|
|
111
|
+
privateKey: "",
|
|
112
|
+
},
|
|
113
|
+
})
|
|
114
|
+
const cut = before.lastIndexOf('""')
|
|
115
|
+
await cdp
|
|
116
|
+
.callWithSecret(
|
|
117
|
+
"WebAuthn.addCredential",
|
|
118
|
+
before.slice(0, cut + 1),
|
|
119
|
+
encoded,
|
|
120
|
+
before.slice(cut + 1),
|
|
121
|
+
session,
|
|
122
|
+
)
|
|
123
|
+
.catch(() => {
|
|
124
|
+
throw new PageError("rejected")
|
|
125
|
+
})
|
|
126
|
+
} finally {
|
|
127
|
+
encoded.fill(0)
|
|
128
|
+
}
|
|
129
|
+
} finally {
|
|
130
|
+
key.fill(0)
|
|
131
|
+
}
|
|
132
|
+
channel.reply({ ready: { origin } })
|
|
133
|
+
const reason = await outcome(cdp, channel, {
|
|
134
|
+
session,
|
|
135
|
+
authenticatorId,
|
|
136
|
+
origin,
|
|
137
|
+
frameId: tab.frameId,
|
|
138
|
+
seconds: passkey.seconds,
|
|
139
|
+
})
|
|
140
|
+
await removeAuthenticator(cdp, authenticatorId, session)
|
|
141
|
+
authenticatorId = undefined
|
|
142
|
+
channel.reply({ ended: reason })
|
|
143
|
+
} finally {
|
|
144
|
+
if (authenticatorId !== undefined) await removeAuthenticator(cdp, authenticatorId, session)
|
|
145
|
+
cdp.close()
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const removeAuthenticator = (cdp, authenticatorId, session) =>
|
|
150
|
+
cdp.call("WebAuthn.removeVirtualAuthenticator", { authenticatorId }, session).catch(() => {})
|
|
151
|
+
|
|
152
|
+
/** Why the authenticator goes away: used once, time up, the tab left the origin or closed, or the Host ended it. */
|
|
153
|
+
function outcome(cdp, channel, { session, authenticatorId, origin, frameId, seconds }) {
|
|
154
|
+
return new Promise((resolve) => {
|
|
155
|
+
const timer = setTimeout(() => resolve("expired"), seconds * 1000)
|
|
156
|
+
const finish = (reason) => {
|
|
157
|
+
clearTimeout(timer)
|
|
158
|
+
resolve(reason)
|
|
159
|
+
}
|
|
160
|
+
cdp.on((message) => {
|
|
161
|
+
if (message.method === "Target.detachedFromTarget" && message.params?.sessionId === session)
|
|
162
|
+
finish("closed")
|
|
163
|
+
if (message.sessionId !== session) return
|
|
164
|
+
if (
|
|
165
|
+
message.method === "WebAuthn.credentialAsserted" &&
|
|
166
|
+
message.params?.authenticatorId === authenticatorId
|
|
167
|
+
) {
|
|
168
|
+
finish("used")
|
|
169
|
+
}
|
|
170
|
+
if (
|
|
171
|
+
message.method === "Page.frameNavigated" &&
|
|
172
|
+
message.params?.frame?.id === frameId &&
|
|
173
|
+
message.params.frame.securityOrigin !== origin
|
|
174
|
+
) {
|
|
175
|
+
finish("navigated")
|
|
176
|
+
}
|
|
177
|
+
})
|
|
178
|
+
cdp.closed.then(() => finish("closed"))
|
|
179
|
+
channel.ended.then(() => finish("released"))
|
|
180
|
+
process.once("SIGTERM", () => finish("released"))
|
|
181
|
+
})
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
main().then(
|
|
185
|
+
() => process.exit(0),
|
|
186
|
+
(error) => {
|
|
187
|
+
process.stdout.write(
|
|
188
|
+
`${JSON.stringify({ error: error instanceof PageError ? error.code : "failed" })}\n`,
|
|
189
|
+
)
|
|
190
|
+
process.exit(1)
|
|
191
|
+
},
|
|
192
|
+
)
|