From 89eaee748b1c660c0d57ebac1fbdd3e4063713d8 Mon Sep 17 00:00:00 2001 From: Yassin Kammoun <52890329+yassin-kammoun-sonarsource@users.noreply.github.com> Date: Wed, 23 Sep 2020 10:01:39 +0200 Subject: [PATCH] Fix SSF-122 (#2161) (cherry picked from commit 5f1f8e8a49e481816fd0d5a82b700009c539a186) --- eslint-bridge/bin/server | 2 +- eslint-bridge/src/server.ts | 9 +++++---- eslint-bridge/tests/server.test.ts | 2 +- .../javascript/eslint/EslintBridgeServerImpl.java | 4 ++-- .../src/test/resources/mock-eslint-bridge/badResponse.js | 5 +++-- .../src/test/resources/mock-eslint-bridge/logging.js | 5 +++-- .../src/test/resources/mock-eslint-bridge/missingTs.js | 5 +++-- .../src/test/resources/mock-eslint-bridge/startServer.js | 5 +++-- .../src/test/resources/mock-eslint-bridge/timeout.js | 5 +++-- .../test/resources/mock-eslint-bridge/tsConfigError.js | 5 +++-- 10 files changed, 27 insertions(+), 20 deletions(-) diff --git a/eslint-bridge/bin/server b/eslint-bridge/bin/server index 038b777c659..eabf482472e 100644 --- a/eslint-bridge/bin/server +++ b/eslint-bridge/bin/server @@ -1,4 +1,4 @@ #!/usr/bin/env node const server = require("../lib/server"); -server.start(process.argv[2]); \ No newline at end of file +server.start(process.argv[2], process.argv[3]); diff --git a/eslint-bridge/src/server.ts b/eslint-bridge/src/server.ts index dff80043fb4..a8dee5887f0 100644 --- a/eslint-bridge/src/server.ts +++ b/eslint-bridge/src/server.ts @@ -34,17 +34,18 @@ import { getFilesForTsConfig } from './tsconfig'; const MAX_REQUEST_SIZE = '50mb'; -export function start(port = 0): Promise { - return startServer(port, analyzeJavaScript, analyzeTypeScript); +export function start(port = 0, host = '127.0.0.1'): Promise { + return startServer(analyzeJavaScript, analyzeTypeScript, port, host); } type AnalysisFunction = (input: AnalysisInput) => AnalysisResponse; // exported for test export function startServer( - port = 0, analyzeJS: AnalysisFunction, analyzeTS: AnalysisFunction, + port = 0, + host = '127.0.0.1', ): Promise { return new Promise(resolve => { console.log('DEBUG starting eslint-bridge server at port', port); @@ -82,7 +83,7 @@ export function startServer( }); }); - server = app.listen(port, () => { + server = app.listen(port, host, () => { console.log( 'DEBUG eslint-bridge server is running at port', (server.address() as AddressInfo).port, diff --git a/eslint-bridge/tests/server.test.ts b/eslint-bridge/tests/server.test.ts index b378ca45de4..07b1d16027f 100644 --- a/eslint-bridge/tests/server.test.ts +++ b/eslint-bridge/tests/server.test.ts @@ -303,7 +303,7 @@ describe('should send error when failing', () => { let close; beforeEach(async () => { - server = await startServer(0, failAnalysis, failAnalysis); + server = await startServer(failAnalysis, failAnalysis, 0, '127.0.0.1'); close = promisify(server.close.bind(server)); }); diff --git a/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/eslint/EslintBridgeServerImpl.java b/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/eslint/EslintBridgeServerImpl.java index 9ba7cc1d95b..2b369f5bdfa 100644 --- a/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/eslint/EslintBridgeServerImpl.java +++ b/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/eslint/EslintBridgeServerImpl.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.io.InterruptedIOException; import java.nio.file.Files; +import java.net.InetAddress; import java.nio.file.Path; import java.nio.file.Paths; import java.time.Duration; @@ -37,7 +38,6 @@ import okhttp3.RequestBody; import okhttp3.Response; import org.sonar.api.SonarProduct; -import org.sonar.api.batch.fs.FileSystem; import org.sonar.api.batch.sensor.SensorContext; import org.sonar.api.config.Configuration; import org.sonar.api.utils.log.Logger; @@ -130,7 +130,7 @@ private void initNodeCommand(SensorContext context, File scriptFile) throws IOEx .minNodeVersion(8) .configuration(context.config()) .script(scriptFile.getAbsolutePath()) - .scriptArgs(String.valueOf(port)); + .scriptArgs(String.valueOf(port), InetAddress.getLoopbackAddress().getHostAddress()); context.config() .getInt(MAX_OLD_SPACE_SIZE_PROPERTY) diff --git a/sonar-javascript-plugin/src/test/resources/mock-eslint-bridge/badResponse.js b/sonar-javascript-plugin/src/test/resources/mock-eslint-bridge/badResponse.js index d2cccbb7e63..61fa973d5a7 100644 --- a/sonar-javascript-plugin/src/test/resources/mock-eslint-bridge/badResponse.js +++ b/sonar-javascript-plugin/src/test/resources/mock-eslint-bridge/badResponse.js @@ -6,6 +6,7 @@ const http = require('http') const port = process.argv[2] +const host = process.argv[3] const requestHandler = (request, response) => { if (request.url == '/status') { @@ -18,10 +19,10 @@ const requestHandler = (request, response) => { const server = http.createServer(requestHandler) -server.listen(port, (err) => { +server.listen(port, host, (err) => { if (err) { return console.log('something bad happened', err) } - console.log(`server is listening on ${port}`) + console.log(`server is listening on ${host} ${port}`); }) diff --git a/sonar-javascript-plugin/src/test/resources/mock-eslint-bridge/logging.js b/sonar-javascript-plugin/src/test/resources/mock-eslint-bridge/logging.js index 6abbccf6e93..c47d14ab7a7 100644 --- a/sonar-javascript-plugin/src/test/resources/mock-eslint-bridge/logging.js +++ b/sonar-javascript-plugin/src/test/resources/mock-eslint-bridge/logging.js @@ -2,6 +2,7 @@ const http = require('http') const port = process.argv[2] +const host = process.argv[3] console.log(`DEBUG testing debug log`) console.log(`WARN testing warn log`) @@ -13,6 +14,6 @@ const server = http.createServer((req, res) => { res.end('OK!'); }) -server.listen(port, () => { - console.log(`server is listening on ${port}`) +server.listen(port, host, () => { + console.log(`server is listening on ${host} ${port}`); }) diff --git a/sonar-javascript-plugin/src/test/resources/mock-eslint-bridge/missingTs.js b/sonar-javascript-plugin/src/test/resources/mock-eslint-bridge/missingTs.js index a6beb4ba32d..4b8aef49fc6 100644 --- a/sonar-javascript-plugin/src/test/resources/mock-eslint-bridge/missingTs.js +++ b/sonar-javascript-plugin/src/test/resources/mock-eslint-bridge/missingTs.js @@ -2,6 +2,7 @@ const http = require("http"); const port = process.argv[2]; +const host = process.argv[3]; const requestHandler = (request, response) => { if (request.url === "/status") { @@ -17,10 +18,10 @@ const requestHandler = (request, response) => { const server = http.createServer(requestHandler); -server.listen(port, err => { +server.listen(port, host, err => { if (err) { return console.log("something bad happened", err); } - console.log(`server is listening on ${port}`); + console.log(`server is listening on ${host} ${port}`); }); diff --git a/sonar-javascript-plugin/src/test/resources/mock-eslint-bridge/startServer.js b/sonar-javascript-plugin/src/test/resources/mock-eslint-bridge/startServer.js index 4904b964d62..54f5c5254e5 100644 --- a/sonar-javascript-plugin/src/test/resources/mock-eslint-bridge/startServer.js +++ b/sonar-javascript-plugin/src/test/resources/mock-eslint-bridge/startServer.js @@ -6,6 +6,7 @@ const http = require('http'); const port = process.argv[2]; +const host = process.argv[3]; const requestHandler = (request, response) => { if (request.url === '/status' || request.url === '/new-tsconfig') { @@ -20,10 +21,10 @@ const requestHandler = (request, response) => { const server = http.createServer(requestHandler); -server.listen(port, (err) => { +server.listen(port, host, (err) => { if (err) { return console.log('something bad happened', err) } - console.log(`server is listening on ${port}`) + console.log(`server is listening on ${host} ${port}`); }); diff --git a/sonar-javascript-plugin/src/test/resources/mock-eslint-bridge/timeout.js b/sonar-javascript-plugin/src/test/resources/mock-eslint-bridge/timeout.js index 55e38058c75..0813adbc16f 100644 --- a/sonar-javascript-plugin/src/test/resources/mock-eslint-bridge/timeout.js +++ b/sonar-javascript-plugin/src/test/resources/mock-eslint-bridge/timeout.js @@ -2,6 +2,7 @@ const http = require("http"); const port = process.argv[2]; +const host = process.argv[3] let server; @@ -19,12 +20,12 @@ const requestHandler = async (request, response) => { server = http.createServer(requestHandler); -server.listen(port, err => { +server.listen(port, host, err => { if (err) { return console.log("something bad happened", err); } - console.log(`server is listening on ${port}`); + console.log(`server is listening on ${host} ${port}`); }); function sleep(ms) { diff --git a/sonar-javascript-plugin/src/test/resources/mock-eslint-bridge/tsConfigError.js b/sonar-javascript-plugin/src/test/resources/mock-eslint-bridge/tsConfigError.js index 741fa00808d..be1c9015582 100644 --- a/sonar-javascript-plugin/src/test/resources/mock-eslint-bridge/tsConfigError.js +++ b/sonar-javascript-plugin/src/test/resources/mock-eslint-bridge/tsConfigError.js @@ -2,6 +2,7 @@ const http = require("http"); const port = process.argv[2]; +const host = process.argv[3] const requestHandler = (request, response) => { if (request.url === "/status") { @@ -16,10 +17,10 @@ const requestHandler = (request, response) => { const server = http.createServer(requestHandler); -server.listen(port, err => { +server.listen(port, host, err => { if (err) { return console.log("something bad happened", err); } - console.log(`server is listening on ${port}`); + console.log(`server is listening on ${host} ${port}`); });