Skip to content

Commit

Permalink
Merge branch 'main' into rohin/request-response-parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
RohinBhargava authored Dec 2, 2024
2 parents b12f838 + 1fce154 commit 9f75038
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 3 deletions.
4 changes: 4 additions & 0 deletions fern/apis/fdr/definition/commons.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ errors:
status-code: 403
type: string

InvalidURLError:
status-code: 400
type: string

InternalError:
status-code: 500
type: string
Expand Down
1 change: 1 addition & 0 deletions fern/apis/fdr/definition/docs/v2/write/__package__.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ service:
- InvalidDomainError
- InvalidCustomDomainError
- rootCommons.DomainBelongsToAnotherOrgError
- rootCommons.InvalidURLError

startDocsPreviewRegister:
auth: true
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions servers/fdr/src/__test__/local/services/docs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,47 @@ it("docs register", async () => {
});
});

it("test invalid domain URL - special characters", async () => {
const fdr = getClient({ authed: true, url: inject("url") });
const domain = `https://fern-${Math.random()}.docs.buildwithfern.com`;
// register docs
const startDocsRegisterResponse = getAPIResponse(
await fdr.docs.v2.write.startDocsRegister({
orgId: FdrAPI.OrgId(`plantstore-2024-test${Math.random()}`),
apiId: FdrAPI.ApiId(""),
domain,
customDomains: [],
filepaths: [
DocsV1Write.FilePath("logo.png"),
DocsV1Write.FilePath("guides/guide.mdx"),
DocsV1Write.FilePath("fonts/Syne.woff2"),
],
}),
);
await fdr.docs.v2.write.finishDocsRegister(startDocsRegisterResponse.docsRegistrationId, {
docsDefinition: WRITE_DOCS_REGISTER_DEFINITION,
});

const startDocsRegisterResponse2 = await fdr.docs.v2.write.startDocsRegister({
orgId: FdrAPI.OrgId(`plantstore-2024-test${Math.random()}`),
apiId: FdrAPI.ApiId(""),
domain: domain + "//",
customDomains: [],
filepaths: [
DocsV1Write.FilePath("logo.png"),
DocsV1Write.FilePath("guides/guide.mdx"),
DocsV1Write.FilePath("fonts/Syne.woff2"),
],
});

// expecting an error, because adding // to the domain should not bypass domain check
expect((startDocsRegisterResponse2 as any).error.content).toEqual({
body: `Domain URL is malformed: ${domain + "//"}`,
reason: "status-code",
statusCode: 400,
});
});

it("docs register V2", async () => {
const fdr = getClient({ authed: true, url: inject("url") });
// register docs
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion servers/fdr/src/controllers/docs/v2/getDocsWriteV2Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { v4 as uuidv4 } from "uuid";
import { DocsV2WriteService } from "../../../api";
import { FernRegistry } from "../../../api/generated";
import { OrgId } from "../../../api/generated/api";
import { DomainBelongsToAnotherOrgError } from "../../../api/generated/api/resources/commons/errors";
import { DomainBelongsToAnotherOrgError, InvalidUrlError } from "../../../api/generated/api/resources/commons/errors";
import { DocsRegistrationIdNotFound } from "../../../api/generated/api/resources/docs/resources/v1/resources/write/errors";
import { LoadDocsForUrlResponse } from "../../../api/generated/api/resources/docs/resources/v2/resources/read";
import {
Expand All @@ -41,8 +41,22 @@ export interface DocsRegistrationInfo {
authType: AuthType;
}

function pathnameIsMalformed(pathname: string): boolean {
if (pathname === "" || pathname === "/") {
return false;
}
if (!/^.*([a-z0-9]).*$/.test(pathname)) {
// does the pathname only contain special characters?
return true;
}
return false;
}

function validateAndParseFernDomainUrl({ app, url }: { app: FdrApplication; url: string }): ParsedBaseUrl {
const baseUrl = ParsedBaseUrl.parse(url);
if (baseUrl.path != null && pathnameIsMalformed(baseUrl.path)) {
throw new InvalidUrlError(`Domain URL is malformed: https://${baseUrl.hostname + baseUrl.path}`);
}
if (!baseUrl.hostname.endsWith(app.config.domainSuffix)) {
throw new InvalidDomainError();
}
Expand Down
2 changes: 0 additions & 2 deletions servers/fdr/src/util/ParsedBaseUrl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { LOGGER } from "../app/FdrApplication";

const HAS_HTTPS_REGEX = /^https?:\/\//i;

export class ParsedBaseUrl {
Expand Down

0 comments on commit 9f75038

Please sign in to comment.