Skip to content
Closed
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
27 changes: 4 additions & 23 deletions .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,13 @@ permissions:

on:
push:
branches: [ main ]
branches: [ main, master ]
pull_request:
branches: [ main ]
branches: [ main, master ]

jobs:
find-examples:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- uses: actions/checkout@v7

- id: set-matrix
run: |
EXAMPLES=$(find examples -mindepth 1 -maxdepth 1 -type d -exec basename {} \; | jq -R -s -c 'split("\n")[:-1]')
echo "matrix=${EXAMPLES}" >> $GITHUB_OUTPUT

test-examples:
needs: find-examples
runs-on: ubuntu-latest
strategy:
matrix:
example: ${{ fromJson(needs.find-examples.outputs.matrix) }}
steps:
- uses: actions/checkout@v7

Expand All @@ -36,8 +20,5 @@ jobs:
go-version: '1.24'
cache: true

- name: Test ${{ matrix.example }}
run: |
cd examples/${{ matrix.example }}
go mod download
go build -v ./...
- name: Test Examples Build
run: go build -v ./examples/...
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
bin/
*.exe
*.exe~
*.dll
Expand All @@ -17,7 +18,8 @@ coverage.*
*.coverprofile
profile.cov

# Dependency directories (remove the comment below to include it)
# Dependency directories
node_modules/
# vendor/

# Go workspace file
Expand Down
8 changes: 4 additions & 4 deletions .pre-commit-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ repos:
- id: go-tests
name: Go Tests
entry: go test
args: [ '-race', '-short', './...' ]
args: [ '-short', './...' ]
language: system
pass_filenames: false
types: [ go ]
Expand All @@ -75,10 +75,10 @@ repos:
language: system
stages: [ commit-msg ]

# Prevent commits to main
# Prevent commits to master or main
- id: no-commit-to-main
name: Don't commit to main
entry: bash -c 'if [ "$(git rev-parse --abbrev-ref HEAD)" = "main" ]; then echo "Cannot commit directly to main branch"; exit 1; fi'
name: Don't commit to master or main
entry: bash -c 'BRANCH=$(git rev-parse --abbrev-ref HEAD); if [ "$BRANCH" = "master" ] || [ "$BRANCH" = "main" ]; then echo "Cannot commit directly to $BRANCH branch"; exit 1; fi'
language: system
pass_filenames: false
always_run: true
Expand Down
9 changes: 8 additions & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ version: '3'

vars:
POSTER: ./bin/poster
TEST_FLAGS: -race -coverprofile=coverage.txt -covermode=atomic
RACE_FLAG:
sh: |
if go test -race ./internal/calculate >/dev/null 2>&1; then
echo "-race"
else
echo ""
fi
TEST_FLAGS: '{{.TEST_FLAGS | default (printf "%s -coverprofile=coverage.txt -covermode=atomic" .RACE_FLAG) | trim}}'
LINT_FLAGS: --timeout=5m
BANNER: |
╔══════════════════════════════════════════════════════════╗
Expand Down
13 changes: 8 additions & 5 deletions internal/load/loaders.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,25 @@ func SecureFilepath(baseDir, relPath string) (string, error) {
return "", fmt.Errorf("invalid base directory: %w", err)
}

// Resolve symlinks on base directory first to canonicalize path
if eval, err := filepath.EvalSymlinks(baseAbs); err == nil {
baseAbs = eval
}

target := filepath.Join(baseAbs, relPath)
targetAbs, err := filepath.Abs(target)
if err != nil {
return "", fmt.Errorf("invalid target path: %w", err)
}

// Resolve symlinks to prevent escaping
if eval, err := filepath.EvalSymlinks(baseAbs); err == nil {
baseAbs = eval
}
// Resolve symlinks on target to prevent escaping via symlinks
if eval, err := filepath.EvalSymlinks(targetAbs); err == nil {
targetAbs = eval
}

// Ensure target is inside base directory
if targetAbs != baseAbs && !strings.HasPrefix(targetAbs, baseAbs+string(os.PathSeparator)) {
rel, err := filepath.Rel(baseAbs, targetAbs)
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
return "", fmt.Errorf("file %s is outside allowed directory", relPath)
}

Expand Down
6 changes: 5 additions & 1 deletion internal/load/loaders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ func createSampleImage() image.Image {

func TestSecureFilepath(t *testing.T) {
tempDir := t.TempDir()
realTempDir, err := filepath.EvalSymlinks(tempDir)
if err != nil {
realTempDir = tempDir
}

// Valid relative path
validRel := "sub/file.png"
abs, err := load.SecureFilepath(tempDir, validRel)
if err != nil {
t.Fatalf("SecureFilepath() unexpected error = %v", err)
}
expected := filepath.Join(tempDir, "sub", "file.png")
expected := filepath.Join(realTempDir, "sub", "file.png")
if abs != expected {
t.Errorf("SecureFilepath() = %q, want %q", abs, expected)
}
Expand Down
Loading