Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
X
xwiki-platform
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
XWiki
xwiki-platform
Commits
aca51acc
Unverified
Commit
aca51acc
authored
2 years ago
by
Thomas Mortagne
Committed by
GitHub
2 years ago
Browse files
Options
Downloads
Plain Diff
Merge pull request #2016 from xumix/master
parents
f6044441
b335fe13
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
xwiki-platform-core/xwiki-platform-resource/xwiki-platform-resource-temporary/src/main/java/org/xwiki/resource/temporary/internal/DefaultTemporaryResourceStore.java
+22
-11
22 additions, 11 deletions
...rce/temporary/internal/DefaultTemporaryResourceStore.java
with
22 additions
and
11 deletions
xwiki-platform-core/xwiki-platform-resource/xwiki-platform-resource-temporary/src/main/java/org/xwiki/resource/temporary/internal/DefaultTemporaryResourceStore.java
+
22
−
11
View file @
aca51acc
...
@@ -31,6 +31,7 @@
...
@@ -31,6 +31,7 @@
import
javax.inject.Inject
;
import
javax.inject.Inject
;
import
javax.inject.Singleton
;
import
javax.inject.Singleton
;
import
org.apache.commons.codec.digest.DigestUtils
;
import
org.apache.commons.io.IOUtils
;
import
org.apache.commons.io.IOUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.xwiki.component.annotation.Component
;
import
org.xwiki.component.annotation.Component
;
...
@@ -75,30 +76,40 @@ public File createTemporaryFile(TemporaryResourceReference reference, InputStrea
...
@@ -75,30 +76,40 @@ public File createTemporaryFile(TemporaryResourceReference reference, InputStrea
public
File
getTemporaryFile
(
TemporaryResourceReference
reference
)
throws
IOException
public
File
getTemporaryFile
(
TemporaryResourceReference
reference
)
throws
IOException
{
{
List
<
String
>
segments
=
new
ArrayList
<>();
List
<
String
>
segments
=
new
ArrayList
<>();
segments
.
add
(
"tmp"
);
segments
.
add
(
reference
.
getModuleId
());
int
safePathLength
=
2
;
if
(
reference
.
getOwningEntityReference
()
!=
null
)
{
if
(
reference
.
getOwningEntityReference
()
!=
null
)
{
for
(
EntityReference
component
:
reference
.
getOwningEntityReference
().
getReversedReferenceChain
())
{
for
(
EntityReference
component
:
reference
.
getOwningEntityReference
().
getReversedReferenceChain
())
{
segments
.
add
(
component
.
getName
());
segments
.
add
(
component
.
getName
());
safePathLength
++;
}
}
}
}
if
(!
reference
.
getParameters
().
isEmpty
())
{
if
(!
reference
.
getParameters
().
isEmpty
())
{
segments
.
add
(
String
.
valueOf
(
reference
.
getParameters
().
hashCode
()));
segments
.
add
(
String
.
valueOf
(
reference
.
getParameters
().
hashCode
()));
safePathLength
++;
}
}
segments
.
addAll
(
reference
.
getResourcePath
());
String
path
=
StringUtils
.
join
(
encode
(
segments
),
'/'
);
String
path
=
StringUtils
.
join
(
encode
(
segments
),
'/'
);
String
safePath
=
StringUtils
.
join
(
encode
(
segments
.
subList
(
0
,
safePathLength
)),
'/'
);
String
md5
=
DigestUtils
.
md5Hex
(
path
);
File
rootFolder
=
this
.
environment
.
getTemporaryDirectory
();
File
rootFolder
=
this
.
environment
.
getTemporaryDirectory
();
File
safeFolder
=
new
File
(
rootFolder
,
safePath
);
File
temporaryFile
=
new
File
(
rootFolder
,
path
);
List
<
String
>
finalPathSegments
=
new
ArrayList
<>();
finalPathSegments
.
add
(
"tmp"
);
finalPathSegments
.
add
(
reference
.
getModuleId
());
// Avoid having too many files in one folder because some filesystems don't perform well with large numbers of
// files in one folder
finalPathSegments
.
add
(
String
.
valueOf
(
md5
.
charAt
(
0
)));
finalPathSegments
.
add
(
String
.
valueOf
(
md5
.
charAt
(
1
)));
finalPathSegments
.
add
(
String
.
valueOf
(
md5
.
substring
(
2
)));
File
safeFolder
=
new
File
(
rootFolder
,
StringUtils
.
join
(
finalPathSegments
,
'/'
));
finalPathSegments
.
addAll
(
reference
.
getResourcePath
());
File
temporaryFile
=
new
File
(
rootFolder
,
StringUtils
.
join
(
finalPathSegments
,
'/'
));
// Make sure the resource path is not relative (e.g. "../../../") and tries to get outside of the safe folder.
// Make sure the resource path is not relative (e.g. "../../../") and tries to get outside of the safe folder.
if
(!
temporaryFile
.
getAbsolutePath
().
startsWith
(
safeFolder
.
getAbsolutePath
()))
{
if
(!
temporaryFile
.
getAbsolutePath
().
startsWith
(
safeFolder
.
getAbsolutePath
()))
{
String
resourcePath
=
StringUtils
.
join
(
encode
(
segments
.
subList
(
safePathLength
,
segments
.
size
())),
'/'
);
throw
new
IOException
(
String
.
format
(
"Resource path [%s] should be within [%s]"
,
temporaryFile
.
getAbsolutePath
(),
safeFolder
.
getAbsolutePath
()));
throw
new
IOException
(
String
.
format
(
"Invalid resource path [%s]."
,
resourcePath
));
}
}
return
temporaryFile
;
return
temporaryFile
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment