From c96d432a1b47fd3e3b2286f96a73d437b109f293 Mon Sep 17 00:00:00 2001 From: Raees Iqbal Date: Wed, 1 Jul 2020 14:56:32 +0500 Subject: [PATCH] Dev: Fix false positives for Netlify Forms (#973) * Dev: Fix false positives for Netlify Forms * Add test for Netlify Form false positive * Formatting --- src/commands/dev/index.js | 16 ++++++++++++++-- tests/dev.test.js | 12 ++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/commands/dev/index.js b/src/commands/dev/index.js index a9ac063f21a..e3d4be8aedf 100644 --- a/src/commands/dev/index.js +++ b/src/commands/dev/index.js @@ -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') @@ -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 }) } @@ -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 }) } diff --git a/tests/dev.test.js b/tests/dev.test.js index b5c430f5292..58bacdaade5 100644 --- a/tests/dev.test.js +++ b/tests/dev.test.js @@ -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())