Adjust uploaded files permission
This commit is contained in:
1 parent
28a6211457
commit
ccfda4c054
2 files changed
+40
-9
No files matched your search
@@ -3844,19 +3844,25 @@ export async function getContainerTop(id: string, envId?: number | null): Promis
|
|||||||
export async function execInContainer(
|
export async function execInContainer(
|
||||||
containerId: string,
|
containerId: string,
|
||||||
cmd: string[],
|
cmd: string[],
|
||||||
envId?: number | null
|
envId?: number | null,
|
||||||
|
user?: string | null
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
// Create exec instance
|
const execBody: any = {
|
||||||
|
Cmd: cmd,
|
||||||
|
AttachStdout: true,
|
||||||
|
AttachStderr: true,
|
||||||
|
Tty: false
|
||||||
|
};
|
||||||
|
|
||||||
|
if (user) {
|
||||||
|
execBody.User = user;
|
||||||
|
}
|
||||||
|
|
||||||
const execCreate = await dockerJsonRequest<{ Id: string }>(
|
const execCreate = await dockerJsonRequest<{ Id: string }>(
|
||||||
`/containers/${containerId}/exec`,
|
`/containers/${containerId}/exec`,
|
||||||
{
|
{
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify({
|
body: JSON.stringify(execBody)
|
||||||
Cmd: cmd,
|
|
||||||
AttachStdout: true,
|
|
||||||
AttachStderr: true,
|
|
||||||
Tty: false
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
envId
|
envId
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { json } from '@sveltejs/kit';
|
import { json } from '@sveltejs/kit';
|
||||||
import { putContainerArchive } from '$lib/server/docker';
|
import { putContainerArchive, inspectContainer, execInContainer } from '$lib/server/docker';
|
||||||
import { authorize } from '$lib/server/authorize';
|
import { authorize } from '$lib/server/authorize';
|
||||||
import { validateDockerIdParam } from '$lib/server/docker-validation';
|
import { validateDockerIdParam } from '$lib/server/docker-validation';
|
||||||
import type { RequestHandler } from './$types';
|
import type { RequestHandler } from './$types';
|
||||||
@@ -111,6 +111,15 @@ export const POST: RequestHandler = async ({ params, url, request, cookies }) =>
|
|||||||
return json({ error: 'No files provided' }, { status: 400 });
|
return json({ error: 'No files provided' }, { status: 400 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We'll inspect the container once to determine its default user
|
||||||
|
let defaultUser: string | undefined;
|
||||||
|
try {
|
||||||
|
const inspectData = await inspectContainer(params.id, envIdNum);
|
||||||
|
defaultUser = inspectData.Config.User || undefined;
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('Failed to inspect container for user info', e);
|
||||||
|
}
|
||||||
|
|
||||||
// For simplicity, we'll upload files one at a time
|
// For simplicity, we'll upload files one at a time
|
||||||
// A more sophisticated implementation could pack multiple files into one tar
|
// A more sophisticated implementation could pack multiple files into one tar
|
||||||
const uploaded: string[] = [];
|
const uploaded: string[] = [];
|
||||||
@@ -128,6 +137,22 @@ export const POST: RequestHandler = async ({ params, url, request, cookies }) =>
|
|||||||
envId ? parseInt(envId) : undefined
|
envId ? parseInt(envId) : undefined
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// chown the uploaded file
|
||||||
|
if (defaultUser) {
|
||||||
|
const targetPath = path.endsWith('/') ? `${path}${file.name}` : `${path}/${file.name}`;
|
||||||
|
const ownerGroup = defaultUser.includes(':') ? defaultUser : `${defaultUser}:${defaultUser}`;
|
||||||
|
try {
|
||||||
|
await execInContainer(
|
||||||
|
params.id,
|
||||||
|
['chown', '-R', ownerGroup, targetPath],
|
||||||
|
envId ? parseInt(envId) : undefined,
|
||||||
|
'root'
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('Failed to set ownership on', targetPath, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
uploaded.push(file.name);
|
uploaded.push(file.name);
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
errors.push(`${file.name}: ${err.message}`);
|
errors.push(`${file.name}: ${err.message}`);
|
||||||
|
|||||||
Reference in new issue
Block a user