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

Hiding secrets when parsing filestash error messages. #3

Merged
merged 3 commits into from
Oct 1, 2024
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
5 changes: 5 additions & 0 deletions .changeset/thirty-oranges-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@deepsirius-ui/app": patch
---

Hiding secrets when parsing filestash errors.
54 changes: 39 additions & 15 deletions apps/deepsirius-ui/src/server/api/routers/ssh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ const unzippedImage = z.object({
src: z.string(),
});

const apiErrorSchema = z
.object({
status: z.string(),
message: z.string(),
})
.transform((data) => {
return {
status: data.status,
message: data.message.replace(env.STORAGE_API_KEY, '***'),
};
});

export const sshRouter = createTRPCRouter({
ls: protectedProcedure
.input(
Expand Down Expand Up @@ -48,12 +60,7 @@ export const sshRouter = createTRPCRouter({
const data: unknown = await res.json();

if (!res.ok) {
const error = z
.object({
status: z.string(),
message: z.string(),
})
.parse(data);
const error = apiErrorSchema.parse(data);

throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
Expand Down Expand Up @@ -110,16 +117,18 @@ export const sshRouter = createTRPCRouter({
Cookie: cookie,
},
});

const data = await res.text();
if (!res.ok) {
const data: unknown = await res.json();
const error = apiErrorSchema.parse(data);

throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: data,
message: error.message,
});
}
const data = await res.text();
return { content: data };
}),
}),
catImage: protectedProcedure
.input(
z
Expand Down Expand Up @@ -147,6 +156,15 @@ export const sshRouter = createTRPCRouter({
Cookie: cookie,
},
});
if (!res.ok) {
const data: unknown = await res.json();
const error = apiErrorSchema.parse(data);

throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: error.message,
});
}
const data = await res.arrayBuffer();
const base64data = Buffer.from(data).toString('base64');
return { src: `data:image/png;base64,${base64data}` };
Expand Down Expand Up @@ -179,11 +197,15 @@ export const sshRouter = createTRPCRouter({
},
});
if (!res.ok) {
throw new Error(`Failed to fetch: ${res.statusText}`);
const data: unknown = await res.json();
const error = apiErrorSchema.parse(data);

throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: error.message,
});
}
const buffer = await res.arrayBuffer().catch((err: Error) => {
throw new Error(`Failed to fetch buffer file: ${err.message}`);
});
const buffer = await res.arrayBuffer();
const zip = await JSZip.loadAsync(buffer).catch((err: Error) => {
throw new Error(`Failed to load ZIP file: ${err.message}`);
});
Expand Down Expand Up @@ -347,7 +369,9 @@ export const sshRouter = createTRPCRouter({
.query(async ({ ctx, input }) => {
const connection = ctx.connection;

const command = `head ${input.lines ? `-n ${input.lines} ` : ''}${input.path}`;
const command = `head ${input.lines ? `-n ${input.lines} ` : ''}${
input.path
}`;
const { stdout, stderr } = await connection.execCommand(command);

if (!!stderr) {
Expand Down