-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Use CleanPath instead of path.Clean #23371
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ import ( | |
"code.gitea.io/gitea/modules/setting" | ||
"code.gitea.io/gitea/modules/storage" | ||
"code.gitea.io/gitea/modules/templates" | ||
"code.gitea.io/gitea/modules/util" | ||
"code.gitea.io/gitea/modules/web/middleware" | ||
"code.gitea.io/gitea/modules/web/routing" | ||
"code.gitea.io/gitea/services/auth" | ||
|
@@ -44,7 +45,7 @@ func storageHandler(storageSetting setting.Storage, prefix string, objStore stor | |
routing.UpdateFuncInfo(req.Context(), funcInfo) | ||
|
||
rPath := strings.TrimPrefix(req.URL.Path, "/"+prefix+"/") | ||
rPath = path.Clean("/" + strings.ReplaceAll(rPath, "\\", "/"))[1:] | ||
rPath = util.CleanPath(strings.ReplaceAll(rPath, "\\", "/")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is wrong and is going to result in it being possible to have rPath have a preceding "/" whereas previously it was impossible. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If rPath has a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. util.CleanPath will result in rPath still having the / whereas previous it would always be removed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need clear definitions for every case, instead of using unpredictable The unstable part is :
But in many cases, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But that's not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the problem is that Callers should always know what they need - absolute or relative - no matter what path has been passed in the util function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. absolute or relative should not be the responsibility of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm .. that's a disagreement: I do not think so about "absolute or relative should not be the responsibility of CleanPath. It should be decided out of the function." IMO maybe it could be (like #23441) rel := util.PathRelJoin(....) // `/path` => `path`, `path` => `path`
abs := path.Join("/", util.PathRelJoin(....)) // if necessary |
||
|
||
u, err := objStore.URL(rPath, path.Base(rPath)) | ||
if err != nil { | ||
|
@@ -80,7 +81,7 @@ func storageHandler(storageSetting setting.Storage, prefix string, objStore stor | |
routing.UpdateFuncInfo(req.Context(), funcInfo) | ||
|
||
rPath := strings.TrimPrefix(req.URL.Path, "/"+prefix+"/") | ||
rPath = path.Clean("/" + strings.ReplaceAll(rPath, "\\", "/"))[1:] | ||
rPath = util.CleanPath(strings.ReplaceAll(rPath, "\\", "/")) | ||
if rPath == "" { | ||
http.Error(w, "file not found", http.StatusNotFound) | ||
return | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -207,7 +207,7 @@ func LFSLockFile(ctx *context.Context) { | |
ctx.Redirect(ctx.Repo.RepoLink + "/settings/lfs/locks") | ||
return | ||
} | ||
lockPath = path.Clean("/" + lockPath)[1:] | ||
lockPath = util.CleanPath(lockPath) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can result in a lockPath with a preceding "/". This is wrong. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and everywhere else too |
||
if len(lockPath) == 0 { | ||
ctx.Flash.Error(ctx.Tr("repo.settings.lfs_invalid_locking_path", originalPath)) | ||
ctx.Redirect(ctx.Repo.RepoLink + "/settings/lfs/locks") | ||
|
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.
I just read through the documentation of the method, this whole block is unnecessary and can be replaced with
return path.Clean("/" + p)[1:]
.That way, we ensure that the path is cleaned and that it is a relative path.
If we always want relative paths, we can just use that instead.
So the question is rather: Is there any use case where we need to clean an absolute path?
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.
I don't think so except we rename the function to
CleanAndEnsureRelativePath
.