Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some SAS token issues (1281) #1282

Merged
merged 1 commit into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/documents/templates/ChildTemplateCodeLens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { assert } from "../../fixed_assert";
import { Span } from '../../language/Span';
import { LanguageServerState } from '../../languageclient/startArmLanguageServer';
import { assertNever } from '../../util/assertNever';
import { parseUri, stringifyUri } from "../../util/uri";
import { parseUri } from "../../util/uri";
import { ResolvableCodeLens } from '../DeploymentDocument';
import { IParameterValuesSourceProvider } from '../parameters/IParameterValuesSourceProvider';
import { SelectParameterFileCodeLens } from "./deploymentTemplateCodeLenses";
Expand Down Expand Up @@ -161,7 +161,14 @@ export class LinkedTemplateCodeLens extends ChildTemplateCodeLens {
friendlyPath = `.${ext.pathSeparator}${friendlyPath}`;
}
} else {
friendlyPath = linkedUri ? stringifyUri(linkedUri) : undefined;
const maxQueryLength = 40;
let shortenedUri = linkedUri;
if (linkedUri && linkedUri?.query.length > maxQueryLength) {
shortenedUri = shortenedUri?.with({
query: `${linkedUri.query.slice(0, maxQueryLength)}...`
});
}
friendlyPath = shortenedUri ? shortenedUri.toString(true) : undefined;
}
} catch (error) {
console.warn(parseError(error).message);
Expand Down
4 changes: 2 additions & 2 deletions src/documents/templates/getNormalizedDocumentKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
// ---------------------------------------------------------------------------------------------

import * as vscode from "vscode";
import { removeLinkedTemplateScheme } from "../../util/linkedTemplateScheme";
import { decodeLinkedTemplateScheme } from "../../util/linkedTemplateScheme";
import { normalizeUri } from "../../util/normalizedPaths";

export function getNormalizedDocumentKey(documentUri: vscode.Uri): string {
// We want a normalized file path to use as key, but also need to differentiate documents with different URI schemes
const uri = removeLinkedTemplateScheme(documentUri);
const uri = decodeLinkedTemplateScheme(documentUri);
return normalizeUri(uri);
}
8 changes: 6 additions & 2 deletions src/documents/templates/linkedTemplates/linkedTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { assert } from '../../../fixed_assert';
import { IProvideOpenedDocuments } from '../../../IProvideOpenedDocuments';
import { ContainsBehavior } from "../../../language/Span";
import { httpGet } from '../../../util/httpGet';
import { prependLinkedTemplateScheme, removeLinkedTemplateScheme } from '../../../util/linkedTemplateScheme';
import { decodeLinkedTemplateScheme, prependLinkedTemplateScheme } from '../../../util/linkedTemplateScheme';
import { normalizeUri } from '../../../util/normalizedPaths';
import { ofType } from '../../../util/ofType';
import { pathExists } from '../../../util/pathExists';
Expand Down Expand Up @@ -89,6 +89,7 @@ export async function onRequestOpenLinkedFile(
openErrorType: string;
fileScheme: string;
hasQuery: string;
hasSas: string;
}>context.telemetry.properties;
properties.openErrorType = '';
properties.openResult = 'Error';
Expand All @@ -102,6 +103,7 @@ export async function onRequestOpenLinkedFile(

properties.fileScheme = requestedLinkUri.scheme;
properties.hasQuery = String(!!requestedLinkUri.query);
properties.uriHasSas = String(!!requestedLinkUri.query.match('[&?]sig='));

if (requestedLinkUri.scheme === documentSchemes.untitled) {
properties.openErrorType = 'template not saved';
Expand Down Expand Up @@ -140,7 +142,7 @@ export async function onRequestOpenLinkedFile(
}

export async function tryLoadNonLocalLinkedFile(uri: Uri, context: IActionContext, open: boolean): Promise<string> {
uri = removeLinkedTemplateScheme(uri);
uri = decodeLinkedTemplateScheme(uri);
let content: string;
try {
content = await httpGet(stringifyUri(uri));
Expand Down Expand Up @@ -250,6 +252,8 @@ export function assignTemplateGraphToDeploymentTemplate(
export async function openLinkedTemplateFileCommand(linkedTemplateUri: Uri, actionContext: IActionContext): Promise<void> {
let targetUri: Uri;
actionContext.telemetry.properties.scheme = linkedTemplateUri.scheme;
actionContext.telemetry.properties.uriHasQuery = String(!!linkedTemplateUri.query);
actionContext.telemetry.properties.uriHasSas = String(!!linkedTemplateUri.query.match('[&?]sig='));

if (linkedTemplateUri.scheme === documentSchemes.file) {
const exists = await pathExists(linkedTemplateUri);
Expand Down
7 changes: 3 additions & 4 deletions src/util/linkedTemplateScheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ export function prependLinkedTemplateScheme(uri: Uri): Uri {
}
}

export function removeLinkedTemplateScheme(uri: Uri): Uri {
export function decodeLinkedTemplateScheme(uri: Uri): Uri {
if (uri.scheme === documentSchemes.linkedTemplate) {
return parseUri(
stringifyUri(uri).
replace(/^linked-template:/, ''));
const extractedUri = uri.toString(true).replace(/^linked-template:/, '');
return parseUri(extractedUri);
}

return uri;
Expand Down
4 changes: 2 additions & 2 deletions src/util/uri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ export function parseUri(uri: string): Uri {
}

export function stringifyUri(uri: Uri): string {
// true: skips the aggressive encoding
return uri.toString(true);
// can't use true argument, seems to create invalid SAS tokens (or at least Azure storage doesn't like them)
return uri.toString(false);
}
7 changes: 6 additions & 1 deletion test/global.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,12 @@ setup(function (this: Mocha.IBeforeAndAfterContext): void {
teardown(function (this: Mocha.IBeforeAndAfterContext): void {
// console.warn("Teardown");
if (!this.currentTest.state || this.currentTest.state === 'failed') {
console.warn(`\n========= TESTLOG =========:\n${testLog.toString()}\n`);
if (testLog.toString()) {
console.warn("Failed");
console.warn(`Failed. TEST LOG:\n${testLog.toString()}\n`);
} else {
console.warn("Failed (test log is empty)");
}
deleteTestLog();
}
});
55 changes: 44 additions & 11 deletions test/linkedTemplateScheme.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
// ----------------------------------------------------------------------------

import * as assert from "assert";
import { parseUri, prependLinkedTemplateScheme, removeLinkedTemplateScheme, stringifyUri } from "../extension.bundle";
import { Uri } from "vscode";
import { decodeLinkedTemplateScheme, parseUri, prependLinkedTemplateScheme as encodeLinkedTemplateScheme, stringifyUri } from "../extension.bundle";

// tslint:disable:max-func-body-length no-http-string max-line-length no-null-keyword

Expand All @@ -12,20 +13,20 @@ suite("linkedTemplateScheme", () => {
test("not there -> noop", () => {
const uriString = 'https://fake.blob.core.windows.net/blobby/subfolder/child.json?sv=2019-12-12&si=sawdeploy&sr=b&sig=wsrxXCkFbk2IVpk489XIR0KDFyzJaeQHwFvTm5mdQcU%FF';
const uri = parseUri(uriString);
assert.equal(stringifyUri(uri), uriString);
assert.equal(uri.toString(true), uriString);

let uri2 = removeLinkedTemplateScheme(uri);
assert.equal(stringifyUri(uri2), uriString);
let uri2 = decodeLinkedTemplateScheme(uri);
assert.equal(uri2.toString(true), uriString);
});

test("is there -> remove", () => {
const uriString = 'linked-template:https://fake.blob.core.windows.net/blobby/subfolder/child.json?sv=2019-12-12&si=sawdeploy&sr=b&sig=wsrxXCkFbk2IVpk489XIR0KDFyzJaeQHwFvTm5mdQcU%FF';
const uriStringWithScheme = `linked-template:${uriString}`;
const uriWithScheme = parseUri(uriStringWithScheme);
assert.equal(stringifyUri(uriWithScheme), uriStringWithScheme);
assert.equal(uriWithScheme.toString(true), uriStringWithScheme);

let uri2 = removeLinkedTemplateScheme(uriWithScheme);
assert.equal(stringifyUri(uri2), uriString);
let uri2 = decodeLinkedTemplateScheme(uriWithScheme);
assert.equal(uri2.toString(true), uriString);
});
});

Expand All @@ -35,17 +36,49 @@ suite("linkedTemplateScheme", () => {
const uriStringWithScheme = `linked-template:${uriString}`;
const uri = parseUri(uriString);

let uri2 = prependLinkedTemplateScheme(uri);
assert.equal(stringifyUri(uri2), uriStringWithScheme);
let uri2 = encodeLinkedTemplateScheme(uri);
assert.equal(uri2.toString(true), uriStringWithScheme);
});

test("is there -> don't add", () => {
const uriString = 'https://fake.blob.core.windows.net/blobby/subfolder/child.json?sv=2019-12-12&si=sawdeploy&sr=b&sig=wsrxXCkFbk2IVpk489XIR0KDFyzJaeQHwFvTm5mdQcU%FF';
const uriStringWithScheme = `linked-template:${uriString}`;
const uriWithScheme = parseUri(uriStringWithScheme);

let uri2 = prependLinkedTemplateScheme(uriWithScheme);
assert.equal(stringifyUri(uri2), uriStringWithScheme);
let uri2 = encodeLinkedTemplateScheme(uriWithScheme);
assert.equal(uri2.toString(true), uriStringWithScheme);
});
});

suite("lossless round-tripping #1281", () => {
test("round-trip 1", () => {
const original = `https://sawdeploy.blob.core.windows.net/test/child.json?sv%3D2019-12-12%26st%3D2021-04-10T00%3A00%3A27Z%26se%3D2021-04-11T00%3A01%3A00Z%26sr%3Db%26sp%3Dr%26sig%3D29komgppogGhkO1Xy7mjE1oAWpZ%2FSZzmT%2BXttFPbLxs%3D`;
const encoded = encodeLinkedTemplateScheme(Uri.parse(original));
const decoded = decodeLinkedTemplateScheme(encoded);
assert.equal(decoded, original);
});

test("round-trip 2", () => {
const original = `https://sawdeploy.blob.core.windows.net/test/child.json?sv%3D2019-12-12%26st%3D2021-04-10T00%3A00%3A27Z%26se%3D2021-04-11T00%3A01%3A00Z%26sr%3Db%26sp%3Dr%26sig%3D29komgppogGhkO1Xy7mjE1oAWpZ%2FSZzmT%2BXttFPbLxs%3D`;
const encoded = encodeLinkedTemplateScheme(Uri.parse(original));
const encodedAsString = stringifyUri(encoded);
const decoded = decodeLinkedTemplateScheme(parseUri(encodedAsString));
assert.equal(decoded, original);
});

test("round-trip 3", () => {
const original = `https://sawdeploy.blob.core.windows.net/test/child.json?sv%3D2019-12-12%26st%3D2021-04-10T00%3A00%3A27Z%26se%3D2021-04-11T00%3A00%3A00Z%26sr%3Db%26sp%3Dr%26sig%3DVzUR3rEwRDBK75F7riLkt%2FqQNhl5LbTQ0tjoLAr5M7w%3D`;
const encoded = encodeLinkedTemplateScheme(parseUri(original));
const decoded = decodeLinkedTemplateScheme(encoded);
assert.equal(decoded, original);
});

test("round-trip 4", () => {
const original = `https://sawdeploy.blob.core.windows.net/test/child.json?sv%3D2019-12-12%26st%3D2021-04-10T00%3A00%3A27Z%26se%3D2021-04-11T00%3A01%3A00Z%26sr%3Db%26sp%3Dr%26sig%3D29komgppogGhkO1Xy7mjE1oAWpZ%2FSZzmT%2BXttFPbLxs%3D`;
const parsed = parseUri(original);
const stringified = stringifyUri(parsed);
const parsed2 = parseUri(stringified);
assert.equal(parsed2.query, parsed.query);
});
});
});