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

Feature/fix security vulnerabilities #52

Merged
merged 2 commits into from
Aug 4, 2023
Merged
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
2 changes: 2 additions & 0 deletions apps/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"react-map-gl": "7.1.0-beta.3",
"superjson": "1.9.1",
"twilio": "^4.11.0",
"validator": "^13.9.0",
"zod": "^3.20.6"
},
"devDependencies": {
Expand All @@ -56,6 +57,7 @@
"@types/react": "^18.0.28",
"@types/react-dom": "^18.0.11",
"@types/react-lottie": "^1.2.6",
"@types/validator": "^13.7.17",
"@typescript-eslint/eslint-plugin": "^5.53.0",
"@typescript-eslint/parser": "^5.53.0",
"eslint": "^8.34.0",
Expand Down
23 changes: 16 additions & 7 deletions apps/server/src/pages/api/trpc/[trpc].ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@ import { createNextApiHandler } from "@trpc/server/adapters/next";
import { env } from "../../../env.mjs";
import { createTRPCContext } from "../../../server/api/trpc";
import { appRouter } from "../../../server/api/root";
import { ZodError } from "zod";

// export API handler
export default createNextApiHandler({
router: appRouter,
createContext: createTRPCContext,
onError:
env.NODE_ENV === "development"
? ({ path, error }) => {
console.error(
`❌ tRPC failed on ${path ?? "<no-path>"}: ${error.message}`,
);
}
: undefined,
(opts) => {
const { error, path } = opts;
console.error("Error:", error);

if (error.cause instanceof ZodError) {
// Returning only the first Zod error message to the client
error.message = JSON.parse(error.message)[0].message;
}

if(env.NODE_ENV === "development"){
console.error(
`❌ tRPC failed on ${path ?? "<no-path>"}: ${error.message}`,
);
}
},
});
9 changes: 7 additions & 2 deletions apps/server/src/server/api/routers/alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,14 @@ export const alertRouter = createTRPCRouter({
data: returnAlertsForUser,
};
} catch (error) {
if (error instanceof TRPCError) {
// if the error is already a TRPCError, just re-throw it
throw error;
}
// if it's a different type of error, throw a new TRPCError
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: `${error}`,
message: `Something Went Wrong`,
});
}
}),
Expand Down Expand Up @@ -141,7 +146,7 @@ export const alertRouter = createTRPCRouter({
// if it's a different type of error, throw a new TRPCError
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: `Unexpected error: ${error}`,
message: `Something went wrong!`,
});
}
}),
Expand Down
42 changes: 36 additions & 6 deletions apps/server/src/server/api/routers/alertMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,14 @@ export const alertMethodRouter = createTRPCRouter({
}
} catch (error) {
logger(`Error in sendVerification: ${error}`, "error");
if (error instanceof TRPCError) {
// if the error is already a TRPCError, just re-throw it
throw error;
}
// if it's a different type of error, throw a new TRPCError
throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: `${error}`,
message: `Something Went Wrong`,
});
}
}),
Expand Down Expand Up @@ -212,9 +217,14 @@ export const alertMethodRouter = createTRPCRouter({
}
} catch (error) {
logger(`Error in createAlertMethod: ${error}`, "error");
if (error instanceof TRPCError) {
// if the error is already a TRPCError, just re-throw it
throw error;
}
// if it's a different type of error, throw a new TRPCError
throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: `${error}`,
message: `Something Went Wrong.`,
});
}
}),
Expand Down Expand Up @@ -246,9 +256,14 @@ export const alertMethodRouter = createTRPCRouter({
};
} catch (error) {
logger(`Error in getAlertMethods: ${error}`, "error");
if (error instanceof TRPCError) {
// if the error is already a TRPCError, just re-throw it
throw error;
}
// if it's a different type of error, throw a new TRPCError
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: `${error}`,
message: `Something Went Wrong`,
});
}
}),
Expand All @@ -274,9 +289,14 @@ export const alertMethodRouter = createTRPCRouter({
};
} catch (error) {
logger(`Error in getAlertMethod: ${error}`, "error");
if (error instanceof TRPCError) {
// if the error is already a TRPCError, just re-throw it
throw error;
}
// if it's a different type of error, throw a new TRPCError
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: `${error}`,
message: `Something Went Wrong`,
});
}
}),
Expand Down Expand Up @@ -312,9 +332,14 @@ export const alertMethodRouter = createTRPCRouter({
};
} catch (error) {
logger(`Error in updateAlertMethod: ${error}`, "error");
if (error instanceof TRPCError) {
// if the error is already a TRPCError, just re-throw it
throw error;
}
// if it's a different type of error, throw a new TRPCError
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: `${error}`,
message: `Something Went Wrong`,
});
}
}),
Expand Down Expand Up @@ -345,9 +370,14 @@ export const alertMethodRouter = createTRPCRouter({
};
} catch (error) {
logger(`Error in deleteAlertMethod: ${error}`, "error");
if (error instanceof TRPCError) {
// if the error is already a TRPCError, just re-throw it
throw error;
}
// if it's a different type of error, throw a new TRPCError
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: `${error}`,
message: `Something Went Wrong`,
});
}
}),
Expand Down
35 changes: 30 additions & 5 deletions apps/server/src/server/api/routers/geoEventProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,14 @@ export const geoEventProviderRouter = createTRPCRouter({
};
} catch (error) {
console.log(error);
if (error instanceof TRPCError) {
// if the error is already a TRPCError, just re-throw it
throw error;
}
// if it's a different type of error, throw a new TRPCError
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: `${error}`,
message: `Something Went Wrong`,
});
}
}),
Expand Down Expand Up @@ -71,9 +76,14 @@ export const geoEventProviderRouter = createTRPCRouter({
};
} catch (error) {
console.log(error);
if (error instanceof TRPCError) {
// if the error is already a TRPCError, just re-throw it
throw error;
}
// if it's a different type of error, throw a new TRPCError
throw new TRPCError({
code: "CONFLICT",
message: `${error}`,
message: `Something Went Wrong.`,
});
}
}),
Expand All @@ -90,9 +100,14 @@ export const geoEventProviderRouter = createTRPCRouter({
};
} catch (error) {
console.log(error);
if (error instanceof TRPCError) {
// if the error is already a TRPCError, just re-throw it
throw error;
}
// if it's a different type of error, throw a new TRPCError
throw new TRPCError({
code: "CONFLICT",
message: `${error}`,
message: `Something Went Wrong`,
});
}
}),
Expand Down Expand Up @@ -120,9 +135,14 @@ export const geoEventProviderRouter = createTRPCRouter({
};
} catch (error) {
console.log(error);
if (error instanceof TRPCError) {
// if the error is already a TRPCError, just re-throw it
throw error;
}
// if it's a different type of error, throw a new TRPCError
throw new TRPCError({
code: "CONFLICT",
message: `${error}`,
message: `Something Went Wrong`,
});
}
}),
Expand All @@ -149,9 +169,14 @@ export const geoEventProviderRouter = createTRPCRouter({
};
} catch (error) {
console.log(error);
if (error instanceof TRPCError) {
// if the error is already a TRPCError, just re-throw it
throw error;
}
// if it's a different type of error, throw a new TRPCError
throw new TRPCError({
code: "CONFLICT",
message: `${error}`,
message: `Something Went Wrong`,
});
}
}),
Expand Down
9 changes: 7 additions & 2 deletions apps/server/src/server/api/routers/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ export const projectRouter = createTRPCRouter({
}
} catch (error) {
console.log(error)
if (error instanceof TRPCError) {
// if the error is already a TRPCError, just re-throw it
throw error;
}
// if it's a different type of error, throw a new TRPCError
throw new TRPCError({
code: "NOT_FOUND",
message: `${error}`,
code: "INTERNAL_SERVER_ERROR",
message: `Something Went Wrong`,
});
}
}),
Expand Down
39 changes: 32 additions & 7 deletions apps/server/src/server/api/routers/site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,14 @@ export const siteRouter = createTRPCRouter({
};
} catch (error) {
console.log(error);
if (error instanceof TRPCError) {
// if the error is already a TRPCError, just re-throw it
throw error;
}
// if it's a different type of error, throw a new TRPCError
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: `${error}`,
message: `Something Went Wrong`,
});
}
}),
Expand Down Expand Up @@ -236,9 +241,14 @@ export const siteRouter = createTRPCRouter({
}
} catch (error) {
console.log(error)
if (error instanceof TRPCError) {
// if the error is already a TRPCError, just re-throw it
throw error;
}
// if it's a different type of error, throw a new TRPCError
throw new TRPCError({
code: "NOT_FOUND",
message: `${error}`,
code: "INTERNAL_SERVER_ERROR",
message: `Something Went Wrong`,
});
}
}),
Expand Down Expand Up @@ -301,9 +311,14 @@ export const siteRouter = createTRPCRouter({

} catch (error) {
console.log(error);
if (error instanceof TRPCError) {
// if the error is already a TRPCError, just re-throw it
throw error;
}
// if it's a different type of error, throw a new TRPCError
throw new TRPCError({
code: "CONFLICT",
message: `${error}`,
message: `Error Updating Site.`,
});
}
}),
Expand All @@ -327,9 +342,14 @@ export const siteRouter = createTRPCRouter({
};
} catch (error) {
console.log(error);
if (error instanceof TRPCError) {
// if the error is already a TRPCError, just re-throw it
throw error;
}
// if it's a different type of error, throw a new TRPCError
throw new TRPCError({
code: `${error.code}`,
message: `${error}`,
code: `INTERNAL_SERVER_ERROR`,
message: `Something Went Wrong`,
});
}
}),
Expand Down Expand Up @@ -376,9 +396,14 @@ export const siteRouter = createTRPCRouter({

} catch (error) {
console.log(error);
if (error instanceof TRPCError) {
// if the error is already a TRPCError, just re-throw it
throw error;
}
// if it's a different type of error, throw a new TRPCError
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: `${error}`,
message: `Something Went Wrong`,
});
}
}),
Expand Down
Loading
Loading