Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.

Commit

Permalink
feat(scoping): support test scopes (#15)
Browse files Browse the repository at this point in the history
* feat(scoping): support test scopes

* bump lock-file
  • Loading branch information
cawalch authored Sep 8, 2022
1 parent 420db84 commit 8e06ee0
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"vm2": "^3.9.4"
},
"devDependencies": {
"@merrymaker/types": "^1.0.6",
"@merrymaker/types": "^1.0.10",
"@types/bull": "^3.15.0",
"@types/jest": "^27.0.2",
"@types/node": "^14.0.27",
Expand Down
46 changes: 36 additions & 10 deletions src/lib/vm-runner.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
import events from 'events'
import crypto from 'crypto'
import { VM } from 'vm2'
import puppeteer, { Page, Browser, ConsoleMessage, WebWorker, HTTPResponse, HTTPRequest } from 'puppeteer'
import puppeteer, {
Page,
Browser,
ConsoleMessage,
WebWorker,
HTTPResponse,
HTTPRequest
} from 'puppeteer'

export type RunnerOpts = {
code: string
scan_id: string
test: boolean
config: {
browserAgs: string[]
timeout: number
}
}

import hooks from '../hooks.js'
import {
Expand Down Expand Up @@ -29,6 +46,8 @@ export default class vmRunner {
vm: VM
// Scan ID
scanID: string
// run in test mode
test: boolean
// event emitter for all browser events
browserEvent: events.EventEmitter
// current page
Expand All @@ -50,12 +69,14 @@ export default class vmRunner {
* and runs puppeteer script
*/
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
public runner = async (
code: string,
scanID: string,
config: { browserAgs: string[]; timeout: number }
) => {
this.scanID = scanID
public runner = async ({
scan_id,
code,
test,
config,
}) => {
this.scanID = scan_id
this.test = test
this.totalEvents = 0
this.events = []
this.failure = ''
Expand Down Expand Up @@ -105,7 +126,11 @@ export default class vmRunner {
})

this.page.on('requestfailed', (request: HTTPRequest) => {
this.pageError({ message: `Request failed: ${request.url()} - ${request.failure().errorText}` })
this.pageError({
message: `Request failed: ${request.url()} - ${
request.failure().errorText
}`
})
})

// handle console log messages
Expand Down Expand Up @@ -173,7 +198,7 @@ export default class vmRunner {
private async htmlSnapshot(page: Page): Promise<void> {
const url = page.url()
const html = await page.content()
page.emit('htmlsnapshot', {url, html})
page.emit('htmlsnapshot', { url, html })
}

/**
Expand Down Expand Up @@ -232,6 +257,7 @@ export default class vmRunner {
this.totalEvents += 1
this.browserEvent.emit('scan-event', {
scanID: this.scanID,
test: this.test,
type: eType,
payload
} as ScanEvent)
Expand All @@ -250,7 +276,7 @@ export default class vmRunner {
private active() {
this.browserEvent.emit('scan-event', {
scanID: this.scanID,
type: 'active',
type: 'active'
})
}

Expand Down
11 changes: 8 additions & 3 deletions src/tests/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ describe('Simple Hooks', () => {
done()
}
})
browserWorker.runner(source, 'test-run', {
browserAgs: config.puppeteer.args,
timeout: config.puppeteer.timeout
browserWorker.runner({
code: source,
scan_id: 'test-run123',
test: false,
config: {
browserAgs: config.puppeteer.args,
timeout: config.puppeteer.timeout
}
})
})

Expand Down
11 changes: 8 additions & 3 deletions src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@ async function work(job: Job) {
await job.discard()
throw new Error('No source')
}
return browserWorker.runner(source, job.data.scan_id, {
browserAgs: config.puppeteer.args,
timeout: config.puppeteer.timeout
return browserWorker.runner({
code: source,
scan_id: job.data.scan_id,
test: job.data.test,
config: {
browserAgs: config.puppeteer.args,
timeout: config.puppeteer.timeout
}
})
}

Expand Down
17 changes: 5 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -565,12 +565,12 @@
"@jridgewell/resolve-uri" "^3.0.3"
"@jridgewell/sourcemap-codec" "^1.4.10"

"@merrymaker/types@^1.0.6":
version "1.0.6"
resolved "https://registry.yarnpkg.com/@merrymaker/types/-/types-1.0.6.tgz#8c2410decac82099b65c94b9692b6b747f7ae77a"
integrity sha512-AlSLrAUh6rSnp1ckhgUrt4m9DaSfphI4zDVLjNZ+uCIePhj4+eR0qn03tOgVMe3JqhpvpEGVG9F6LjVcBw2Uuw==
"@merrymaker/types@^1.0.10":
version "1.0.10"
resolved "https://registry.yarnpkg.com/@merrymaker/types/-/types-1.0.10.tgz#4691e42a8bc836367a76e2efe3f47088c6acf30f"
integrity sha512-MFSmUP2SNQq5A9/yw85yyJyJYt9aBUwrtqOgrEXDDt/9XZJH4ufFv3wuro021bJhOCkBEI81s9V5mz7dwuNv7w==
dependencies:
"@types/puppeteer" "^3.0.1"
"@types/puppeteer" "^5.4.4"

"@nodelib/fs.scandir@2.1.5":
version "2.1.5"
Expand Down Expand Up @@ -740,13 +740,6 @@
resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-2.4.4.tgz#5d9b63132df54d8909fce1c3f8ca260fdd693e17"
integrity sha512-ReVR2rLTV1kvtlWFyuot+d1pkpG2Fw/XKE3PDAdj57rbM97ttSp9JZ2UsP+2EHTylra9cUf6JA7tGwW1INzUrA==

"@types/puppeteer@^3.0.1":
version "3.0.7"
resolved "https://registry.yarnpkg.com/@types/puppeteer/-/puppeteer-3.0.7.tgz#43e5baa3a2d515d895d5a01b50b2b523ff76b966"
integrity sha512-fwmQHrM+CbJd/ZUD4IBXmEKMF3nPMLQL+CaTSfzjo/ayElg8AIzFLcJZ81nCFR47RxLJRdAuSydtlky7N1cwkw==
dependencies:
"@types/node" "*"

"@types/puppeteer@^5.4.4":
version "5.4.5"
resolved "https://registry.yarnpkg.com/@types/puppeteer/-/puppeteer-5.4.5.tgz#154e3850a77bfd3967f036680de8ddc88eb3a12b"
Expand Down

0 comments on commit 8e06ee0

Please sign in to comment.