Skip to content

Commit d594a06

Browse files
authored
Fixing the header sanitisation to allow only Ascii characters. (#136)
1 parent 8f34727 commit d594a06

File tree

5 files changed

+99
-3
lines changed

5 files changed

+99
-3
lines changed

lib/Utils/CommonUtils.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ exports.getDefaultTestRunName = getDefaultTestRunName;
7979
exports.getDefaultRunDescription = getDefaultRunDescription;
8080
exports.validateTestRunParamsFromPipeline = validateTestRunParamsFromPipeline;
8181
exports.getAllFileErrors = getAllFileErrors;
82+
exports.sanitisePipelineNameHeader = sanitisePipelineNameHeader;
8283
const { v4: uuidv4 } = require('uuid');
8384
const util_1 = require("util");
8485
const GeneralConstants_1 = require("../Constants/GeneralConstants");
@@ -484,3 +485,26 @@ function getAllFileErrors(testObj) {
484485
}
485486
return fileErrors;
486487
}
488+
/**
489+
* This function returns the string with only ascii charaters, removing the non-ascii characters.
490+
* @param pipelineName - original pipeline name
491+
* @returns sanitised pipeline name with only ascii characters
492+
*/
493+
function sanitisePipelineNameHeader(pipelineName) {
494+
if (!pipelineName) {
495+
return pipelineName;
496+
}
497+
let result = "";
498+
for (const ch of pipelineName) {
499+
const code = ch.codePointAt(0);
500+
const allowed = (code >= 32 && code <= 126); // ASCII characters range, the only allowed characters in headers.
501+
if (allowed) {
502+
result += ch;
503+
}
504+
}
505+
result = result.trim();
506+
if (result.length == 0) {
507+
result = "-"; // this is what GH does when i try to give all non-ascii characters in the repo name.
508+
}
509+
return result;
510+
}

lib/Utils/FetchUtils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function httpClientRetries(urlSuffix, header, method, retries = 1, data, isUploa
8484
const runId = process.env.GITHUB_RUN_ID;
8585
const pipelineName = process.env.GITHUB_WORKFLOW || "Unknown Pipeline";
8686
const pipelineUri = `${githubBaseUrl}/${repository}/actions/runs/${runId}`;
87-
header['x-ms-pipeline-name'] = pipelineName; // setting these for patch calls.
87+
header['x-ms-pipeline-name'] = (0, CommonUtils_1.sanitisePipelineNameHeader)(pipelineName); // setting these for patch calls.
8888
header['x-ms-pipeline-uri'] = pipelineUri;
8989
httpResponse = yield httpClient.request(methodEnumToString[method], urlSuffix, data, header);
9090
}

src/Utils/CommonUtils.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,3 +444,27 @@ export function getAllFileErrors(testObj:TestModel | null): { [key: string]: str
444444

445445
return fileErrors;
446446
}
447+
448+
/**
449+
* This function returns the string with only ascii charaters, removing the non-ascii characters.
450+
* @param pipelineName - original pipeline name
451+
* @returns sanitised pipeline name with only ascii characters
452+
*/
453+
export function sanitisePipelineNameHeader(pipelineName: string | null): string | null {
454+
if(!pipelineName) {
455+
return pipelineName;
456+
}
457+
let result = "";
458+
for (const ch of pipelineName) {
459+
const code = ch.codePointAt(0)!;
460+
const allowed = (code >= 32 && code <= 126); // ASCII characters range, the only allowed characters in headers.
461+
if(allowed) {
462+
result += ch;
463+
}
464+
}
465+
result = result.trim();
466+
if(result.length == 0) {
467+
result = "-"; // this is what GH does when i try to give all non-ascii characters in the repo name.
468+
}
469+
return result;
470+
}

src/Utils/FetchUtils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { IHeaders, IHttpClientResponse } from 'typed-rest-client/Interfaces';
2-
import { errorCorrection, getResultObj, getUniqueId, sleep } from './CommonUtils';
2+
import { errorCorrection, getResultObj, getUniqueId, sanitisePipelineNameHeader, sleep } from './CommonUtils';
33
import { FetchCallType, correlationHeader } from './../models/UtilModels';
44
import * as httpc from 'typed-rest-client/HttpClient';
55
import { uploadFileData } from './FileUtils';
@@ -39,7 +39,7 @@ export async function httpClientRetries(urlSuffix : string, header : IHeaders, m
3939
const pipelineName = process.env.GITHUB_WORKFLOW || "Unknown Pipeline";
4040
const pipelineUri = `${githubBaseUrl}/${repository}/actions/runs/${runId}`;
4141

42-
header['x-ms-pipeline-name'] = pipelineName; // setting these for patch calls.
42+
header['x-ms-pipeline-name'] = sanitisePipelineNameHeader(pipelineName); // setting these for patch calls.
4343
header['x-ms-pipeline-uri'] = pipelineUri;
4444
httpResponse = await httpClient.request(methodEnumToString[method], urlSuffix, data, header);
4545
}

test/CommonUtils.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { sanitisePipelineNameHeader } from "../src/Utils/CommonUtils";
2+
describe("CommonUtils tests", () => {
3+
it.each([
4+
{
5+
input: "Pipeline@2025#Release$!",
6+
expected: "Pipeline@2025#Release$!"
7+
},
8+
{
9+
input: "Build_Definition-01 (Test) ",
10+
expected: "Build_Definition-01 (Test)"
11+
},
12+
{
13+
input: "Normal Name",
14+
expected: "Normal Name"
15+
},
16+
{
17+
input: "Special*&^%$#@!Characters",
18+
expected: "Special*&^%$#@!Characters"
19+
},
20+
{
21+
input: "",
22+
expected: ""
23+
},
24+
{
25+
input: " ",
26+
expected: "-"
27+
},
28+
{
29+
input: "Name_with_underscores_and-dashes",
30+
expected: "Name_with_underscores_and-dashes"
31+
},
32+
{
33+
input: null,
34+
expected: null
35+
},
36+
{
37+
input: "🚀 Deploy",
38+
expected: "Deploy"
39+
},
40+
{
41+
input: "流水线-test-𰻞",
42+
expected: "-test-"
43+
}
44+
])("sanitisePipelineNameHeader removes special characters", ({ input, expected }) => {
45+
const result = sanitisePipelineNameHeader(input);
46+
expect(result).toBe(expected);
47+
});
48+
});

0 commit comments

Comments
 (0)