Skip to content

Commit

Permalink
rawx: accept chunk IDs shorter than 64 hexdigits
Browse files Browse the repository at this point in the history
Now it will accept between 24 and 64 hexdigits. Accepting less than 24
digits (96 bits) would lead to a high risk of collisions (it's just a
feeling, I didn't do the math).
  • Loading branch information
fvennetier committed Mar 26, 2021
1 parent f17deaa commit 5f99b9d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
11 changes: 6 additions & 5 deletions rawx/chunk_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func (chunk *chunkInfo) retrieveContentFullpathHeader(headers *http.Header) erro
chunk.ContentVersion = version

contentID, err := url.PathUnescape(fullpath[4])
if err != nil || !isHexaString(contentID, 0) {
if err != nil || !isHexaString(contentID, 0, 64) {
return errInvalidHeader
}
headerContentID := headers.Get(HeaderNameContentID)
Expand Down Expand Up @@ -345,7 +345,8 @@ func retrieveDestinationHeader(headers *http.Header, rawx *rawxService, srcChunk
return chunk, os.ErrPermission
}
chunk.ChunkID = filepath.Base(filepath.Clean(dstURL.Path))
if !isHexaString(chunk.ChunkID, 64) {
if !isHexaString(chunk.ChunkID, 24, 64) {
LogWarning("%s did not parse as hexadecimal string", chunk.ChunkID)
return chunk, errInvalidHeader
}
chunk.ChunkID = strings.ToUpper(chunk.ChunkID)
Expand Down Expand Up @@ -380,7 +381,7 @@ func retrieveHeaders(headers *http.Header, chunkID string) (chunkInfo, error) {

chunk.MetachunkHash = headers.Get(HeaderNameMetachunkChecksum)
if chunk.MetachunkHash != "" {
if !isHexaString(chunk.MetachunkHash, 0) {
if !isHexaString(chunk.MetachunkHash, 0, 64) {
return chunk, errInvalidHeader
}
chunk.MetachunkHash = strings.ToUpper(chunk.MetachunkHash)
Expand All @@ -394,7 +395,7 @@ func retrieveHeaders(headers *http.Header, chunkID string) (chunkInfo, error) {

chunk.ChunkHash = headers.Get(HeaderNameChunkChecksum)
if chunk.ChunkHash != "" {
if !isHexaString(chunk.ChunkHash, 0) {
if !isHexaString(chunk.ChunkHash, 0, 64) {
return chunk, errInvalidHeader
}
chunk.ChunkHash = strings.ToUpper(chunk.ChunkHash)
Expand All @@ -417,7 +418,7 @@ func (chunk *chunkInfo) patchWithTrailers(trailers *http.Header, ul uploadInfo)
if trailerMetachunkHash != "" {
chunk.MetachunkHash = trailerMetachunkHash
if chunk.MetachunkHash != "" {
if !isHexaString(chunk.MetachunkHash, 0) {
if !isHexaString(chunk.MetachunkHash, 0, 64) {
return errInvalidHeader
}
chunk.MetachunkHash = strings.ToUpper(chunk.MetachunkHash)
Expand Down
3 changes: 2 additions & 1 deletion rawx/filerepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ func (fr *fileRepository) linkRelPath(fromPath, toPath string) (linkOperation, e
if e0 := syscall.Faccessat(fr.rootFd, fromPath, syscall.F_OK, 0); e0 != nil {
return nil, err
}
if e0 := os.MkdirAll(filepath.Dir(toPath), fr.putMkdirMode); e0 != nil {
if e0 := os.MkdirAll(filepath.Dir(filepath.Join(fr.root, toPath)),
fr.putMkdirMode); e0 != nil {
return nil, err
}
default:
Expand Down
8 changes: 5 additions & 3 deletions rawx/handler_chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,11 @@ func (rr *rawxRequest) uploadChunk() {
func (rr *rawxRequest) copyChunk() {
var err error
if rr.chunk, err = retrieveDestinationHeader(&rr.req.Header, rr.rawx, rr.chunkID); err != nil {
rr.replyError("copyChunk()", err)
rr.replyError("copyChunk() dest", err)
return
}
if err := rr.chunk.retrieveContentFullpathHeader(&rr.req.Header); err != nil {
rr.replyError("copyChunk()", err)
rr.replyError("copyChunk() fullpath", err)
return
}

Expand Down Expand Up @@ -489,7 +489,9 @@ func (rr *rawxRequest) removeChunk() {
}

func (rr *rawxRequest) serveChunk() {
if !isHexaString(rr.req.URL.Path[1:], 64) {
// 24 digits (96 bits) seems reasonable to avoid collisions.
// TODO(FVE): make the minimum and maximum configurable
if !isHexaString(rr.req.URL.Path[1:], 24, 64) {
rr.replyError("", errInvalidChunkID)
return
}
Expand Down
4 changes: 2 additions & 2 deletions rawx/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ func init() {
}
}

func isHexaString(name string, length int) bool {
func isHexaString(name string, minLen int, maxLen int) bool {
var i int
var n rune
for i, n = range name {
if notHexa[byte(n)] {
return false
}
}
return length <= 0 || i+1 == length
return i+1 >= minLen && i < maxLen
}

func hasPrefix(s, prefix string) (string, bool) {
Expand Down

0 comments on commit 5f99b9d

Please sign in to comment.