Skip to content
This repository was archived by the owner on Jul 8, 2025. It is now read-only.

Commit 1b9c875

Browse files
authored
fix: Add headers and utm params to CA request (#186)
1 parent 4ea91fd commit 1b9c875

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"@types/mocha": "^5.2.7",
4848
"@types/node": "^12.7.5",
4949
"@types/node-fetch": "^2.5.7",
50+
"@types/uuid": "^8.3.0",
5051
"chai": "^4.2.0",
5152
"fake-exec": "^1.1.0",
5253
"mocha": "^6.2.0",

src/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class Config
1616
home_dir: string;
1717
uuid: string;
1818
golang_executable: string;
19+
utm_source: string;
20+
telemetry_id: string;
1921

2022
constructor() {
2123
// TODO: this needs to be configurable
@@ -28,6 +30,8 @@ class Config
2830
this.home_dir = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
2931
this.uuid = process.env.UUID || "";
3032
this.golang_executable = process.env.GOLANG_EXECUTABLE || 'go';
33+
this.utm_source = process.env.UTM_SOURCE || "";
34+
this.telemetry_id = process.env.TELEMETRY_ID || "";
3135
}
3236
};
3337

src/server.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
'use strict';
66
import * as path from 'path';
77
import * as fs from 'fs';
8+
import * as uuid from 'uuid';
9+
import * as crypto from "crypto";
10+
811
import {
9-
IPCMessageReader, IPCMessageWriter, createConnection, IConnection,
10-
TextDocuments, InitializeResult, CodeLens, CodeAction, CodeActionKind} from 'vscode-languageserver';
12+
IPCMessageReader, IPCMessageWriter, createConnection, IConnection,
13+
TextDocuments, InitializeResult, CodeLens, CodeAction, CodeActionKind} from 'vscode-languageserver';
1114
import fetch from 'node-fetch';
1215
import url from 'url';
1316

@@ -171,7 +174,7 @@ const getCAmsg = (deps, diagnostics, totalCount): string => {
171174
const caDefaultMsg = 'Checking for security vulnerabilities ...';
172175

173176
/* Fetch Vulnerabilities by component-analysis batch api-call */
174-
const fetchVulnerabilities = async (reqData) => {
177+
const fetchVulnerabilities = async (reqData: any, manifestHash: string, requestId: string) => {
175178
let url = config.server_url;
176179
if (config.three_scale_user_token) {
177180
url += `/component-analyses/?user_key=${config.three_scale_user_token}`;
@@ -181,10 +184,23 @@ const fetchVulnerabilities = async (reqData) => {
181184
const headers = {
182185
'Content-Type': 'application/json',
183186
'Authorization': 'Bearer ' + config.api_token,
187+
'X-Request-Id': requestId,
184188
};
189+
190+
url += `&utm_content=${manifestHash}`;
191+
192+
if (config.utm_source) {
193+
url += `&utm_source=${config.utm_source}`;
194+
}
195+
185196
if (config.uuid) {
186197
headers['uuid'] = config.uuid;
187198
}
199+
200+
if (config.telemetry_id) {
201+
headers['X-Telemetry-Id'] = config.telemetry_id;
202+
}
203+
188204
try {
189205
const response = await fetch(url , {
190206
method: 'post',
@@ -271,6 +287,8 @@ const sendDiagnostics = async (ecosystem: string, diagnosticFilePath: string, co
271287
const diagnostics = [];
272288
const totalCount = new TotalCount();
273289
const start = new Date().getTime();
290+
const manifestHash = crypto.createHash("sha256").update(diagnosticFilePath).digest("hex");
291+
const requestId = uuid.v4();
274292
// Closure which captures common arg to runPipeline.
275293
const pipeline = response => runPipeline(response, diagnostics, packageAggregator, diagnosticFilePath, pkgMap, totalCount);
276294
// Get and fire diagnostics for items found in Cache.
@@ -289,7 +307,7 @@ const sendDiagnostics = async (ecosystem: string, diagnosticFilePath: string, co
289307
pipeline(response);
290308
}
291309
const allRequests = slicePayload(requestPayload, batchSize, ecosystem).
292-
map(request => fetchVulnerabilities(request).then(cacheAndRunPipeline));
310+
map(request => fetchVulnerabilities(request, manifestHash, requestId).then(cacheAndRunPipeline));
293311

294312
await Promise.allSettled(allRequests);
295313
const end = new Date().getTime();
@@ -341,7 +359,7 @@ connection.onCodeAction((params, token): CodeAction[] => {
341359
let codeAction = codeActionsMap[diagnostic.range.start.line + "|" + diagnostic.range.start.character];
342360
if (codeAction != null) {
343361
codeActions.push(codeAction);
344-
362+
345363
}
346364
if (!hasAnalyticsDiagonostic) {
347365
hasAnalyticsDiagonostic = diagnostic.source === AnalyticsSource;

0 commit comments

Comments
 (0)