-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Security hardening: Use mktemp for temporary files in diff tool to pr… #1957
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
base: main
Are you sure you want to change the base?
Security hardening: Use mktemp for temporary files in diff tool to pr… #1957
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Summary of ChangesHello @krishnagamerz, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the security and stability of the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request hardens the diff.bash script against symlink attacks by using mktemp to create temporary directories and files, instead of using predictable paths in /tmp. This is a great security improvement. The changes also consistently quote variable expansions, which improves script robustness.
I've found a few issues:
- A regression where
STDERRandCHANGED_IMAGES_FILEare initialized after theon_exittrap is set, which can cause the script to fail. - The temporary files for
STDERRandCHANGED_IMAGES_FILEare not cleaned up on exit. - There is some code duplication when setting up the temporary registry that could be refactored.
I've left specific comments with suggestions for each of these points.
| fi | ||
| pkill -P $$ | ||
| } | ||
| trap "on_exit" EXIT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The trap is set before STDERR and CHANGED_IMAGES_FILE are initialized with mktemp (lines 36-37). If the script exits for any reason between this line and line 37, the on_exit function will fail because it tries to access STDERR, which would be an unbound variable. This is especially problematic with set -o nounset.
To fix this, you should initialize STDERR and CHANGED_IMAGES_FILE before setting the trap.
| if [[ -n "${REGISTRY_TMPDIR:-}" && -d "${REGISTRY_TMPDIR}" ]]; then | ||
| rm -rf "${REGISTRY_TMPDIR}" | ||
| fi | ||
| pkill -P $$ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private/tools/diff.bash
Outdated
| if [[ "${REGISTRY}" == "spawn_https" ]]; then | ||
| # Make a self signed cert | ||
| rm -f /tmp/localhost.pem /tmp/localhost-key.pem | ||
| rm -rf $DISK_STORAGE | ||
| umask 077 | ||
| REGISTRY_TMPDIR="$(mktemp -d)" | ||
| DISK_STORAGE="${REGISTRY_TMPDIR}/diff-storage" | ||
| CFG_JSON="${REGISTRY_TMPDIR}/cfg.json" | ||
| CERT_PATH="${REGISTRY_TMPDIR}/localhost.pem" | ||
| KEY_PATH="${REGISTRY_TMPDIR}/localhost-key.pem" | ||
| rm -rf "${DISK_STORAGE}" | ||
| mkdir -p "${DISK_STORAGE}" | ||
| mkcert -install | ||
| (cd /tmp && mkcert localhost) | ||
| echo '{ | ||
| "http":{ | ||
| "address":"127.0.0.1", "port":"4564", | ||
| "tls": { | ||
| "cert":"/tmp/localhost.pem", | ||
| "key":"/tmp/localhost-key.pem" | ||
| } | ||
| }, | ||
| "log": { "level": "info" }, | ||
| "storage":{"rootDirectory":"/tmp/diff-storage"} | ||
| }' >/tmp/cfg.json | ||
| mkcert -cert-file "${CERT_PATH}" -key-file "${KEY_PATH}" localhost | ||
| cat >"${CFG_JSON}" <<EOF | ||
| { | ||
| "http": { | ||
| "address": "127.0.0.1", | ||
| "port": "4564", | ||
| "tls": { | ||
| "cert": "${CERT_PATH}", | ||
| "key": "${KEY_PATH}" | ||
| } | ||
| }, | ||
| "log": { "level": "info" }, | ||
| "storage": { "rootDirectory": "${DISK_STORAGE}" } | ||
| } | ||
| EOF | ||
| REGISTRY="localhost:4564" | ||
| zot serve /tmp/cfg.json 1>&2 & | ||
| zot serve "${CFG_JSON}" 1>&2 & | ||
| sleep 1 | ||
| fi | ||
|
|
||
| if [[ "${REGISTRY}" == "spawn" ]]; then | ||
| rm -rf $DISK_STORAGE | ||
| mkdir $DISK_STORAGE | ||
| umask 077 | ||
| REGISTRY_TMPDIR="$(mktemp -d)" | ||
| DISK_STORAGE="${REGISTRY_TMPDIR}/diff-storage" | ||
| mkdir -p "${DISK_STORAGE}" | ||
| REGISTRY="localhost:4564" | ||
| crane registry serve --address "$REGISTRY" --disk "$DISK_STORAGE" & | ||
| fi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's some code duplication between the spawn_https and spawn blocks. You can refactor this to reduce duplication and improve maintainability. The common logic for creating a temporary directory and storage path can be extracted.
if [[ "${REGISTRY}" == "spawn_https" || "${REGISTRY}" == "spawn" ]]; then
umask 077
REGISTRY_TMPDIR="$(mktemp -d)"
DISK_STORAGE="${REGISTRY_TMPDIR}/diff-storage"
mkdir -p "${DISK_STORAGE}"
REGISTRY="localhost:4564"
fi
if [[ "${REGISTRY}" == "spawn_https" ]]; then
# Make a self signed cert
CFG_JSON="${REGISTRY_TMPDIR}/cfg.json"
CERT_PATH="${REGISTRY_TMPDIR}/localhost.pem"
KEY_PATH="${REGISTRY_TMPDIR}/localhost-key.pem"
mkcert -install
mkcert -cert-file "${CERT_PATH}" -key-file "${KEY_PATH}" localhost
cat >"${CFG_JSON}" <<EOF
{
"http": {
"address": "127.0.0.1",
"port": "4564",
"tls": {
"cert": "${CERT_PATH}",
"key": "${KEY_PATH}"
}
},
"log": { "level": "info" },
"storage": { "rootDirectory": "${DISK_STORAGE}" }
}
EOF
zot serve "${CFG_JSON}" 1>&2 &
sleep 1
fi
if [[ "${REGISTRY}" == "spawn" ]]; then
crane registry serve --address "$REGISTRY" --disk "$DISK_STORAGE" &
fi
0fe27ed to
cbff088
Compare
…event symlink attacks