Skip to content

Commit

Permalink
Merge branch 'master' into lj-tokenMinting
Browse files Browse the repository at this point in the history
  • Loading branch information
yuchenshi committed Apr 27, 2022
2 parents 0dec725 + 28b3467 commit 11670a0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/emulator/functionsEmulatorRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
HttpConstants,
SignatureType,
} from "./functionsEmulatorShared";
import { compareVersionStrings } from "./functionsEmulatorUtils";
import { compareVersionStrings, isLocalHost } from "./functionsEmulatorUtils";

let functionModule: any;
let FUNCTION_TARGET_NAME: string;
Expand Down Expand Up @@ -365,7 +365,7 @@ function initializeNetworkFiltering(frb: FunctionsRuntimeBundle): void {
.filter((v) => v);
const href = (hrefs.length && hrefs[0]) || "";

if (href && !history[href] && !href.startsWith("http://localhost")) {
if (href && !history[href] && !isLocalHost(href)) {
history[href] = true;
if (href.indexOf("googleapis.com") !== -1) {
new EmulatorLog("SYSTEM", "googleapis-network-access", "", {
Expand Down
7 changes: 7 additions & 0 deletions src/emulator/functionsEmulatorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,10 @@ export function compareVersionStrings(a?: string, b?: string) {

return 0;
}

/**
* Check if a url is localhost
*/
export function isLocalHost(href: string): boolean {
return !!href.match(/^(http(s)?:\/\/)?(localhost|127.0.0.1|\[::1])/);
}
45 changes: 45 additions & 0 deletions src/test/emulators/functionsEmulatorUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
trimSlashes,
compareVersionStrings,
parseRuntimeVersion,
isLocalHost,
} from "../../emulator/functionsEmulatorUtils";

describe("FunctionsEmulatorUtils", () => {
Expand Down Expand Up @@ -138,4 +139,48 @@ describe("FunctionsEmulatorUtils", () => {
expect(parseRuntimeVersion("banana")).to.eql(undefined);
});
});

describe("isLocalHost", () => {
const testCases: {
desc: string;
href: string;
expected: boolean;
}[] = [
{
desc: "should return true for localhost",
href: "http://localhost:4000",
expected: true,
},
{
desc: "should return true for 127.0.0.1",
href: "127.0.0.1:5001/firestore",
expected: true,
},
{
desc: "should return true for ipv6 loopback",
href: "[::1]:5001/firestore",
expected: true,
},
{
desc: "should work with https",
href: "https://127.0.0.1:5001/firestore",
expected: true,
},
{
desc: "should return false for external uri",
href: "http://google.com/what-is-localhost",
expected: false,
},
{
desc: "should return false for external ip",
href: "123:100:99:12",
expected: false,
},
];
for (const t of testCases) {
it(t.desc, () => {
expect(isLocalHost(t.href)).to.eq(t.expected);
});
}
});
});

0 comments on commit 11670a0

Please sign in to comment.