CodeQL 文档

通过 JavaScript 暴露访问 Java 对象方法

ID: java/android/webview-addjavascriptinterface
Kind: problem
Security severity: 6.1
Severity: warning
Precision: medium
Tags:
   - security
   - external/cwe/cwe-079
Query suites:
   - java-security-extended.qls
   - java-security-and-quality.qls

点击查看 CodeQL 代码库中的查询

调用 android.webkit.WebView 类的 addJavascriptInterface 方法允许 WebView 的网页通过 JavaScript 访问 Java 对象的方法。

暴露给 JavaScript 的对象在 WebView 的所有框架中都可用。

建议

如果需要将 Java 对象暴露给 JavaScript,请确保没有不受信任的第三方内容加载到 WebView 中。

示例

在以下(错误的)示例中,一个 Java 对象暴露给了 JavaScript。

import android.webkit.JavascriptInterface;
import android.database.sqlite.SQLiteOpenHelper;

class ExposedObject extends SQLiteOpenHelper {
    @JavascriptInterface
    public String studentEmail(String studentName) {
        // SQL injection
        String query = "SELECT email FROM students WHERE studentname = '" + studentName + "'";

        Cursor cursor = db.rawQuery(query, null);
        cursor.moveToFirst();
        String email = cursor.getString(0);

        return email;
    }
}

webview.getSettings().setJavaScriptEnabled(true);
webview.addJavaScriptInterface(new ExposedObject(), "exposedObject");
webview.loadData("", "text/html", null);

String name = "Robert'; DROP TABLE students; --";
webview.loadUrl("javascript:alert(exposedObject.studentEmail(\""+ name +"\"))");

参考文献

  • ©GitHub 公司
  • 条款
  • 隐私