(core) updates from grist-core

This commit is contained in:
Paul Fitzpatrick
2024-07-01 09:37:47 -04:00
38 changed files with 3218 additions and 66 deletions

40
sandbox/docker_entrypoint.sh Executable file
View File

@@ -0,0 +1,40 @@
#!/usr/bin/env bash
set -Eeuo pipefail
# Runs the command provided as arguments, but attempts to configure permissions first.
important_read_dirs=("/grist" "/persist")
write_dir="/persist"
current_user_id=$(id -u)
# We want to avoid running Grist as root if possible.
# Try to setup permissions and de-elevate to a normal user.
if [[ $current_user_id == 0 ]]; then
target_user=${GRIST_DOCKER_USER:-grist}
target_group=${GRIST_DOCKER_GROUP:-grist}
# Make sure the target user owns everything that Grist needs write access to.
find $write_dir ! -user "$target_user" -exec chown "$target_user" "{}" +
# Restart as the target user, replacing the current process (replacement is needed for security).
# Alternative tools to setpriv are: chroot, gosu.
# Need to use `exec` to close the parent shell, to avoid vulnerabilities: https://github.com/tianon/gosu/issues/37
exec setpriv --reuid "$target_user" --regid "$target_group" --init-groups /usr/bin/env bash "$0" "$@"
fi
# Validate that this user has access to the top level of each important directory.
# There might be a benefit to testing individual files, but this is simpler as the dir may start empty.
for dir in "${important_read_dirs[@]}"; do
if ! { test -r "$dir" ;} ; then
echo "Invalid permissions, cannot read '$dir'. Aborting." >&2
exit 1
fi
done
for dir in "${important_write_dirs[@]}"; do
if ! { test -r "$dir" && test -w "$dir" ;} ; then
echo "Invalid permissions, cannot write '$dir'. Aborting." >&2
exit 1
fi
done
exec /usr/bin/tini -s -- "$@"

35
sandbox/supervisor.mjs Normal file
View File

@@ -0,0 +1,35 @@
import {spawn} from 'child_process';
let grist;
function startGrist(newConfig={}) {
saveNewConfig(newConfig);
// H/T https://stackoverflow.com/a/36995148/11352427
grist = spawn('./sandbox/run.sh', {
stdio: ['inherit', 'inherit', 'inherit', 'ipc']
});
grist.on('message', function(data) {
if (data.action === 'restart') {
console.log('Restarting Grist with new environment');
// Note that we only set this event handler here, after we have
// a new environment to reload with. Small chance of a race here
// in case something else sends a SIGINT before we do it
// ourselves further below.
grist.on('exit', () => {
grist = startGrist(data.newConfig);
});
grist.kill('SIGINT');
}
});
return grist;
}
// Stub function
function saveNewConfig(newConfig) {
// TODO: something here to actually persist the new config before
// restarting Grist.
}
startGrist();