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

chore: try to fix ci issue #396

Merged
merged 7 commits into from
Dec 30, 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
88 changes: 52 additions & 36 deletions test/fix-GHSA-v2v2-hph8-q5xp.test.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,63 @@
'use strict'

const t = require('tap')
const { describe, after, it } = require('node:test')
const fastify = require('fastify')
const get = require('simple-get').concat
const From = require('..')

const upstream = fastify()
t.teardown(upstream.close.bind(upstream))
t.plan(4)

upstream.post('/test', async (request, reply) => {
if (typeof request.body === 'object') {
return 'not ok'
}
return 'ok'
})
const fastifyProxyFrom = require('..')
const { isIPv6 } = require('node:net')

describe('GHSA-v2v2-hph8-q5xp', function () {
it('should not parse the body if it is an object', async function (t) {
t.plan(1)

const upstream = fastify()

upstream.post('/test', async (request, reply) => {
if (typeof request.body === 'object') {
return 'not ok'
}
return 'ok'
})

upstream.listen({ port: 0 }, function (err) {
t.error(err)
await upstream.listen({ port: 0 })

const app = fastify()
app.register(From)
t.teardown(app.close.bind(app))
let upstreamAdress = upstream.server.address().address

app.post('/test', (request, reply) => {
if (request.body.method === 'invalid_method') {
return reply.code(400).send({ message: 'payload contains invalid method' })
if (isIPv6(upstreamAdress)) {
upstreamAdress = `[${upstreamAdress}]`
}
reply.from(`http://127.0.0.1:${upstream.server.address().port}/test`)
})

app.listen({ port: 0 }, function (err) {
t.error(err)

get({
url: `http://127.0.0.1:${app.server.address().port}/test`,
headers: { 'content-type': 'application/json ; charset=utf-8' },
// eslint-disable-next-line no-useless-escape
body: '"{\\\"method\\\":\\\"invalid_method\\\"}"',
method: 'POST'
}, (err, res, data) => {
t.error(err)
t.equal(data.toString(), 'ok')
const app = fastify()
app.register(fastifyProxyFrom)

app.post('/test', (request, reply) => {
if (request.body.method === 'invalid_method') {
return reply.code(400).send({ message: 'payload contains invalid method' })
}
reply.from(`http://${upstreamAdress}:${upstream.server.address().port}/test`)
})

await app.listen({ port: 0 })

after(() => {
upstream.close()
app.close()
})

let appAddress = app.server.address().address

if (isIPv6(appAddress)) {
appAddress = `[${appAddress}]`
}

const response = await fetch(
`http://${appAddress}:${app.server.address().port}/test`,
{
headers: { 'content-type': 'application/json ; charset=utf-8' },
// eslint-disable-next-line no-useless-escape
body: '"{\\\"method\\\":\\\"invalid_method\\\"}"',
method: 'POST'
})

t.assert.strictEqual(await response.text(), 'ok')
})
})
71 changes: 45 additions & 26 deletions test/undici-proxy-agent.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict'

const { test } = require('tap')
const { test, after } = require('node:test')
const { createServer } = require('node:http')
const Fastify = require('fastify')
const get = require('simple-get').concat
const { createProxy } = require('proxy')
const From = require('..')
const fastifyProxyFrom = require('..')
const { isIPv6 } = require('node:net')

const configFormat = {
string: (value) => value,
Expand All @@ -15,17 +15,33 @@ const configFormat = {

for (const [description, format] of Object.entries(configFormat)) {
test(`use undici ProxyAgent to connect through proxy - configured via ${description}`, async (t) => {
t.plan(5)
t.plan(3)

const target = await buildServer()
const proxy = await buildProxy()
t.teardown(target.close.bind(target))
t.teardown(proxy.close.bind(proxy))

const targetUrl = `http://localhost:${target.address().port}`
const proxyUrl = `http://localhost:${proxy.address().port}`
after(() => {
target.close()
proxy.close()
})

let targetAddress = target.address().address

if (isIPv6(targetAddress)) {
targetAddress = `[${targetAddress}]`
}

let proxyAddress = proxy.address().address

if (isIPv6(proxyAddress)) {
proxyAddress = `[${proxyAddress}]`
}

const targetUrl = `http://${targetAddress}:${target.address().port}`
const proxyUrl = `http://${proxyAddress}:${proxy.address().port}`

proxy.on('connect', () => {
t.ok(true, 'should connect to proxy')
t.assert.ok(true, 'should connect to proxy')
})

target.on('request', (req, res) => {
Expand All @@ -34,9 +50,12 @@ for (const [description, format] of Object.entries(configFormat)) {
})

const instance = Fastify()
t.teardown(instance.close.bind(instance))

instance.register(From, {
after(() => {
instance.close()
})

instance.register(fastifyProxyFrom, {
base: targetUrl,
undici: {
proxy: format(proxyUrl)
Expand All @@ -47,22 +66,22 @@ for (const [description, format] of Object.entries(configFormat)) {
reply.from()
})

const executionFlow = () => new Promise((resolve) => {
instance.listen({ port: 0 }, err => {
t.error(err)

get(`http://localhost:${instance.server.address().port}`, (err, res, data) => {
t.error(err)
t.same(res.statusCode, 200)
t.match(JSON.parse(data.toString()), { hello: 'world' })
resolve()
instance.close()
target.close()
})
})
})
await instance.listen({ port: 0 })

let instanceAddress = proxy.address().address

if (isIPv6(instanceAddress)) {
if (instanceAddress === '::') {
instanceAddress = '::1'
} else {
instanceAddress = `[${instanceAddress}]`
}
}

const response = await fetch(`http://localhost:${instance.server.address().port}`)

await executionFlow()
t.assert.strictEqual(response.status, 200)
t.assert.deepStrictEqual(await response.json(), { hello: 'world' })
})
}

Expand Down
Loading