作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Google 就 ContentProvider 中的路径遍历漏洞发出警告
https://support.google.com/faqs/answer/7496913
Implementations of
openFile
andopenAssetFile
in exportedContentProviders
can be vulnerable if they do not properly validate incomingUri
parameters. A malicious app can supply a craftedUri
(for example, one that contains “/../”) to trick your app into returning aParcelFileDescriptor
for a file outside of the intended directory, thereby allowing the malicious app to access any file accessible to your app.
public ParcelFileDescriptor openFile (Uri uri, String mode)
throws FileNotFoundException {
File f = new File(DIR, uri.getLastPathSegment());
if (!f.getCanonicalPath().startsWith(DIR)) {
throw new IllegalArgumentException();
}
return ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY);
}
DIR 指的是什么?我如何实现正确的修复?
最佳答案
DIR
指您要从中共享文件的应用程序内部存储中的目录。
这可能是:
File fd = getContext().getFilesDir();
String DIR = fd.getAbsolutePath() + "/public";
(结果:
/data/user/0/[APPLICATION_ID]/files/public
)
/data/user/0/[APPLICATION_ID]/databases
中的数据库文件是“意外文件”。)
content://[APPLICATION_ID]/public/../../databases/database.db
/data/user/0/[APPLICATION_ID]/files/public/../../databases/database.db
/data/user/0/[APPLICATION_ID]/databases/database.db
private String DIR;
@Override
public boolean onCreate() {
File fd = getContext().getFilesDir();
DIR = fd.getAbsolutePath() + "/public";
return true;
}
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode)
throws FileNotFoundException {
File f = new File(DIR, uri.getLastPathSegment());
if (!f.getCanonicalPath().startsWith(DIR)) {
throw new IllegalArgumentException();
}
return ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY);
}
(旁注:
DIR
应该是
dir
,因为它不是常数。)
关于android - ContentProvider 路径遍历漏洞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49812687/
我是一名优秀的程序员,十分优秀!