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

Fix to address directory traversal #2 #3

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
28 changes: 28 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'

- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
30 changes: 30 additions & 0 deletions .github/workflows/goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: goreleaser

on:
push:
tags:
- '*'

permissions:
contents: write

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v4

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v5
with:
distribution: goreleaser
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
50 changes: 34 additions & 16 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,44 @@
env:
- GO111MODULE=on
- GOPROXY=https://gocenter.io
# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com

# The lines bellow are called `modelines`. See `:help modeline`
# Feel free to remove those if you don't want/need to use them.
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
# vim: set ts=2 sw=2 tw=0 fo=cnqoj

before:
hooks:
- go mod tidy
- go mod download

builds:
- env:
- CGO_ENABLED=0
- env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin

archives:
- replacements:
darwin: Darwin
linux: Linux
windows: Windows
386: i386
amd64: x86_64
- format: tar.gz
# this name template makes the OS and Arch compatible with the results of `uname`.
name_template: >-
{{ .ProjectName }}_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}v{{ .Arm }}{{ end }}
# use zip for windows archives
format_overrides:
- goos: windows
format: zip

checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ .Tag }}-next"

changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
- "^docs:"
- "^test:"
13 changes: 7 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"time"

"github.com/pin/tftp"
Expand All @@ -18,9 +18,10 @@ var dir string

// readHandler is called when client starts file download from server
func readHandler(filename string, rf io.ReaderFrom) error {
file_path := path.Join(dir, filepath.Clean(path.Join("/", filename)))

if _, err := os.Stat(path.Join(dir, filename)); err == nil {
file, err := os.Open(path.Join(dir, filename))
if _, err := os.Stat(file_path); err == nil {
file, err := os.Open(file_path)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return err
Expand All @@ -41,7 +42,7 @@ func readHandler(filename string, rf io.ReaderFrom) error {

fmt.Printf("%s %d bytes sent\n", filename, n)
} else { // File not found locally. Proxying the request.
fileUrl := url + "/" + filename
fileUrl := url + filepath.Clean(path.Join("/", filename))
resp, err := http.Get(fileUrl)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
Expand All @@ -50,8 +51,8 @@ func readHandler(filename string, rf io.ReaderFrom) error {
defer resp.Body.Close()

if resp.StatusCode != 200 {
io.Copy(ioutil.Discard, resp.Body)
return fmt.Errorf("Received status code: %d", resp.StatusCode)
io.Copy(io.Discard, resp.Body)
return fmt.Errorf("received status code: %d", resp.StatusCode)
}

rf.(tftp.OutgoingTransfer).SetSize(resp.ContentLength)
Expand Down