Skip to content

Commit

Permalink
Dev: Fix false positives for Netlify Forms (#973)
Browse files Browse the repository at this point in the history
* Dev: Fix false positives for Netlify Forms

* Add test for Netlify Form false positive

* Formatting
  • Loading branch information
RaeesBhatti authored Jul 1, 2020
1 parent 1091205 commit c96d432
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/commands/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const Command = require('../../utils/command')
const chalk = require('chalk')
const jwtDecode = require('jwt-decode')
const open = require('open')
const contentType = require('content-type')
const { NETLIFYDEV, NETLIFYDEVLOG, NETLIFYDEVWARN, NETLIFYDEVERR } = require('../../utils/logo')
const boxen = require('boxen')
const { createTunnel, connectTunnel } = require('../../utils/live-tunnel')
Expand Down Expand Up @@ -211,7 +212,12 @@ async function startProxy(settings = {}, addonUrls, configPath, projectDir, func

if (match) return serveRedirect(req, res, proxy, match, options)

if (req.method === 'POST' && !isInternal(req.url)) {
const ct = req.headers['content-type'] ? contentType.parse(req) : {}
if (
req.method === 'POST' &&
!isInternal(req.url) &&
(ct.type.endsWith('/x-www-form-urlencoded') || ct.type === 'multipart/form-data')
) {
return proxy.web(req, res, { target: functionsServer })
}

Expand Down Expand Up @@ -329,7 +335,13 @@ async function serveRedirect(req, res, proxy, match, options) {
return handler(req, res, {})
}

if (req.method === 'POST' && !isInternal(req.url) && !isInternal(destURL)) {
const ct = req.headers['content-type'] ? contentType.parse(req) : {}
if (
req.method === 'POST' &&
!isInternal(req.url) &&
!isInternal(destURL) &&
(ct.type.endsWith('/x-www-form-urlencoded') || ct.type === 'multipart/form-data')
) {
return proxy.web(req, res, { target: options.functionsServer })
}

Expand Down
12 changes: 12 additions & 0 deletions tests/dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,18 @@ test('Netlify Forms support', async t => {
})
})

test('Netlify Forms: False positive', async t => {
const response = await fetch(`http://${host}/?ding=dong`, {
method: 'POST',
body: 'Something',
headers: {
'content-type': 'text/plain',
},
}).then(r => r.text())

t.deepEqual(response, 'Method Not Allowed')
})

test('functions env file overriding prod var', async t => {
const response = await fetch(`http://${host}/.netlify/functions/override-process-env`).then(r => r.text())

Expand Down

0 comments on commit c96d432

Please sign in to comment.