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

remove most get-port usage in appsec tests #4428

Merged
merged 5 commits into from
Jun 24, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const getPort = require('get-port')
const agent = require('../plugins/agent')
const {
schema,
Expand Down Expand Up @@ -41,10 +40,9 @@ withVersions('apollo-server-core', 'express', '>=4', expressVersion => {

server.applyMiddleware({ app })

config.port = await getPort()

return new Promise(resolve => {
expressServer = app.listen({ port: config.port }, () => {
config.port = expressServer.address().port
resolve()
})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const getPort = require('get-port')
const agent = require('../plugins/agent')
const {
schema,
Expand Down Expand Up @@ -41,10 +40,9 @@ withVersions('apollo-server-core', 'fastify', '3', fastifyVersion => {

app.register(server.createHandler())

config.port = await getPort()

return new Promise(resolve => {
app.listen({ port: config.port }, (data) => {
config.port = app.server.address().port
resolve()
})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const getPort = require('get-port')
const path = require('path')
const agent = require('../plugins/agent')
const {
Expand Down Expand Up @@ -31,9 +30,9 @@ withVersions('apollo-server', '@apollo/server', apolloServerVersion => {
resolvers
})

config.port = await getPort()
const { url } = await startStandaloneServer(server, { listen: { port: 0 } })

await startStandaloneServer(server, { listen: { port: config.port } })
config.port = new URL(url).port
})

after(async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const getPort = require('get-port')
const agent = require('../../../../plugins/agent')
const {
schema,
Expand Down Expand Up @@ -41,10 +40,9 @@ withVersions('graphql', 'express', '>=4', expressVersion => {

server.applyMiddleware({ app })

config.port = await getPort()

return new Promise(resolve => {
expressServer = app.listen({ port: config.port }, () => {
config.port = expressServer.address().port
resolve()
})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const getPort = require('get-port')
const path = require('path')
const agent = require('../../../../plugins/agent')
const {
Expand Down Expand Up @@ -31,9 +30,9 @@ withVersions('apollo-server', '@apollo/server', apolloServerVersion => {
resolvers
})

config.port = await getPort()
const { url } = await startStandaloneServer(server, { listen: { port: config.port } })

await startStandaloneServer(server, { listen: { port: config.port } })
config.port = new URL(url).port
})

after(async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const axios = require('axios')
const getPort = require('get-port')
const semver = require('semver')
const agent = require('../../../../plugins/agent')
const Config = require('../../../../../src/config')
Expand Down Expand Up @@ -59,13 +58,13 @@ describe('URI sourcing with express', () => {
res.status(200).send()
})

getPort().then(port => {
appListener = app.listen(port, 'localhost', () => {
axios
.get(`http://localhost:${port}/path/vulnerable`)
.then(() => done())
.catch(done)
})
appListener = app.listen(0, 'localhost', () => {
const port = appListener.address().port

axios
.get(`http://localhost:${port}/path/vulnerable`)
.then(() => done())
.catch(done)
})
})
})
Expand Down Expand Up @@ -137,13 +136,13 @@ describe('Path params sourcing with express', () => {
res.status(200).send()
})

getPort().then(port => {
appListener = app.listen(port, 'localhost', () => {
axios
.get(`http://localhost:${port}/tainted1/tainted2`)
.then(() => done())
.catch(done)
})
appListener = app.listen(0, 'localhost', () => {
const port = appListener.address().port

axios
.get(`http://localhost:${port}/tainted1/tainted2`)
.then(() => done())
.catch(done)
})
})

Expand Down Expand Up @@ -172,13 +171,13 @@ describe('Path params sourcing with express', () => {

app.use('/:parameterParent', nestedRouter)

getPort().then(port => {
appListener = app.listen(port, 'localhost', () => {
axios
.get(`http://localhost:${port}/tainted1/tainted2`)
.then(() => done())
.catch(done)
})
appListener = app.listen(0, 'localhost', () => {
const port = appListener.address().port

axios
.get(`http://localhost:${port}/tainted1/tainted2`)
.then(() => done())
.catch(done)
})
})

Expand All @@ -192,13 +191,13 @@ describe('Path params sourcing with express', () => {
app.param('parameter1', checkParamIsTaintedAndNext)
app.param('parameter2', checkParamIsTaintedAndNext)

getPort().then(port => {
appListener = app.listen(port, 'localhost', () => {
axios
.get(`http://localhost:${port}/tainted1/tainted2`)
.then(() => done())
.catch(done)
})
appListener = app.listen(0, 'localhost', () => {
const port = appListener.address().port

axios
.get(`http://localhost:${port}/tainted1/tainted2`)
.then(() => done())
.catch(done)
})
})

Expand All @@ -216,13 +215,13 @@ describe('Path params sourcing with express', () => {
app.param('parameter1')
app.param('parameter2')

getPort().then(port => {
appListener = app.listen(port, 'localhost', () => {
axios
.get(`http://localhost:${port}/tainted1/tainted2`)
.then(() => done())
.catch(done)
})
appListener = app.listen(0, 'localhost', () => {
const port = appListener.address().port

axios
.get(`http://localhost:${port}/tainted1/tainted2`)
.then(() => done())
.catch(done)
})
})
})
Expand Down
32 changes: 12 additions & 20 deletions packages/dd-trace/test/appsec/iast/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const fs = require('fs')
const os = require('os')
const path = require('path')

const getPort = require('get-port')
const agent = require('../../plugins/agent')
const axios = require('axios')
const iast = require('../../../src/appsec/iast')
Expand All @@ -17,12 +16,6 @@ function testInRequest (app, tests) {
let appListener
const config = {}

beforeEach(() => {
return getPort().then(newPort => {
config.port = newPort
})
})

beforeEach(() => {
listener = (req, res) => {
const appResult = app && app(req, res)
Expand All @@ -48,7 +41,10 @@ function testInRequest (app, tests) {
beforeEach(done => {
const server = new http.Server(listener)
appListener = server
.listen(config.port, 'localhost', () => done())
.listen(0, 'localhost', () => {
config.port = appListener.address().port
done()
})
})

afterEach(() => {
Expand Down Expand Up @@ -219,12 +215,6 @@ function prepareTestServerForIast (description, tests, iastConfig) {
let appListener
let app

before(() => {
return getPort().then(newPort => {
config.port = newPort
})
})

before(() => {
listener = (req, res) => {
endResponse(res, app && app(req, res))
Expand All @@ -241,7 +231,10 @@ function prepareTestServerForIast (description, tests, iastConfig) {
before(done => {
const server = new http.Server(listener)
appListener = server
.listen(config.port, 'localhost', () => done())
.listen(0, 'localhost', () => {
config.port = appListener.address().port
done()
})
})

beforeEachIastTest(iastConfig)
Expand Down Expand Up @@ -311,11 +304,10 @@ function prepareTestServerForIastInExpress (description, expressVersion, loadMid
}

expressApp.all('/', listener)
getPort().then(newPort => {
config.port = newPort
server = expressApp.listen(newPort, () => {
done()
})

server = expressApp.listen(0, () => {
config.port = server.address().port
done()
})
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const axios = require('axios')
const getPort = require('get-port')
const path = require('path')
const agent = require('../plugins/agent')
const appsec = require('../../src/appsec')
Expand All @@ -27,11 +26,9 @@ withVersions('body-parser', 'body-parser', version => {
res.end('DONE')
})

getPort().then(newPort => {
port = newPort
server = app.listen(port, () => {
done()
})
server = app.listen(port, () => {
port = server.address().port
done()
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const { assert } = require('chai')
const axios = require('axios')
const getPort = require('get-port')
const path = require('path')
const agent = require('../plugins/agent')
const appsec = require('../../src/appsec')
Expand All @@ -28,11 +27,9 @@ withVersions('cookie-parser', 'cookie-parser', version => {
res.end('DONE')
})

getPort().then(newPort => {
port = newPort
server = app.listen(port, () => {
done()
})
server = app.listen(port, () => {
port = server.address().port
done()
})
})

Expand Down
9 changes: 3 additions & 6 deletions packages/dd-trace/test/appsec/index.express.plugin.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const axios = require('axios')
const getPort = require('get-port')
const path = require('path')
const agent = require('../plugins/agent')
const appsec = require('../../src/appsec')
Expand Down Expand Up @@ -45,11 +44,9 @@ withVersions('express', 'express', version => {
res.jsonp({ jsonResKey: 'jsonResValue' })
})

getPort().then(newPort => {
port = newPort
server = app.listen(port, () => {
done()
})
server = app.listen(port, () => {
port = server.address().port
done()
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const path = require('path')
const axios = require('axios')
const getPort = require('get-port')
const agent = require('../plugins/agent')
const appsec = require('../../src/appsec')
const Config = require('../../src/config')
Expand Down Expand Up @@ -69,11 +68,9 @@ describe('sequelize', () => {
res.json(users)
})

getPort().then(newPort => {
port = newPort
server = app.listen(newPort, () => {
done()
})
server = app.listen(0, () => {
port = server.address().port
done()
})
})

Expand Down
Loading
Loading