Skip to content

Commit afd58a1

Browse files
CopilotMathiasVDA
andcommitted
Fix PowerShell variable expansion by not escaping $query in Body parameter
Co-authored-by: MathiasVDA <15101339+MathiasVDA@users.noreply.github.com>
1 parent b4d7e68 commit afd58a1

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

packages/yasqe/src/__tests__/share-test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ describe("Share Functionality", () => {
146146
expect(psString).to.include('"@');
147147
expect(psString).to.include(query);
148148
expect(psString).to.include('Body = "query=$query"');
149+
expect(psString).to.not.include('Body = "query=`$query"'); // Should NOT escape the variable
149150
expect(psString).to.include("sparql-generated");
150151
});
151152

packages/yasqe/src/sparql.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -546,15 +546,20 @@ export function getAsPowerShellString(yasqe: Yasqe, _config?: Config["requestCon
546546
}
547547

548548
// Build the body with the query variable and any other parameters
549-
const bodyParts: string[] = [];
550-
if (queryParam) {
551-
bodyParts.push(`${queryParamName}=$${queryParamName}`);
552-
}
553-
if (Object.keys(otherArgs).length > 0) {
549+
// Note: We don't escape the variable reference itself, only the other args
550+
let bodyExpression: string;
551+
if (queryParam && Object.keys(otherArgs).length > 0) {
552+
// Both query variable and other args
553+
const otherArgsString = queryString.stringify(otherArgs);
554+
bodyExpression = `"${queryParamName}=$${queryParamName}&${escapePowerShellString(otherArgsString)}"`;
555+
} else if (queryParam) {
556+
// Only query variable
557+
bodyExpression = `"${queryParamName}=$${queryParamName}"`;
558+
} else {
559+
// Only other args (shouldn't happen, but handle it)
554560
const otherArgsString = queryString.stringify(otherArgs);
555-
bodyParts.push(otherArgsString);
561+
bodyExpression = `"${escapePowerShellString(otherArgsString)}"`;
556562
}
557-
const body = bodyParts.join("&");
558563

559564
lines.push("$params = @{");
560565
lines.push(` Uri = "${escapePowerShellString(url)}"`);
@@ -565,7 +570,7 @@ export function getAsPowerShellString(yasqe: Yasqe, _config?: Config["requestCon
565570
lines.push(" }");
566571
}
567572
lines.push(` ContentType = "application/x-www-form-urlencoded"`);
568-
lines.push(` Body = "${escapePowerShellString(body)}"`);
573+
lines.push(` Body = ${bodyExpression}`);
569574
lines.push(` OutFile = "sparql-generated.${fileExtension}"`);
570575
lines.push("}");
571576
} else {

0 commit comments

Comments
 (0)