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(manifest): Strip protocol before feeding just the outgoing domain to the manifest API #246

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
66 changes: 65 additions & 1 deletion src/manifest/manifest_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
ISlackManifestRunOnSlack,
SlackManifestType,
} from "./types.ts";
import { Manifest, SlackManifest } from "./mod.ts";
import { Manifest, SlackManifest, validateOutgoingDomains } from "./mod.ts";
import {
DefineDatastore,
DefineEvent,
Expand All @@ -26,6 +26,7 @@ import {
import { DefineConnector } from "../functions/mod.ts";
import { InternalSlackTypes } from "../schema/slack/types/custom/mod.ts";
import { DuplicateCallbackIdError, DuplicateNameError } from "./errors.ts";
import { assertThrows } from "https://deno.land/[email protected]/testing/asserts.ts";

Deno.test("SlackManifestType correctly resolves to a Hosted App when runOnSlack = true", () => {
const definition: SlackManifestType = {
Expand Down Expand Up @@ -1172,3 +1173,66 @@ Deno.test("Manifest throws error when CustomEvents with duplicate name are added
assertStringIncludes(error.message, "CustomEvent");
}
});

Deno.test("Manifest correctly parses valid domains", () => {
const def = {
outgoingDomains: [
"https://slack.com",
"http://salesforce.com",
"https://api.slack.com",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd add some more test cases here:

  1. Bare IP addresses
  2. Fully qualified domain names, including subdomains, i.e. en.wikipedia.org
  3. Either of the above w/ port specifiers
  4. All of the above with and without protocol specifiers

],
};
const expected = ["slack.com", "salesforce.com", "api.slack.com"];

const definition: SlackManifestType = {
runOnSlack: false,
name: "test",
description: "description",
backgroundColor: "#FFF",
longDescription:
"The book is a roman à clef, rooted in autobiographical incidents. The story follows its protagonist, Raoul Duke, and his attorney, Dr. Gonzo, as they descend on Las Vegas to chase the American Dream...",
displayName: "displayName",
icon: "icon.png",
botScopes: ["channels:history", "chat:write", "commands"],
};
const manifest = Manifest(definition);

manifest.outgoing_domains = validateOutgoingDomains(
def.outgoingDomains || [],
);

assertEquals(manifest.outgoing_domains, expected);
});

Deno.test("Manifest throws error for invalid domains", () => {
const def = {
outgoingDomains: [
"slack",
"htt*ps://api.slack.com",
"https://api slack.com",
],
};

assertThrows(
() => {
const definition: SlackManifestType = {
runOnSlack: false,
name: "test",
description: "description",
backgroundColor: "#FFF",
longDescription:
"The book is a roman à clef, rooted in autobiographical incidents. The story follows its protagonist, Raoul Duke, and his attorney, Dr. Gonzo, as they descend on Las Vegas to chase the American Dream...",
displayName: "displayName",
icon: "icon.png",
botScopes: ["channels:history", "chat:write", "commands"],
};
const manifest = Manifest(definition);

manifest.outgoing_domains = validateOutgoingDomains(
def.outgoingDomains || [],
);
},
Error,
"Invalid outgoing domain",
);
});
28 changes: 27 additions & 1 deletion src/manifest/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,30 @@
return manifest.export();
};

export const validateOutgoingDomains = (
domains: string[],
) => {
return domains.map((domain) => {
try {
const url = domain.includes("://") ? domain : `http://${domain}`;
return new URL(url).hostname;
} catch (e) {
if (isValidDomain(domain)) {
return domain;
} else {

Check warning on line 45 in src/manifest/mod.ts

View check run for this annotation

Codecov / codecov/patch

src/manifest/mod.ts#L44-L45

Added lines #L44 - L45 were not covered by tests
throw new Error(
`Invalid outgoing domain: ${domain}, error ${e}`,
);
}
}
});
};

const isValidDomain = (domain: string) => {
const domainPattern = /^[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z]{2,}$/;
return domainPattern.test(domain);
};

export class SlackManifest {
constructor(private definition: SlackManifestType) {
this.registerFeatures();
Expand Down Expand Up @@ -132,7 +156,9 @@
);
}

manifest.outgoing_domains = def.outgoingDomains || [];
manifest.outgoing_domains = validateOutgoingDomains(
def.outgoingDomains || [],
);

// Assign remote hosted app properties
if (manifest.settings.function_runtime === "slack") {
Expand Down