Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import AdmZip from "adm-zip";
import fs from "fs-extra";
import { cloneDeep } from "lodash";
import * as os from "os";
import * as path from "path";
import "reflect-metadata";
import stripBom from "strip-bom";
Expand Down Expand Up @@ -463,6 +464,11 @@ export class ManifestUtils {
maxLength = 25
): Promise<Result<undefined, FxError>> {
const manifestPath = this.getTeamsAppManifestPath(projectPath);
const resolvedManifestPath = path.resolve(manifestPath);
const tempDir = path.resolve(os.tmpdir());
if (resolvedManifestPath.startsWith(tempDir + path.sep)) {
return ok(undefined);
}
Comment on lines +467 to +471
if (fs.pathExistsSync(manifestPath)) {
const manifest = (await fs.readJson(manifestPath)) as TeamsAppManifest;
const shortName = manifest.name.short;
Expand Down
16 changes: 14 additions & 2 deletions packages/fx-core/src/component/utils/settingsUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import { err, FxError, ok, Result, Settings } from "@microsoft/teamsfx-api";
import * as fs from "fs-extra";
import * as os from "os";
import * as path from "path";
import * as uuid from "uuid";
import { parseDocument } from "yaml";
import { featureFlagManager, FeatureFlags } from "../../common/featureFlags";
Expand All @@ -17,6 +19,12 @@
import { pathUtils } from "./pathUtils";

class SettingsUtils {
private isInTempDirectory(filePath: string): boolean {
const resolvedPath = path.resolve(filePath);
const tempDir = path.resolve(os.tmpdir());
return resolvedPath.startsWith(tempDir + path.sep);
}
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed

async readSettings(
projectPath: string,
ensureTrackingId = true
Expand All @@ -37,7 +45,9 @@
const projectId = uuid.v4();
const projectIdField = appYaml.createPair("projectId", uuid.v4());
appYaml.add(projectIdField);
await fs.writeFile(projectYamlPath, appYaml.toString()); // only write yaml file once instead of write yaml file after every command
if (!this.isInTempDirectory(projectYamlPath)) {
await fs.writeFile(projectYamlPath, appYaml.toString());

Check failure

Code scanning / CodeQL

Insecure temporary file High

Insecure creation of file in
the os temp dir
.
}
sendTelemetryEvent(Component.core, TelemetryEvent.FillProjectId, {
[TelemetryProperty.ProjectId]: projectId,
});
Expand All @@ -64,7 +74,9 @@
const yamlFileContent: string = await fs.readFile(projectYamlPath, "utf8");
const appYaml = parseDocument(yamlFileContent);
appYaml.set("projectId", settings.trackingId);
await fs.writeFile(projectYamlPath, appYaml.toString());
if (!this.isInTempDirectory(projectYamlPath)) {
await fs.writeFile(projectYamlPath, appYaml.toString());

Check failure

Code scanning / CodeQL

Insecure temporary file High

Insecure creation of file in
the os temp dir
.
}
return ok(projectYamlPath);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { assert } from "chai";
import fs from "fs-extra";
import mockedEnv, { RestoreFn } from "mocked-env";
import * as os from "os";
import path from "path";
import * as sinon from "sinon";
import {
Expand Down Expand Up @@ -521,6 +522,18 @@ describe("trimManifestShortName", () => {
assert.isTrue(readJsonStub.notCalled);
assert.isTrue(writeFileStub.notCalled);
});
it("Skips temp directory paths", async () => {
const teamsManifest = new TeamsAppManifest();
teamsManifest.name.short = "shortname abcdefghijklmnopqrstuvwxyz${{APP_NAME_SUFFIX}}";
const readJsonStub = sandbox.stub(fs, "readJson").resolves(teamsManifest);
const writeFileStub = sandbox.stub(fs, "writeFile").resolves();
sandbox.stub(fs, "pathExistsSync").returns(true);
const tempPath = path.join(os.tmpdir(), "test-project");
const res = await manifestUtils.trimManifestShortName(tempPath);
assert.isTrue(res.isOk());
assert.isTrue(readJsonStub.notCalled);
assert.isTrue(writeFileStub.notCalled);
Comment on lines +527 to +533
});
});

describe("resolveLocFile", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe("SettingsUtils", () => {
tempDir = path.join(os.tmpdir(), `test-settings-${Date.now()}`);
await fs.ensureDir(tempDir);
envRestore = mockedEnv({});
sandbox.stub(os, "tmpdir").returns("/fake-temp-dir-for-test");
});
Comment on lines 23 to 26

afterEach(async () => {
Expand Down
Loading