Skip to content

Commit

Permalink
(fix): await ownership check and only throw only in the api route (#719)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi authored Apr 28, 2024
1 parent 8a124e7 commit 59893a5
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
run: pnpm lint:monorepo

- name: Lint style
run: pnpm turbo lint:style format:check
run: pnpm turbo lint:style

- name: eslint
run: pnpm lint:eslint
Expand Down
4 changes: 2 additions & 2 deletions packages/template-resolver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"scripts": {
"compile": "tsc --build",
"clean": "rm -rf ./dist && tsc --build --clean",
"format": "prettier --write --ignore-unknown --ignore-path ../../../shared/.prettierignore \"**\"",
"format:check": "prettier --check --ignore-unknown --ignore-path ../../../shared/.prettierignore \"**\"",
"format": "prettier --write --ignore-unknown --ignore-path ../../shared/.prettierignore \"**\"",
"format:check": "prettier --check --ignore-unknown --ignore-path ../../shared/.prettierignore \"**\"",
"lint:eslint": "eslint --max-warnings 0 . --ignore-path=../../.eslintignore",
"lint:eslint:fix": "pnpm lint:eslint --fix",
"lint:style": "stylelint 'src/**/*.scss' --allow-empty-input --max-warnings 0",
Expand Down
5 changes: 4 additions & 1 deletion servers/fdr/src/controllers/docs/v2/getDocsWriteV2Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,13 @@ export function getDocsWriteV2Service(app: FdrApplication): DocsV2WriteService {
const customUrls = validateAndParseCustomDomainUrl({ customUrls: req.body.customDomains });

// ensure that the domains are not already registered by another org
app.dao.docsV2().checkDomainsDontBelongToAnotherOrg(
const hasOwnership = await app.dao.docsV2().checkDomainsDontBelongToAnotherOrg(
[fernUrl, ...customUrls].map((url) => url.getFullUrl()),
req.body.orgId,
);
if (!hasOwnership) {
throw new FdrAPI.DomainBelongsToAnotherOrgError();
}

const docsRegistrationId = uuidv4();
const s3FileInfos = await app.services.s3.getPresignedUploadUrls({
Expand Down
10 changes: 3 additions & 7 deletions servers/fdr/src/db/docs/DocsV2Dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface LoadDocsConfigResponse {
}

export interface DocsV2Dao {
checkDomainsDontBelongToAnotherOrg(domains: string[], orgId: string): Promise<void>;
checkDomainsDontBelongToAnotherOrg(domains: string[], orgId: string): Promise<boolean>;

loadDocsForURL(url: URL): Promise<LoadDocsDefinitionByUrlResponse | undefined>;

Expand All @@ -48,7 +48,7 @@ export interface DocsV2Dao {

export class DocsV2DaoImpl implements DocsV2Dao {
constructor(private readonly prisma: PrismaClient) {}
public async checkDomainsDontBelongToAnotherOrg(domains: string[], orgId: string): Promise<void> {
public async checkDomainsDontBelongToAnotherOrg(domains: string[], orgId: string): Promise<boolean> {
const matchedDomains = await this.prisma.docsV2.findMany({
select: {
orgID: true,
Expand All @@ -61,11 +61,7 @@ export class DocsV2DaoImpl implements DocsV2Dao {
distinct: ["orgID", "domain"],
});

matchedDomains.forEach((matchedDomain) => {
if (matchedDomain.orgID !== orgId) {
throw new FdrAPI.DomainBelongsToAnotherOrgError();
}
});
return matchedDomains.every((matchedDomain) => matchedDomain.orgID === orgId);
}

public async loadDocsForURL(url: URL): Promise<WithoutQuestionMarks<LoadDocsDefinitionByUrlResponse> | undefined> {
Expand Down

0 comments on commit 59893a5

Please sign in to comment.