CWE-245 J2EE不安全实践:对连接的直接管理

admin 2022年1月5日21:04:26CWE(弱点枚举)评论15 views2386字阅读7分57秒阅读模式

CWE-245 J2EE不安全实践:对连接的直接管理

J2EE Bad Practices: Direct Management of Connections

结构: Simple

Abstraction: Variant

状态: Draft

被利用可能性: unkown

基本描述

The J2EE application directly manages connections, instead of using the container's connection management facilities.

扩展描述

The J2EE standard forbids the direct management of connections. It requires that applications use the container's resource management facilities to obtain connections to resources. Every major web application container provides pooled database connection management as part of its resource management framework. Duplicating this functionality in an application is difficult and error prone, which is part of the reason it is forbidden under the J2EE standard.

相关缺陷

  • cwe_Nature: ChildOf cwe_CWE_ID: 695 cwe_View_ID: 1000 cwe_Ordinal: Primary

  • cwe_Nature: ChildOf cwe_CWE_ID: 695 cwe_View_ID: 699 cwe_Ordinal: Primary

适用平台

Language: {'cwe_Name': 'Java', 'cwe_Prevalence': 'Undetermined'}

常见的影响

范围 影响 注释
Other Quality Degradation

示例代码

In the following example, the class DatabaseConnection opens and manages a connection to a database for a J2EE application. The method openDatabaseConnection opens a connection to the database using a DriverManager to create the Connection object conn to the database specified in the string constant CONNECT_STRING.

bad Java

public class DatabaseConnection {

private static final String CONNECT_STRING = "jdbc:mysql://localhost:3306/mysqldb";
private Connection conn = null;

public DatabaseConnection() {
}

public void openDatabaseConnection() {

try {

conn = DriverManager.getConnection(CONNECT_STRING);

} catch (SQLException ex) {...}

}

// Member functions for retrieving database connection and accessing database
...

}

The use of the DriverManager class to directly manage the connection to the database violates the J2EE restriction against the direct management of connections. The J2EE application should use the web application container's resource management facilities to obtain a connection to the database as shown in the following example.

good

public class DatabaseConnection {

private static final String DB_DATASRC_REF = "jdbc:mysql://localhost:3306/mysqldb";
private Connection conn = null;

public DatabaseConnection() {
}

public void openDatabaseConnection() {

try {

InitialContext ctx = new InitialContext();
DataSource datasource = (DataSource) ctx.lookup(DB_DATASRC_REF);
conn = datasource.getConnection();

} catch (NamingException ex) {...}
} catch (SQLException ex) {...}

}

// Member functions for retrieving database connection and accessing database
...

}

分类映射

映射的分类名 ImNode ID Fit Mapped Node Name
7 Pernicious Kingdoms J2EE Bad Practices: getConnection()
Software Fault Patterns SFP3 Use of an improper API

文章来源于互联网:scap中文网

特别标注: 本站(CN-SEC.COM)所有文章仅供技术研究,若将其信息做其他用途,由用户承担全部法律及连带责任,本站不承担任何法律及连带责任,请遵守中华人民共和国安全法.
  • 我的微信
  • 微信扫一扫
  • weinxin
  • 我的微信公众号
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2022年1月5日21:04:26
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                  CWE-245 J2EE不安全实践:对连接的直接管理 http://cn-sec.com/archives/612814.html

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: