Sei sulla pagina 1di 13

Secure Coding Practices in Java: Challenges and Vulnerabilities

Na Meng, Stefan Nagy, Daphne Yao, Wenjie Zhuang, Gustavo Arango Argoty
Virginia Tech
Blacksburg, Virginia 24060
{nm8247,snagy2,danfeng,kaito,gustavo1}@vt.edu

ABSTRACT 1 INTRODUCTION
Java platform and third-party libraries provide various security Java platform and third-party libraries or frameworks (e.g., Boun-
features to facilitate secure coding. However, misusing these fea- cyCastle [7] and Spring Security [53]) provide various features
tures can cost tremendous time and effort of developers or cause to facilitate secure coding. However, misusing these libraries and
arXiv:1709.09970v1 [cs.CR] 28 Sep 2017

security vulnerabilities in software. Prior research was focused on frameworks not only costs excessive debugging effort of developers,
the misuse of cryptography and SSL APIs, but did not explore the but also leads to security vulnerabilities in software [13, 63, 95, 96].
key fundamental research question: what are the biggest challenges For example, Veracode identified software errors in the handling of
and vulnerabilities in secure coding practices? In this paper, we con- user credentials, including hard-coded password and plaintext pass-
ducted a comprehensive empirical study on StackOverflow posts words in configuration files [63]. These errors can enable attackers
to understand developers’ concerns on Java secure coding, their to bypass access controls.
programming obstacles, and potential vulnerabilities in their code. Prior research mainly focused on the misuse of cryptography and
We observed that developers have shifted their effort to the usage SSL APIs that causes security vulnerabilities [78, 80, 83, 86]. Specif-
of authentication and authorization features provided by Spring ically, Lazar et al. manually examined 269 published cryptographic
security—a third-party framework designed to secure enterprise vulnerabilities in the CVE database, and observed 83% of them
applications. Multiple programming challenges are related to APIs were caused by cryptography API misuse [86]. Fahl et al. [80] and
or libraries, including the complicated cross-language data handling Georgiev et al. [83] separately implemented the man-in-the-middle
of cryptography APIs, and the complex Java-based or XML-based attack, and detected vulnerable Android applications and software
approaches to configure Spring security. More interestingly, we libraries that misused SSL APIs. Nadi et al. further investigated
identified security vulnerabilities in the suggested code of accepted the obstacles developers face while using the Java cryptography
answers. The vulnerabilities included using insecure hash functions APIs, the tasks for which they use the APIs, and the kind of tool
such as MD5, breaking SSL/TLS security through bypassing cer- support they desire [92]. Despite these studies, some key questions
tificate validation, and insecurely disabling the default protection on secure coding practices remain unanswered. They include (1)
against Cross Site Request Forgery (CSRF) attacks. Our findings whether programmers are equipped with sufficient security knowl-
reveal the insufficiency of secure coding assistance and education, edge and automatic coding support, and (2) whether the coding
and the gap between security theory and coding practices. practices benefitted from security research over the years.
For this paper, we conducted a comprehensive in-depth investiga-
CCS CONCEPTS tion on the common concerns, programming challenges, and security
vulnerabilities in developers’ secure coding practices by manually in-
• General and reference → Empirical studies;
specting 497 StackOverflow posts related to Java security. We chose
StackOverflow [62] because (1) developers usually share and dis-
KEYWORDS cuss programming issues and solutions on this online platform, and
CSRF, SSL/TLS certificate validation, cryptographic hash functions, (2) StackOverflow plays an important role in educating developers
authentication, authorization and impacting their daily coding practices. The main challenge of
performing this empirical study is interpreting each security-relevant
ACM Reference format: programming issue or solution within both the program context and
Na Meng, Stefan Nagy, Daphne Yao, Wenjie Zhuang, Gustavo Arango Ar- security context. To comprehend each post within the program con-
goty. 2017. Secure Coding Practices in Java: Challenges and Vulnerabilities. text, we manually checked all mentioned information about the
In Proceedings of ACM Conference, Washington, DC, USA, July 2017 (Confer- source code, configuration files, and/or execution environments.
ence’17), 13 pages. Then we decided the root cause and solution of the problem. To
https://doi.org/10.475/123_4
comprehend each post within the security context, we also identi-
fied the security requirement that developers tried to implement
and investigated the involved security libraries. Then we deter-
Permission to make digital or hard copies of part or all of this work for personal or mined whether the implementation fulfilled the requirement. Such
classroom use is granted without fee provided that copies are not made or distributed
for profit or commercial advantage and that copies bear this notice and the full citation
manual analysis requires so much comprehension and expertise in
on the first page. Copyrights for third-party components of this work must be honored. both software engineering (SE) and security that it is difficult to
For all other uses, contact the owner/author(s). automate the process.
Conference’17, July 2017, Washington, DC, USA
With our thorough manual analysis on the 497 posts, we investi-
© 2017 Copyright held by the owner/author(s).
ACM ISBN 123-4567-24-567/08/06. gated the following three research questions (RQs):
https://doi.org/10.475/123_4
Conference’17, July 2017, Washington, DC, USA Na Meng, Stefan Nagy, Daphne Yao, Wenjie Zhuang, Gustavo Arango Argoty

RQ1 What are the common concerns on Java secure coding? Al- 2 BACKGROUND
though there are various security libraries and frameworks [2, The examined StackOverflow posts were mainly about three per-
32, 34, 56, 84, 93], several questions are still unanswered, spectives of Java security: Java platform security, Java EE security,
such as (1) which are the popular security features being fre- and other third-party frameworks. This section introduces the key
quently asked about, and (2) what are the hard-to-implement terminologies used throughout the paper.
security defenses in practice?
RQ2 What are the common programming challenges? We aim to
identify the common obstacles that prevented developers
2.1 Java Platform Security
from implementing secure code easily and correctly. This The platform defines APIs spanning major security areas, including
information will help guide SE researchers and tool builders cryptography, access control, and secure communication [41].
to better develop tools, and to help close the gap between The Java Cryptography Architecture (JCA) contains APIs for
the intended library usage and developers’ actual usage. hashes, keys and certificates, digital signatures, and encryp-
RQ3 What are the common security vulnerabilities? We aim to tion [34]. Nine cryptographic engines are defined to provide either
identify security vulnerabilities in StackOverflow posts, be- cryptographic operations (encryption, digital signatures, hashes),
cause the bad practices recommended on the platform can generators or converters of cryptographic material (keys and algo-
become popular and cause profound negative impact. This rithm parameters), or objects (keystores or certificates) that encap-
effort will help raise the security consciousness of secure sulate the cryptographic data.
software practitioners. The access control architecture protects the access to sensitive re-
sources (e.g., local files) or sensitive application code (e.g., methods
In our study, we made three major observations. in a class). All access control decisions are mediated by a security
• There were multiple security vulnerabilities in the recommended manager. By default, the security manager uses the AccessController
code of some accepted answers. For instance, the usage of MD5 class for access control operations and decisions.
and SHA-1 algorithms were repetitively suggested, although Secure communication ensures that the data which travels across
these algorithms are notoriously insecure and should not a network is sent to the appropriate party, without being modified
be used anymore. Additionally, developers were advised to during the transmission. Cryptography forms the basis for secure
trust all incoming SSL/TLS certificates from servers as a communication. The Java platform provides API support for stan-
workaround to certificate verification errors. Such practice dard secure communication protocols like SSL/TLS. HTTPS, or
completely disables the security checks of SSL. Although “HTTP secure”, is an application-specific implementation that is a
this bad practice was initially reported by researchers in combination of HTTP and SSL/TLS.
2012 [80, 83], developers have still asked for and accepted
the practice till now. Furthermore, when implementing au- 2.2 Java EE Security
thentication with Spring security and getting errors, devel- Java EE is an standard specification for enterprise Java extensions [44].
opers were suggested with a workaround solution to blindly Various application servers are built to implement this specification,
disable the default security protection against Cross Site such as JBoss or WildFly [71], Glassfish [18], WebSphere [69],
Request Forgery (CSRF) attacks. and WebLogic [3]. A Java EE application consists of components
• There were various programming challenges related to security deployed into various containers. The Java EE security specification
libraries. For instance, developers were stuck with cryptog- defines that containers secure components by supporting features
raphy API usage due to clueless error messages, complex like authentication and authorization.
cross-language data handling, and delicate implicit API us- In particular, authentication defines how communicating en-
age constraints. However, when using Spring security, de- tities, such as a client and a server, prove to each other that they
velopers struggled a lot with the two alternative ways of are who they say they are. An authenticated user is issued a cre-
configuring security: Java-based or XML-based. dential, which includes user information like usernames/passwords
• Software developers have shifted their security implementation or tokens. Authorization ensures that users have permissions to
effort to Spring security since 2012. 261 of the 497 examined perform operations or access data. When accessing certain resource,
posts (53%) were about Spring security. However, we have a user is authorized if the server can map this user to a security
not seen any research that checks or analyzes the security role permitted for the resource.
vulnerabilities related to the framework. Security for Java EE applications can be implemented in the
following two ways:
The significance of this work is that we provided empirical ev-
idence for a significant number of alarming secure coding issues, • Declarative Security expresses an application component’s
which have not been previously reported. These issues are due security requirements using either deployment descrip-
to a variety of reasons, including the rapidly increasing need for tors or annotations. A deployment descriptor is an XML
enterprise security applications, the lack of security training in file external to the application. This XML file expresses an ap-
the software development workforce, and poorly designed security plication’s security structure, including security roles, access
libraries. We hope our findings can motivate the community to control, and authentication requirements. Annotations are
research solutions for helping developers overcome these obstacles used to specify security information in a class file. They can
in the long term. be either used by or overridden by deployment descriptors.
Secure Coding Practices in Java: Challenges and Vulnerabilities Conference’17, July 2017, Washington, DC, USA

• Programmatic Security is embedded in an application and 2) Removing posts without code snippets. We only focused on posts
is used to make security decisions, when declarative security containing code snippets to better understand the questions within
alone is not sufficient to express the security model. the program context. Since our crawled data did not include any
metadata to describe the existence of code snippets, we developed
2.3 Other Third-Party Frameworks an intuitive filter to search for keywords “public” and “class” in
Several frameworks were built to provide authentication, authoriza- each post. Based on our observation, a post usually contains these
tion, and other security features for enterprise applications, such two keywords when it includes a code snippet.
as Spring Security [53]. Different from the Java EE security APIs, 3) Discarding irrelevant posts. After applying the above two filters,
these frameworks are container independent, meaning that they do we manually examined the remaining posts, and decided whether
not require containers to implement security. For example, Spring they were relevant to Java secure coding or simply contained the
security is installed as a single Filter in the filter chain inside a checked keywords accidentally.
container to handle requests. There can be multiple security fil- With the above three filters, we finally included 497 posts in
ters inside Spring security. Developers can configure security in an our data set. The question asking time of these posts were during
XML-based way, a Java-based way, or a hybrid of the two. Simi- 2008-2016. We did not include the posts in 2017, because at the
lar to Java EE security, the XML-based way implements security time we conducted experiments, there was only data for the first
requirements with deployment descriptors and source code, while several months of 2017. When manually filtering retrieved posts,
the Java-based way expresses security with annotations and code. we also characterized relevant posts based on their security concerns,
programming challenges, and security vulnerabilities. Based on the
3 METHODOLOGY characterization, we classified posts and investigated the following
We leveraged Scrapy—an open source python library [50] to crawl three research questions (RQs):
posts from the StackOverflow website. Figure 1 presents the format RQ1: What are the common security concerns of develop-
of a typical StackOverflow post. Each post mainly contains two ers? We aimed to investigate: (1) what are the popular security
regions: the question and answers. features that developers frequently asked about, and (2) how do
○1 Question region contains the question description and some developers’ security concerns shift over the years? Besides, we
metadata. The metadata includes a vote for the question (e.g., 3)— also classified posts into three categories based on the number of
indicating whether the question is well-defined or well-representative, positive votes and favorite counts their questions received:
and a favorite count (e.g., 1)—showing how many people liking
• Neutral: A question does not receive a positive vote or fa-
the question.
vorite count.
○2 Answer region contains all answer(s) provided. When one
• Positive: A question receives at least one positive vote but
or more answers are provided, the asker decides which answer to
zero favorite count.
accept, and marks it with (✓).
• Favorite: A question receives at least one favorite vote.

java class to trust all for sending file to https web service The post shown in Figure 1 is classified as “Favorite” in this way,
because its favorite count is one. By combining this category with
I need to write my own class to tell mule that h3ps connec5on to service the identified security concerns, we explore developers’ sentiment
(wsdl) is verified. I already have mule project nearly finnished but last piece is
missing, sending file at specific url. towards questions related to different concerns. It is possible that
although some security features are frequently asked, people do
What I want to achieve: ①  not like the questions, probably because the questions are so com-

plicated and project-specific that most developers cannot learn or
benefit from them.

What worked for me is to set the TrustManagerFactory on the HTTPS


RQ2: What are the common programming challenges? For
connector. Here's how I did it. each identified security concern, we further characterized each

②  post with the problem (buggy source code, wrongly implemented
First, create a keystore that contains the cer5ficate of the SSL server you configuration files, improperly configured execution environment),
want to trust. You can create the keystore using the tools included with the
… the root cause and accepted solution of the problem. Then we
clustered posts if they had similar characterizations. For the post in
Figure 1: A highly viewed post asking about workarounds Figure 1, we identified the problem as asking for a workaround in
to bypass key checking and allow all host names for SSL verification, because apparently the developer did not realize
HTTPS [33] that the SSL verification should not be bypassed. The recommended
solution was to first create a keystore that contains the certificates
We obtained 22,195 posts containing keywords “java” and “se-
of all trusted SSL servers, and then use the keystore to create a
curity”. After extracting the question, the answers, and relevant
TrustManagerFactory instance and establish connections.
metadata for each post, we refined the data in three ways.
1) Filtering less useful posts. We automatically refined posts by RQ3: What are the common security vulnerabilities? For
removing duplicated posts, posts without accepted answers, and each post, we also inspected unaccepted answers and the conver-
posts whose questions received negative votes perhaps because the sational comments between the question asker and other devel-
questions were ill-formed or confusing. opers, in order to learn about the security context. Based on the
Conference’17, July 2017, Washington, DC, USA Na Meng, Stefan Nagy, Daphne Yao, Wenjie Zhuang, Gustavo Arango Argoty

All StackOverflow posts (497)

Implementa:on ques:ons (472) Understanding ques:ons (25)

Java plaDorm security (140) Java EE security (58) Spring security (261) Other (13)

Cryptography (64) Configura:on (26)


Access control (43) Other (2) Authoriza:on (16)
Secure communica:on (31) Authen:ca:on (219)

Figure 2: Taxonomy of StackOverflow posts

recommended security coding practice and the investigated secu-


Finding 1: 55%, 30%, and 12% of the implementation-
rity context, we decided whether the accepted solution was security
relevant posts focused on Spring security, Java platform
vulnerable. The post shown in Figure 1 contains a secure accepted
security, and Java EE security, indicating that developers
answer, although the question asker originally asked for a vulnera-
need more help to secure Java enterprise applications.
ble implementation as an easy fix.

Based on the second- and third-level classifications, we identified


4 MAJOR FINDINGS
seven major security concerns: cryptography, access control, secure
We present our investigation results for the research questions communication, Java EE security, authentication, authorization, and
separately in Section 4.1-4.3. configuration. The first three concerns correspond to Java platform
security, while the last three correspond to Spring security. To reveal
4.1 Common Concerns in Security Coding developers’ security concern trends over the years, we clustered
Figure 2 presents our classification hierarchy among the 497 posts. posts based on the year when each question was asked.
At the highest level of the hierarchy, we created two categories:
2008 Cryptography
implementation questions vs. understanding questions. The
Access control
majority—472 questions—were about implementing security func- 2009
Secure communica>on
tionalities or solving program errors. Only 25 questions were asked 2010
Java EE security
to understand why certain features were designed in certain ways 2011
Authen>ca>on
(e.g., how does Java string being immutable increase security [22]).
2012 Authoriza>on
Since our focus is on secure coding practice, our further classifica-
2013 Configura>on
tion expands on the 472 implementation-relevant posts.
At the second level of the classification hierarchy, we clustered 2014

posts based on the major security platforms or frameworks involved 2015


in each post. For instance, Java platform security posts were 2016
relevant to the security features of the Java platform and related
0 20 40 60 80 100 120
software libraries extending the features (e.g., BouncyCastle [7]).
Figure 3: The post distribution during 2008-2016
Java EE security posts were related to the security mechanisms of
Java EE platform, and Spring security posts were about the Spring Figure 3 presents the post distribution among 2008-2016. The
security framework [53]. The Other category at this level includes total number of posts increased over the years, indicating that more
posts relevant to other libraries or platforms, such as Shiro [4] and developers were involved with secure coding and faced problems.
Android [73]. Unexpectedly, Spring security posts (261) counted for Specifically, there was only 1 post created in 2008, but 107 posts
55% of the implementation questions. However, no research exists were created in 2016. During 2009-2011, most posts were about Java
to explore the misuse of Spring security APIs. Although there are platform security. However, since 2012, the major security concern
many application servers developed to implement the Java EE spec- has shifted to securing Java enterprise applications (including both
ifications, developers have many fewer questions concerning Java Java EE security and Spring security). Specifically, Spring security
EE security than Spring security. has taken up over 50% of the posts published every year since 2013.
At the third level of the classification hierarchy, we further classi- For each security concern, we also clustered posts based on devel-
fied posts separately belonging to the two categories: Java platform opers’ attitudes towards the questions. As mentioned in Section 3,
security and Spring security, because both categories contained we defined three types of sentiment: neutral, positive, and favorite.
many posts. Among the Java platform security posts, in addition to Figure 4 shows the post distribution among different developers’
cryptography and secure communication, we identified a third attitudes. In this figure, configuration posts received the highest
major concern—access control. Among the Spring security posts, percentage of neutral opinions (50%). One possible reason is that
we found the majority (219) related to authentication, with the these posts mainly focused on the problems caused by wrong ver-
minority discussing authorization and configuration. sions of software libraries and version conflicts between dependent
Secure Coding Practices in Java: Challenges and Vulnerabilities Conference’17, July 2017, Washington, DC, USA

Neutral Posi-ve Favorite secure two different sets of URLs of his/her Spring Boot web appli-
Cryptography
cation as shown below.

Access control
Listing 1: An exemplar implementation working unexpect-
edly in Spring Boot applications
Secure communica-on
1 @ En ab leW eb Se cur it y
Java EE security 2 public class SecurityConfiguration {
3 @ C o n f i g u r a t i o n @Order ( 1 )
Authen-ca-on
4 public s t a t i c c l a s s ApiConfigurationAdapter
Authoriza-on 5 extends WebSecurityConfigurerAdapter {
6 @Bean
Configura-on
7 public GenericFilterBean
0% 20% 40% 60% 80% 100% 8 apiAuthenticationFilter () { . . . }
9 @Override
Figure 4: The post distribution among developers’ sentiment 10 protected void configure ( HttpSecurity http )
towards the security features: neutral, positive, and favorite 11 throws Exception {
12 http . antMatcher ( " / api / ∗ ∗ " )
13 . addFilterAfter ( apiAuthenticationFilter ( ) . . . )
14 . sessionManagement ( ) . . . ; } }
libraries. Since such problems are usually specific to programmers’ 15 @ C o n f i g u r a t i o n @Order ( 2 )
software development environments, they are not representative 16 public s t a t i c c l a s s WebSecurityConfiguration
or relevant to many developers’ security interests. In comparison, 17 extends WebSecurityConfigurerAdapter {
secure communication posts received the lowest percentage of neu- 18 @Bean
tral opinions (16%), but the highest percentage of favorite (61%), 19 public GenericFilterBean
20 webAuthenticationFilter () { . . . }
indicating that the questions were more representative, focusing
21 @Override
more on security implementation instead of environment settings. 22 protected void configure ( HttpSecurity http )
23 throws Exception {
Finding 2: Developers’ major security concern has shifted 24 http . antMatcher ( " / " )
from Java platform security to enterprise application security 25 . addFilterAfter ( webAuthenticationFilter ( ) . . . )
over the years, especially to Spring security. Compared with 26 . authorizeRequests ( ) . . . ; } } }
others, secure communication posts received the highest per-
In Listing 1, lines 3-14 correspond to ApiConfigurationAdapter, a se-
centage (61%) of favorite votes, indicating that the questions
curity configuration class that specifies apiAuthenticationFilter to
are important and representative.
authenticate URLs matching the pattern “/api/**”. Lines 15-26 corre-
spond to WebSecurityConfiguration, which configures webAuthentication-
Filter to authenticate the other URLs. Ideally, only one filter is in-
4.2 Common Programming Challenges voked given one URL. However, both filters were invoked in reality.
To understand the common challenges developers faced, we further The root cause is that each filter is a bean (annotated with @Bean
examined the posts of the most popular five major categories: au- on lines 6 and 18). Spring Boot detects the filters and adds them to a
thentication (219), cryptography (64), Java EE security (58), access regular filter chain, while Spring security also adds them to its own
control (43), and secure communication (31). We identified posts filter chain. Consequently, both filters are registered twice and can
with similar questions and related answers, and further investi- be invoked twice. To solve the problem, developers need to enforce
gated why developers asked these common questions. This section each bean to be registered only once by adding specialized code.
presents our key findings for each category. Challenge 2: The two ways of security configurations (Java-based
and XML-based) are hard to implement correctly. Take the Java-
4.2.1 Authentication. Most posts were on (1) integrating Spring based configuration for example. There are lots of annotations and
security with different application servers (e.g., JBoss) [58] or frame- APIs of classes, methods, and fields available to specify different
works (e.g., Spring MVC) [54], (2) configuring security in an XML- configuration options. Particularly, HttpSecurity has 10 methods,
based [55] or Java-based way [27], or (3) converting XML-based each of which can be invoked on an HttpSecurity instance and then
configurations to Java-based ones [10]. Specifically, we observed produces another HttpSecurity object. If developers are not careful
three challenges. about the invocation order between these methods, they can get
Challenge 1: The way to integrate Spring security with different errors [25]. As shown in Listing 1, the method antMatcher("/api/**’’)
types of applications varies a lot. Although Spring security can be must be invoked before addFilterAfter(...) (lines 12-13), so that
leveraged to secure various applications, the usage varies with the the filter is only applied to URLs matching the pattern “/api/**”.
application settings [57]. What is even worse, some Spring security- Unfortunately, such implicit constraints are not documented in the
relevant implementations may exhibit different dynamic behaviors API specifications [28].
in different applications. For instance, by following a standard tu- Challenge 3: Converting from XML-based to Java-based configu-
torial example [68], a developer defined two custom authentica- rations is tedious and error-prone. The semantic conflicts between
tion filters—apiAuthenticationFilter and webAuthenticationFilter—to annotations, deployment descriptors, and code implementations
Conference’17, July 2017, Washington, DC, USA Na Meng, Stefan Nagy, Daphne Yao, Wenjie Zhuang, Gustavo Arango Argoty

are always hard to locate and resolve. Such problems become more 13 i n c l u d e ( ' C r y p t / RSA . php ' ) ;
serious when developers configure security in a hybrid way of 14 $ r s a = new Crypt_RSA ( ) ;
Java-based and XML-based. Since Spring security 3.2, developers 15 $ r s a −> s e t E n c r y p t i o n M o d e ( CRYPT_RSA_ENCRYPTION_OAEP ) ;
are supported to configure Spring-security in a pure Java-based 16 $ r s a −> l o a d K e y ( f i l e _ g e t _ c o n t e n t s ( ' p u b l i c . key ' ) ) ;
approach, and there is documentation describing how to migrate 17 // *****MainClass.java *****
18 BASE64Decoder d e c o d e r =new BASE64Decoder ( ) ;
from XML-based to Java-based configurations [56]. However, man-
19 String b64PrivateKey=getContents (
ually applying every migration rule to convert every configuration 20 " h t t p : / / l o c a l h o s t / a p i / k e y p a i r . php " ) . t r i m ( ) ;
file is tedious and error-prone. 21 b y t e [ ] decodedKey = d e c o d e r . d e c o d e B u f f e r ( b 6 4 P r i v a t e K e y ) ;
22 B u f f e r e d R e a d e r b r =new B u f f e r e d R e a d e r (
Finding 3: Spring security authentication posts were mainly 23 new S t r i n g R e a d e r ( new S t r i n g ( decodedKey ) ) ) ;
about configuring security for different applications or in 24 PEMReader p r =new PEMReader ( b r ) ;
different ways (Java-based or XML-based), and converting 25 K e y P a i r kp = ( K e y P a i r ) p r . r e a d O b j e c t ( ) ;
between these ways. The challenges were due to incomplete 26 pr . c l o s e ( ) ;
documentation and missing tool support for error checking, 27 P r i v a t e K e y p r i v a t e K e y =kp . g e t P r i v a t e ( ) ;
28 Cipher cipher =Cipher . g e t I n s t a n c e (
solution recommendation, and configuration generation.
29 " RSA / None / OAEPWithSHA1AndMGF1Padding " , " BC " ) ;
30 c i p h e r . i n i t ( C i p h e r . DECRYPT_MODE , p r i v a t e K e y ) ;
31 byte [] p l a i n t e x t = cipher . doFinal ( cipher ) ;
4.2.2 Cryptography. Many posts were about key generation and
usage. For instance, some posts discussed how to create a key from
scratch [38], or how to generate or retrieve a key from a random In this example, when a key pair is generated in PHP (lines 2-11),
number [26], a byte array [12], a string [15], a certificate [17], BigIn- the public key is easy to retrieve in PHP (lines 13-16). However,
tegers [6], a keystore [5], or a file [67]. Some other posts focused on retrieving the private key in Java is more complicated (lines 18-30).
how to compare keys [9], print key information [66], or initialize a After reading in the private key string (lines 19-20), the Java imple-
cipher for encryption and decryption [39]. Specifically, we observed mentation first uses Base64Decoder to decode the string into a byte
three common challenges of correctly using the cryptography APIs. array (line 21), which corresponds to an OpenSSL PEM encoded
Challenge 1: The error messages did not provide sufficient useful stream (line 22-23). Because OpenSSL PEM is not a standard data
hints about fixes. We found five posts concentrated on the same format, the Java code further uses a PEMReader to convert the
problem: “get InvalidKeyException: Illegal key size”, while the solu- stream to a PrivateKey instance (lines 24-27) before using the key
tions were almost identical: (1) download the “Java Cryptography to initialize a cipher (lines 28-30). Existing documentation seldom
Extension (JCE) Unlimited Strength Jurisdiction Policy Files”, “lo- describes how the security data format (e.g., key) defined in one
cal_policy.jar”, and “US_export_policy.jar”; and (2) place the policy language corresponds to that of another language. Unless develop-
files in proper folders [1]. Developers got the same exception be- ers are experts in both languages, it is hard for them to figure out
cause of missing either of the two steps. Providing a checklist of the security data processing across languages.
these necessary steps in the error message could help developers Challenge 3: Implicit constraints on API usage cause confusion. Two
quickly resolve the problem. However, the existing error messages posts were about getting “InvalidKeySpecException: algid parse
did not provide any constructive suggestion. error, not a sequence”, when obtaining a private key from a file [30].
Challenge 2: It is difficult to implement security with multiple The problem is that the key should be in PKCS#8 format when used
programming languages. Three posts were about encrypting data to create a PKCS8EncodedKeySpec instance, as shown below:
with one language (e.g. PHP or Python) and decrypting data with
another language (e.g., Java). Such cross-language data encryption
& decryption is challenging, because the format of the generated
data by one language requires special handling of another language. Listing 3: Consistency between the key format and keyspec
Listing 2 is an example to generate an RSA key pair and encrypt 1 / / p r i v K e y s h o u l d be i n PKCS #8 f o r m a t
data in PHP, and to decrypt data in Java [16]. 2 byte [ ] privKey = . . . ;
3 PKCS8EncodedKeySpec k e y S p e c =
Listing 2: Encryption in PHP and decryption in Java 4 new PKCS8EncodedKeySpec ( p r i v K e y ) ;
1 // *****keypair.php *****
2 i f ( f i l e _ e x i s t s ( ' p r i v a t e . key ' ) ) {
3 echo f i l e _ g e t _ c o n t e n t s ( ' p r i v a t e . key ' ) ; } The tricky part here is that a private key retrieved from a file always
4 else { has the data type byte[] even if it is not in PKCS#8 format. If devel-
5 i n c l u d e ( ' C r y p t / RSA . php ' ) ; opers invoke the API PKCS8EncodedKeySpec(...) with a non-PKCS#8
6 $ r s a = new Crypt_RSA ( ) ; formatted key, they will be stuck with the clueless exception. Three
7 $ r e s = $ r s a −> c r e a t e K e y ( ) ; solutions were suggested to get a PKCS#8 format key: (1) to imple-
8 $privateKey = $res [ ' privatekey ' ] ; ment code to convert the byte array, (2) to use an openssl command
9 $publicKey = $res [ ' publickey ' ] ;
to convert the file format, or (3) to use the PEMReader class of Boun-
10 f i l e _ p u t _ c o n t e n t s ( ' p u b l i c . key ' , $ p u b l i c K e y ) ;
cyCastle to generate a key from the file. Such implicit constraints
11 f i l e _ p u t _ c o n t e n t s ( ' p r i v a t e . key ' , $ p r i v a t e K e y ) ; }
12 // *****encrypt.php ***** between an API and its input format are delicate.
Secure Coding Practices in Java: Challenges and Vulnerabilities Conference’17, July 2017, Washington, DC, USA

Challenge 2: The effect of access control varies with the execu-


Finding 4: The cryptography posts were majorly about
tion environment. SecurityManager can disallow illegal accesses via
key generation and usage. Developers asked these questions
reflection only when the program is executed in a controlled envi-
mainly due to clueless error messages, cross-language data
ronment (i.e., on a trusted server) [8]. Nevertheless, if the program
handling, and implicit API usage constraints.
is executed in an uncontrolled environment (e.g. on an untrusted
client machine) and hackers can control how to run the program
or manipulate the jar file, the security mechanisms become voided.
4.2.3 Java EE security. 33 of the 58 posts were on authentication
and authorization. However, the APIs of these two security fea-
Finding 6: The access control posts were mainly about
tures were defined differently on different application servers (e.g.,
SecurityManager, AccessController, and the policy file. Config-
WildFly and Glassfish), and developers might use these servers in
uring and customizing access control policies are challenging.
combination with diverse third-party libraries [48]. As a result, the
posts seldom shared common solutions or code implementation.
One common challenge we identified is the usage of declarative
4.2.5 Secure Communication. Among the 31 examined posts,
security and programmatic security. When developers misunder-
22 posts were about SSL/TLS-related issues, discussing how to cre-
stood annotations, they could use incorrect annotations that conflict
ate [59], install [64], find [43], or validate an SSL certificate [61],
with other annotations [35], deployment descriptors [72], code im-
how to establish a secure connection [36], and how to use SSL to-
plementation [11], or file paths [47]. Nevertheless, existing error
gether with other libraries, such as JNDI [23] and PowerMock [70].
reporting systems only throw exceptions. Unfortunately, there is no
In particular, six posts focused on solving the problem of un-
tool support that prevents developers from configuring such con-
able to find a valid server certificate to establish an SSL connec-
flicting settings, or assists developers with diagnosing conflicting
tion with a server [43]. Instead of suggesting a way to install the
usage of annotations and deployment descriptors.
required certificates, two accepted answers suggested a highly in-
secure workaround to disable the SSL verification process, so that
Finding 5: Java EE security posts were mainly about au-
any incoming certificate can pass the validation [60]. Although
thentication and authorization. One challenge is the complex
such workarounds can effectively remove the error, they essen-
usage of declarative security and programmatic security, and
tially fail the requirement to secure communication with SSL. In
any complicated interaction between the two.
Section 4.3, we will further explain the security vulnerability due
to such workarounds. Probably developers tended to accept the
vulnerable answers because they felt it challenging to implement
4.2.4 Access Control. The 43 posts mainly discussed how to the whole process of creating, installing, finding, and validating an
restrict or relax the access permission(s) of a software application SSL certificate.
for certain resource(s).
Specifically, 21 questions asked about restricting untrusted code Finding 7: Security communication posts mainly discussed
from accessing certain packages [40], classes [42], or class members the process of establishing SSL/TLS connections. This process
(i.e., methods and fields) [20]. Two alternative solutions were com- contains so many steps that developers were tempted to accept
monly suggested for these questions: (1) to override the checkXXX() a broken solution to simply bypass the security check.
methods of SecurityManager to disallow invalid accesses, or (2) to
define a custom policy file to grant limited permissions. Another
nine posts were on how to allow applets to perform privileged
operations [52], because applets are executed in a security sand- 4.3 Common Problems from Security
box by default and can only perform a set of safe operations. One Perspectives
commonly recommended solution was to digitally sign the applet. Among the five categories listed in Section 4.2, we identified security
Although it seems that there exist common solutions to the most fre- vulnerabilities in the accepted answers of three frequently discussed
quently asked questions, the access control implementation is not topics: Spring security’s csrf(), SSL/TLS, and password hashing.
always intuitive. We identified two common challenges of correctly
implementing access control. 4.3.1 Spring security’s csrf(). Cross-site request forgery (CSRF)
Challenge 1: The effect of access control varies with the program is a serious attack that tricks a web browser into executing an
context. We identified two typical scenarios from multiple sim- unwanted action (e.g., transfer money to another account) in a
ilar posts. First, the RMI tutorial [29] suggested that a security web application (e.g., a bank website) for which a user is authenti-
manager is needed only when RMI code downloads code from a cated [102]. The root cause is that attackers created forged requests
remote machine. Including a SecurityManager instance in the RMI that appear to be legitimate requests, and somehow mixed them
program which does not download any code can cause an Access- with the legitimate ones. Since the application cannot distinguish
ControlException [37]. Second, although a signed applet is allowed between the two types of requests, it normally responds to the
to perform sensitive operations, it loses its privileges when in- forged requests, performing undesired operations.
voked from Javascript [21]. As a result, the invocation to the signed By default, Spring security provides CSRF protection by defining
applet should be wrapped with an invocation of AccessController. a function csrf() and implicitly enabling the function invocation.
doPrivileged(...). Correspondingly, developers should include the CSRF token in all
Conference’17, July 2017, Washington, DC, USA Na Meng, Stefan Nagy, Daphne Yao, Wenjie Zhuang, Gustavo Arango Argoty

PATCH, POST, PUT, and DELETE methods to leverage the protec- Listing 4: A typical implementation to disable SSL certificate
tion [31]. However, among the 12 examined posts that were relevant validation [51]
to csrf(), 5 posts discussed program failures, while all the accepted 1 // Create a trust manager that does not validate certificate chains
answers suggested an insecure solution: disabling the CSRF pro- 2 T r u s t M a n a g e r [ ] t r u s t A l l C e r t s = new T r u s t M a n a g e r [ ] {
tection by invoking http.csrf().disable(). In one instance, after ac- 3 new X 5 0 9 T r u s t M a n a g e r ( ) {
cepting the vulnerable solution, an asker commented as “Adding 4 public java . security . cert . X509Certificate []
csrf().disable() solved the issue!!! I have no idea why it was enabled getAcceptedIssuers ( ) { return null ; }
5 public void checkClientTrusted ( . . . ) { }
by default” [45]. Unfortunately, the developer happily disabled the
6 public void checkServerTrusted ( . . . ) { } } } ;
security protection without realizing that such workaround would 7 // Install the all-trusting trust manager
expose the resulting system to CSRF security exploits. 8 try {
9 S S L C o n t e x t s c = S S L C o n t e x t . g e t I n s t a n c e ( " SSL " ) ;
Finding 8: In 5 of the 12 csrf()-relevant posts, developers 10 s c . i n i t ( n u l l , t r u s t A l l C e r t s , new j a v a . s e c u r i t y .
took the suggestion to irresponsibly disable the default CSRF SecureRandom ( ) ) ;
protection. Developers are unaware of the security conse- 11 HttpsURLConnection . s e t D e f a u l t S S L S o c k e t F a c t o r y ( sc
quences of their insecure coding. . getSocketFactory ( ) ) ;
12 } catch ( Exception e ) { }
13 // Access an https URL without having the certificate in the
4.3.2 SSL/TLS. We examined 11 posts discussing the usage of 14 // truststore
SSL/TLS, and observed two important security issues. 15 try {
16 URL u r l =new URL ( " h t t p s : / / hostname / i n d e x . html " ) ;
Cer$ficate 17 } c a t c h ( MalformedURLException e ) { }
Authority
② Issue Disabling the SSL certificate validation process in a client can
① Request for an SSL cert.
thoroughly invalidate the secure communication protocol, leav-
an SSL cert.
ing clients susceptible to the man-in-the-middle (MITM) at-
tack [83]. Namely, by secretly relaying and possibly altering the
③ Ini$ate an SSL connec$on communication (e.g., through DNS poisoning) between the client
④ Send the SSL cert. and server, attackers can trick the SSL-client to instead connect
to an attacker-controlled server [83]. Although the insecure cod-
⑤ Validate SSL cert. ing practice was shown to induce the MITM attack in 2012 [83],
Client Server there are three examined posts created after 2012 still discussing
Figure 5: Simplified overview of creating an SSL connection the bad practice. This indicates a significant gap between secu-
Problem 1: Developers commonly trusted all SSL certificates and rity theory and coding practices. Some developers justified their
allowed all hostnames in order to quickly build a prototype in the checking-bypass logic by saying “Because I needed a quick solution
development environment. SSL is the standard security technology for debugging purposes only. I would not use this in production due to
for establishing an encrypted connection between a web server the security concerns . . . ” [65]. However, as pointed by another user
and a browser. Figure 5 overviews the major steps of establishing of StackOverflow [65] and demonstrated by prior research [80, 83],
an SSL connection [49]. To activate SSL on the server, developers a lot of these implementations find their way into production,
need to provide all identity information of the website (e.g., the and have yielded radically insecure systems as a result.
host name) to a Certification Authority (CA) and request for an SSL Problem 2: Developers were unaware of the best usage of SSL/TLS.
1 After validating the website’s information, CA
certificate (Step ○). TLS is SSL’s successor. It is so different from SSL that the two proto-
2 When a client or
issues a digitally signed SSL certificate (Step ○). cols do not interoperate. To maintain the backwards compatibility
browser attempts to connect to the website ((Step ○), 3 the server with SSL 3.0 and interoperate with systems supporting SSL, most
4 The client then conducts several
sends over its certificate (Step ○). SSL/TLS implementations allow for protocol version negotiation: if
checks, including (1) whether the certificate is issued by a CA the a client and a server cannot connect via TLS, they will fall back to
browser trusts, and (2) whether the request hostname matches the using the older protocol SSL 3.0. In 2014, Möller et al. reported the
5 If all these checks
hostname associated with the certificate (Step ○). POODLE attack which exploits the SSL 3.0 fallback [91]. Specif-
are passed, the SSL connection can be established successfully. ically, there is a design vulnerability in the way SSL 3.0 handles
Although ideally, developers should only enable SSL after ob- block cipher mode padding, which can be exploited by attackers to
taining a certificate from CA, in reality, they usually implement decrypt encrypted messages. With the POODLE attack, a hacker
and test the certificate verification code before obtaining the cer- can intentionally trigger a TLS connection failure and force usage
tificate. Therefore, a well-accepted recommended solution without of SSL 3.0, allowing decryption of encrypted messages.
CA-signed certificates is to create a self-signed certificate and use Ever since 2014, researchers have recommended developers to
the certificate to drive the implementation of SSL certificate verifica- disable SSL 3.0 support and configure systems to present the SSL
tion [59]. However, 9 of the 11 examined posts accepted an insecure 3.0 fallback. The US government (NIST) mandates to ceasing usage
solution to bypass security checks by trusting all certificates and/or of SSL in the protection of Federal information [19]. In reality,
allowing all hostnames, as demonstrated by Listing 4. nevertheless, none of the 11 posts mentioned the POODLE attack.
Secure Coding Practices in Java: Challenges and Vulnerabilities Conference’17, July 2017, Washington, DC, USA

Both of the two examined posts created in 2016 were about SSL are not normally accessible to be accessed and thus can cause poten-
usage. tial vulnerabilities. Lazar et al. manually examined 269 published
cryptographic vulnerabilities in the CVE database, and observed
Finding 9: 9 of 11 SSL/TLS-relevant posts discussed insecure 83% of them were caused by the misuse of cryptographic libraries,
code to bypass security checks. We observed two important including low encryption strength, insufficient randomness, and in-
security threats: (1) StackOverflow contains a lot of obsolete adequate checks [86]. Veracode reported that 39% of all applications
and insecure coding practices; and (2) secure programmers used broken or risky cryptographic algorithms [63]. The study by
are unaware of the state-of-the-art security knowledge. Yang et al. [101] is most relevant to our research. They used an
advanced topic model approach, Latent Dirichlet Allocation (LDA)
tuned using Genetic Algorithm (GA), to cluster security-related
4.3.3 Password Hashing. We found six posts related to hash- StackOverflow questions based on the text. They identified fre-
ing passwords with MD5 or SHA-1 to store the user credentials quently mentioned security topics like “Password” and “Hash” for
in a database. However, these hashing functions were found in- categorization purposes. In comparison, we investigate the pro-
secure [99, 100]. They are vulnerable to offline dictionary at- gramming challenges and security vulnerabilities among the topics.
tacks [14]. After obtaining a password hash H from a compro- Our SE and security findings have more technical depth.
mised database, a hacker can use brute-force methods to enumerate
a list of password guesses, until finding the password P whose hash
value is H . By impersonating a valid user to login a server, the at-
5.2 Detecting Security Vulnerabilities
tacker can conduct malicious behaviors. Researchers recommended Approaches were built to detect security vulnerabilities caused
key-stretching algorithms (e.g., PBKDF2, bcrypt, and scrypt) as by API misuse [75, 78, 80, 81, 83, 85, 87, 94]. For instance, Egele
the best practice for secure password hashing, because these algo- et al. implemented a static checker for six well defined Android
rithms are specially crafted to slow down the hash computation by cryptographic API usage rules, such as “Do not use ECB mode for
orders of magnitude [74, 82, 98], which substantially increases the encryption”, and analyzed 11,748 Android applications for any rule
difficulty of dictionary attacks. violation [78]. They found 88% of the applications violating at least
Unfortunately, only three of the six posts (50%) mentioned the one checked rule. Fischer et al. extracted Android security-related
best practice in the accepted answers, indicating that many posts on code snippets from StackOverflow, and manually labeled a subset
secure hashing suggested insecure hash functions. We found one of the data as “secure” or “insecure” [81]. The labeled data allowed
post which asked about using MD5 hashing in Android [46]. Within them to train a classifier and efficiently judge whether a code snip-
the comment conversation between developers, although people pet is secure or not for the whole data set. Next, they searched for
recommended to avoid MD5, the asker kept justifying his/her choice code clones of the snippets in 1.3 million Android apps, and found
of MD5. The asker even shared a completely wrong understanding many clones of the insecure code. Fahl et al. [80] and Georgiev
of secure hashing: “The security of hash algorithms really is MD5 et al. [83] separately implemented an attack model: man-in-the-
(strongest) > SHA-1 > SHA-256 > SHA-512 (weakest)”, although the op- middle attack, and detected vulnerable Android applications and
posite is true, which is MD5 < SHA-1 < SHA-256 < SHA-512. Among popular software libraries which misused SSL APIs. Both research
these posts, some developers misunderstood security APIs and ig- groups observed that developers disabled certification validation
nored the security consequences of their security API choices. Such for testing with self-signed and/or trusted certificates. He et al. de-
StackOverflow posts can have profound negative impact, because veloped SSLINT, an automatic static analysis tool, to identify the
they convey the wrong information and may mislead people. misuse of TLS/SSL APIs in client-side applications [85].
Compared with prior research, our study has two new contri-
Finding 10: Three of six hashing-relevant posts accepted butions. First, our scope is broader. We report new challenges on
vulnerable solutions as correct answers, indicating that de- secure coding practices, such as complex security configurations in
velopers were unaware of the best practice of secure pro- Spring security, poor error messages, and multilingual programs.
gramming. Their wrong knowledge or practice can propagate Second, our investigation on the online forum provides a new social
among StackOverflow users and negatively influence people. and community perspective about secure coding. The unique insights
cannot be discovered through analyzing code.

5 RELATED WORK 5.3 Preventing Security Vulnerabilities


This section describes related work on analyzing, detecting, and Various techniques were proposed to prevent developers from im-
preventing security vulnerabilities due to Java library misuse. plementing vulnerable code and misusing APIs [76, 77, 79, 89, 90,
97]. For example, Mettler et al. designed Joe-E—a security-oriented
5.1 Analyzing Security Vulnerabilities subset of Java—to support secure software development by remov-
Prior studies showed that the API misuse of cryptography, SSL, and ing any encapsulation-breaking features from Java (e.g., reflection),
Java reflection caused many security vulnerabilities [63, 86, 88, 101]. and by enforcing the least privilege principle (i.e., by default, each
For instance, Long identified several Java features whose misuse or Joe-E object has no privilege to access system resources, unless an-
improper implementation can compromise security [88]. One fea- other entity passes it a reference to a system resource object) [89].
ture he identified is the Java reflection API which enables fields that Keyczar is a library designed to simplify the usage of cryptography,
Conference’17, July 2017, Washington, DC, USA Na Meng, Stefan Nagy, Daphne Yao, Wenjie Zhuang, Gustavo Arango Argoty

and thus to prevent API misuse [77]. Below shows how Keyczar 7 THREATS TO VALIDITY
APIs are used to decrypt data: This study is mainly based on our manual inspection of Java security-
Listing 5: Simple decryption with Keyczar APIs relevant posts, so the observations may be subject to human bias.
To alleviate the problem, the first author of the paper carefully
1 C r y p t e r c r y p t e r =new C r y p t e r ( " / r s a k e y s " ) ;
inspected all posts relevant to implementation questions multiple
2 String plaintext = crypter . decrypt ( ciphertext ) ;
times, while the second author also examined the posts related to
Compared with the decryption code shown in Listing 2 (lines 18-31), security vulnerabilities (mentioned in Section 4.3) multiple times.
this implementation is much simpler and more intuitive. All details To remove posts without code snippets, we defined a filter to
about data format conversion and cipher initialization are hidden, search for keywords “public” and “class”. If a post does not contain
while a default strong block cipher is used to properly decrypt data. both words, the filter automatically removes the post from our data
Some approaches were developed to apply formal verification set. This filter may incorrectly remove some relevant posts that
techniques and analyze the security properties of cryptographic contain code. In future, we will improve our crawling technique
protocol specifications [76, 90] and cryptographic API implemen- to keep the <code> tags around code snippets in the raw data, and
tations [79, 97]. For instance, Protocol Composition Logic (PCL) then rely on these tags to filter posts more precisely.
is a logic for proving security properties, like network protocols We conservatively mentioned posts whose accepted answers
that use public and symmetric key cryptography [76]. The logic is will cause security vulnerabilities, although there might be more
designed around a process calculus with actions for possible proto- accepted answers that suffer from known security attacks. Due to
col steps including generating new random numbers, sending and the limited available program and environment information in each
receiving messages, and performing decryption and digital signa- post, and our limited knowledge about frameworks and potential
ture verification actions. The proof system consists of axioms about security attacks, we decided not to mention the suspicious posts
individual protocol actions and inference rules that yield assertions whose accepted answers might lead to security vulnerabilities.
about protocols composed of multiple steps.
8 CONCLUSION
6 OUR RECOMMENDATIONS Our work aimed at assessing the current secure coding practices,
By analyzing the StackOverflow posts relevant to Java security and identifying potential gaps between theory and practice and be-
from both software engineering and security perspectives, we ob- tween specification and implementation. Our analysis of hundreds
served the gap between the intended usage of APIs and the actual of posts on the popular developer forum (StackOverflow) revealed
problematic API usage by developers, sensed developers’ frustra- a worrisome reality in the software development industry.
tion when they spent tremendous time figuring out the correct
• A substantial number of developers do not appear to un-
usage of APIs (e.g., two weeks as mentioned in [54]), and observed
derstand the security implications of coding options, show-
terrible security consequences of library misuse. Below are our
ing a lack of cybersecurity training. This situation creates
recommendations based on the analysis.
frustration in developers, who sometimes end up choosing
For Security Developers. Conduct security testing to check whether completely insecure-but-easy fixes. Examples of such easy
the implemented features work as expected. Do not disable security fixes include using obsolete cryptographic hash functions,
checks (e.g., SSL certificate validation) to implement a temporary disabling cross-site request forgery protection, trusting all
workaround in the testing or development environment. Be cautious certificates in HTTPS verification, or using obsolete commu-
when following the StackOverflow accepted answers to implement nication protocols. These poor coding practices, if used in
secure code, because these solutions may be unsafe and outdated. production code, will seriously compromise the security of
For administrators of StackOverflow, we recommend them to care- software products.
fully handle the posts that suggest vulnerable code, because these • We provided substantial empirical evidences showing that
posts can play an influential negative role when educating security APIs in Spring security (designed for enterprise security ap-
programmers. plications) are overly complicated and poorly documented,
and error reports from runtime systems cause confusion. In
For Library Designers. Remove or deprecate the APIs whose
addition, multi-language support for securing data is rather
security protection is broken (e.g., MD5). Design clean and helpful
weak. The multi-language situation is common in security
error reporting interfaces which show not only the error, but also
applications, as oftentimes the data is encrypted in one pro-
the possible root causes and solutions. Design simplified APIs with
gramming language (e.g., Python) and decrypted in another
strong security defenses implemented by default.
(e.g., Java). These issues can seriously hinder developers’
For Tool Builders. Develop automatic tools to diagnose security productivity, resulting in great frustration and confusion.
errors, locate buggy code, and suggest security patches or solutions. • Interestingly, we found that the social dynamics among
Build vulnerability prevention techniques that compare peer appli- askers and responders can impact people’s security choices.
cations that use the same set of APIs to infer and warn potential Some influential posts are not secure. For example, we ob-
misuses. Explore approaches that check and enforce the seman- served that in some cases an insecure suggestion by a user
tic consistency between security-relevant annotations, code, and with a high reputation score was selected as the accepted an-
configurations. Build new approaches to transform between the swer, as opposed to the correct fix by a user with a lower rep-
implementations of declarative security and programmatic security. utation score [24]. Sometimes insecure answers have many
Secure Coding Practices in Java: Challenges and Vulnerabilities Conference’17, July 2017, Washington, DC, USA

positive StackOverflow votes (as the quick fixes indeed make [26] [n. d.]. How to generate secret key using SecureRan-
error messages go away) [51], which is quite misleading. dom.getInstanceStrong()? https://stackoverflow.com/questions/37244064/
how-to-generate-secret-key-using-securerandom-getinstancestrong. ([n. d.]).
We described several possible solutions to improve secure coding [27] [n. d.]. How to override Spring Security default configuration
in Spring Boot. https://stackoverflow.com/questions/35600488/
practices in the paper. However, efforts (e.g., workforce retraining) how-to-override-spring-security-default-configuration-in-spring-boot.
to correct these alarming security issues may take a while to take ([n. d.]).
effect. Our future work is on building automatic or semi-automatic [28] [n. d.]. HttpSecurity (Spring Security 4.2.3.RELEASE API). https://docs.spring.
io/spring-security/site/docs/current/apidocs/org/springframework/security/
security bug detection and fixing tools. config/annotation/web/builders/HttpSecurity.html. ([n. d.]).
[29] [n. d.]. Implementing a Remote Interface. http://docs.oracle.com/javase/tutorial/
rmi/implementing.html. ([n. d.]).
REFERENCES [30] [n. d.]. InvalidKeySpecException : algid parse error, not
[1] [n. d.]. AES-256 implementation in GAE. https://stackoverflow.com/questions/ a sequence. https://stackoverflow.com/questions/31941413/
12833826/aes-256-implementation-in-gae. ([n. d.]). invalidkeyspecexception-algid-parse-error-not-a-sequence. ([n. d.]).
[2] [n. d.]. Apache Shiro Documentation. https://shiro.apache.org/documentation. [31] [n. d.]. java - Simple example of Spring Security with
html. ([n. d.]). Thymeleaf. https://stackoverflow.com/questions/25692735/
[3] [n. d.]. Application Server - Oracle WebLogic Server. https://www.oracle.com/ simple-example-of-spring-security-with-thymeleaf. ([n. d.]).
middleware/weblogic/index.html. ([n. d.]). [32] [n. d.]. Java Authentication and Authorization Service (JAAS) Reference
[4] [n. d.]. authc filter is not calling MyRealm in shiro Guide. https://docs.oracle.com/javase/8/docs/technotes/guides/security/jaas/
with spring. https://stackoverflow.com/questions/30463057/ JAASRefGuide.html. ([n. d.]).
authc-filter-is-not-calling-myrealm-in-shiro-with-spring. ([n. d.]). [33] [n. d.]. java class to trust all for sending file to https
[5] [n. d.]. Basic Program for encrypt/Decrypt : javax.crypto.BadPaddingException: web service. https://stackoverflow.com/questions/21156929/
Decryption error. https://stackoverflow.com/questions/39518979/ java-class-to-trust-all-for-sending-file-to-https-web-service. ([n. d.]).
basic-program-for-encrypt-decrypt-javax-crypto-badpaddingexception-decryption. [34] [n. d.]. Java Cryptography Architecture. http://docs.oracle.com/javase/7/docs/
([n. d.]). technotes/guides/security/crypto/CryptoSpec.html. ([n. d.]).
[6] [n. d.]. BigInteger to Key. https://stackoverflow.com/questions/10271164/ [35] [n. d.]. Java EE 7 EJB Security not working. https://stackoverflow.com/questions/
biginteger-to-key. ([n. d.]). 30504131/java-ee-7-ejb-security-not-working. ([n. d.]).
[7] [n. d.]. Bouncy Castle. https://www.bouncycastle.org. ([n. d.]). [36] [n. d.]. Java Mail get mails with pop3 from
[8] [n. d.]. Can a secret be hidden in a ’safe’ java class offering exchange server => Exception in thread “main”
access credentials? https://stackoverflow.com/questions/5761519/ javax.mail.MessagingException. https://stackoverflow.com/questions/25017050/
can-a-secret-be-hidden-in-a-safe-java-class-offering-access-credentials. java-mail-get-mails-with-pop3-from-exchange-server-exception-in-thread-main.
([n. d.]). ([n. d.]).
[9] [n. d.]. Compare two Public Key values in java [duplicate]. https://stackoverflow. [37] [n. d.]. Java RMI / access denied. https://stackoverflow.com/questions/36570012/
com/questions/37439695/compare-two-public-key-values-in-java. ([n. d.]). java-rmi-access-denied. ([n. d.]).
[10] [n. d.]. Configure Spring Security without XML in Spring 4. https://stackoverflow. [38] [n. d.]. Java Security - RSA Public Key & Private Key
com/questions/20961600/configure-spring-security-without-xml-in-spring-4. Code Issue. https://stackoverflow.com/questions/18757114/
([n. d.]). java-security-rsa-public-key-private-key-code-issue. ([n. d.]).
[11] [n. d.]. @Context injection in Stateless EJB used by JAX-RS. https://stackoverflow. [39] [n. d.]. Java security init Cipher from SecretKeySpec
com/questions/29132547/context-injection-in-stateless-ejb-used-by-jax-rs. ([n. properly. https://stackoverflow.com/questions/14230096/
d.]). java-security-init-cipher-from-secretkeyspec-properly. ([n. d.]).
[12] [n. d.]. Converted secret key into bytes, how to convert it [40] [n. d.]. Java Security Manager completely disable reflection. https://stackoverflow.
back to secrect key? https://stackoverflow.com/questions/5364338/ com/questions/40218973/java-security-manager-completely-disable-reflection.
converted-secret-key-into-bytes-how-to-convert-it-back-to-secrect-key. ([n. d.]).
([n. d.]). [41] [n. d.]. Java Security Overview. http://docs.oracle.com/javase/8/docs/technotes/
[13] [n. d.]. CWE-227: Improper Fulfillment of API Contract (’API Abuse’). https: guides/security/overview/jsoverview.html. ([n. d.]).
//cwe.mitre.org/data/definitions/227.html. ([n. d.]). [42] [n. d.]. Java security: Sandboxing plugins loaded via URL-
[14] [n. d.]. Dictionary Attacks 101. https://blog.codinghorror.com/ ClassLoader. https://stackoverflow.com/questions/3947558/
dictionary-attacks-101/. ([n. d.]). java-security-sandboxing-plugins-loaded-via-urlclassloader. ([n. d.]).
[15] [n. d.]. Edit code sample to specify DES key value. [43] [n. d.]. Java SSL - InstallCert recognizes certificate, but still “unable to find
EditcodesampletospecifyDESkeyvalue. ([n. d.]). valid certification path” error? https://stackoverflow.com/questions/11087121/
[16] [n. d.]. Encryption PHP, Decryption Java. https://stackoverflow.com/questions/ java-ssl-installcert-recognizes-certificate-but-still-unable-to-find-valid-c. ([n.
15639442/encryption-php-decryption-java. ([n. d.]). d.]).
[17] [n. d.]. Get public and private key from ASN1 encrypted pem certifi- [44] [n. d.]. JSR-000366 Java Platform, Enterprise Edition 8 Public Review Specification.
cate. https://stackoverflow.com/questions/30392114/get-public-and-private-key- http://download.oracle.com/otndocs/jcp/java_ee-8-pr-spec/. ([n. d.]).
from-asn1-encrypted-pem-certificate. ([n. d.]). [45] [n. d.]. logout call - Spring security logout call. https://stackoverflow.com/
[18] [n. d.]. GlassFish. https://javaee.github.io/glassfish/. ([n. d.]). questions/24530603/spring-security-logout-call. ([n. d.]).
[19] [n. d.]. Guidelines for the Selection, Configuration, and Use of Trans- [46] [n. d.]. MD5 hashing in Android. https://stackoverflow.com/questions/4846484/
port Layer Security (TLS) Implementations. http://nvlpubs.nist.gov/nistpubs/ md5-hashing-in-android. ([n. d.]).
SpecialPublications/NIST.SP.800-52r1.pdf. ([n. d.]). [47] [n. d.]. PicketLink / Deltaspike security does not work in SOAP (JAX-
[20] [n. d.]. Hiding my security key from java reflection. https://stackoverflow.com/ WS) layer (CDI vs EJB?). https://stackoverflow.com/questions/32392702/
questions/14903318/hiding-my-security-key-from-java-reflection. ([n. d.]). picketlink-deltaspike-security-does-not-work-in-soap-jax-ws-layer-cdi-vs-ej.
[21] [n. d.]. How can I get a signed Java Applet to perform privileged operations when ([n. d.]).
called from unsigned Javascript? https://stackoverflow.com/questions/1006674/ [48] [n. d.]. Resteasy Authorization design - check a user owns
how-can-i-get-a-signed-java-applet-to-perform-privileged-operations-when-called. a resource. https://stackoverflow.com/questions/34315838/
([n. d.]). resteasy-authorization-design-check-a-user-owns-a-resource. ([n. d.]).
[22] [n. d.]. How does Java string being immutable increase [49] [n. d.]. RF 6101 - The Secure Sockets Layer (SSL) Protocol Version 3.0. https:
security? https://stackoverflow.com/questions/15274874/ //tools.ietf.org/html/rfc6101. ([n. d.]).
how-does-java-string-being-immutable-increase-security. ([n. d.]). [50] [n. d.]. Scrapy | A Fast and Powerful Scraping and Web Crawling Framework.
[23] [n. d.]. how to accept self-signed certificates for JNDI/L- https://scrapy.org. ([n. d.]).
DAP connections? https://stackoverflow.com/questions/4615163/ [51] [n. d.]. security - Allowing Java to use an untrusted certificate for SS-
how-to-accept-self-signed-certificates-for-jndi-ldap-connections. ([n. d.]). L/HTTPS connection. https://stackoverflow.com/questions/1201048/
[24] [n. d.]. How to add MD5 or SHA hash to spring security? https://stackoverflow. allowing-java-to-use-an-untrusted-certificate-for-ssl-https-connection.
com/questions/18581463/how-to-add-md5-or-sha-hash-to-spring-security. ([n. ([n. d.]).
d.]). [52] [n. d.]. security exception when loading web image in jar. https://stackoverflow.
[25] [n. d.]. How to apply spring security filter only on se- com/questions/2011407/security-exception-when-loading-web-image-in-jar.
cured endpoints? https://stackoverflow.com/questions/36795894/ ([n. d.]).
how-to-apply-spring-security-filter-only-on-secured-endpoints. ([n. d.]).
Conference’17, July 2017, Washington, DC, USA Na Meng, Stefan Nagy, Daphne Yao, Wenjie Zhuang, Gustavo Arango Argoty

[53] [n. d.]. Spring Security. https://projects.spring.io/spring-security/. ([n. d.]). [79] Levent Erkök and John Matthews. 2008. Pragmatic Equivalence and Safety
[54] [n. d.]. Spring Security 4 xml configuration UserDetailsService authen- Checking in Cryptol. In Proceedings of the 3rd Workshop on Programming Lan-
tication not working. https://stackoverflow.com/questions/41321176/ guages Meets Program Verification (PLPV ’09). ACM, New York, NY, USA, 73–82.
spring-security-4-xml-configuration-userdetailsservice-authentication-not-workin. https://doi.org/10.1145/1481848.1481860
([n. d.]). [80] Sascha Fahl, Marian Harbach, Thomas Muders, Lars Baumgärtner, Bernd
[55] [n. d.]. Spring security JDK based proxy issue while using @Secured anno- Freisleben, and Matthew Smith. 2012. Why Eve and Mallory Love Android:
tation on Controller method. https://stackoverflow.com/questions/35860442/ An Analysis of Android SSL (in)Security. In Proceedings of the 2012 ACM Confer-
spring-security-jdk-based-proxy-issue-while-using-secured-annotation-on-control. ence on Computer and Communications Security (CCS ’12). ACM, New York, NY,
([n. d.]). USA, 50–61. https://doi.org/10.1145/2382196.2382205
[56] [n. d.]. Spring Security Reference. http://docs.spring.io/spring-security/site/ [81] Felix Fischer, Konstantin BÂĺottinger, Huang Xiao, Christian Stransky, Yasemin
docs/3.2.4.RELEASE/reference/htmlsingle/#jc-httpsecurity. ([n. d.]). Acar, Michael Backes, and Sascha Fahl. 2017. Stack Overflow Considered Harm-
[57] [n. d.]. Spring Security Tutorial. http://www.mkyong.com/tutorials/ ful? The Impact of Copy&Paste on Android Application Security. In 38th IEEE
spring-security-tutorials/. ([n. d.]). Symposium on Security and Privacy (S&P ’17) (2017-05-22).
[58] [n. d.]. Spring Security using JBoss <security-domain>. https://stackoverflow. [82] Cory Gackenheimer. 2013. Implementing Security and Cryptography. In Node. js
com/questions/28172056/spring-security-using-jboss-security-domain. ([n. d.]). Recipes. Springer, 133–160.
[59] [n. d.]. SSL Certificate Verification : javax.net.ssl.SSLHandshakeException. [83] Martin Georgiev, Subodh Iyengar, Suman Jana, Rishita Anubhai, Dan Boneh, and
https://stackoverflow.com/questions/25079751/ Vitaly Shmatikov. 2012. The Most Dangerous Code in the World: Validating SSL
ssl-certificate-verification-javax-net-ssl-sslhandshakeexception. ([n. d.]). Certificates in Non-browser Software. In Proceedings of the 2012 ACM Conference
[60] [n. d.]. Ssl handshake fails with unable to find valid certification path on Computer and Communications Security (CCS ’12). ACM, New York, NY, USA,
to requested target. https://stackoverflow.com/questions/40977556/ 38–49. https://doi.org/10.1145/2382196.2382204
ssl-handshake-fails-with-unable-to-find-valid-certification-path-to-requested-ta. [84] Li Gong and Gary Ellison. 2003. Inside Java(TM) 2 Platform Security: Architecture,
([n. d.]). API Design, and Implementation (2nd ed.). Pearson Education.
[61] [n. d.]. SSL Socket Connection working even though client is not [85] B. He, V. Rastogi, Y. Cao, Y. Chen, V. N. Venkatakrishnan, R. Yang, and Z. Zhang.
sending certificate? https://stackoverflow.com/questions/26761966/ 2015. Vetting SSL Usage in Applications with SSLINT. In 2015 IEEE Symposium
ssl-socket-connection-working-even-though-client-is-not-sending-certificate. on Security and Privacy. 519–534. https://doi.org/10.1109/SP.2015.38
([n. d.]). [86] David Lazar, Haogang Chen, Xi Wang, and Nickolai Zeldovich. 2014. Why Does
[62] [n. d.]. StackOverflow. https://stackoverflow.com. ([n. d.]). Cryptographic Software Fail?: A Case Study and Open Problems. In Proceedings
[63] [n. d.]. STATE OF SOFTWARE SECURITY. of 5th Asia-Pacific Workshop on Systems (APSys ’14). ACM, New York, NY, USA,
https://www.veracode.com/sites/default/files/Resources/Reports/state-of- Article 7, 7 pages. https://doi.org/10.1145/2637166.2637237
software-security-volume-7-veracode-report.pdf. ([n. d.]). [87] Yong Li, Yuanyuan Zhang, Juanru Li, and Dawu Gu. 2014. iCryptoTracer: Dynamic
[64] [n. d.]. The Webserver I talk to updated its SSL cert and now my Analysis on Misuse of Cryptography Functions in iOS Applications. Springer Interna-
app can’t talk to it. https://stackoverflow.com/questions/5758812/ tional Publishing, Cham, 349–362. https://doi.org/10.1007/978-3-319-11698-3_27
the-webserver-i-talk-to-updated-its-ssl-cert-and-now-my-app-cant-talk-to-it. [88] Fred Long. 2005. Software Vulnerabilities in Java. Technical Report CMU/SEI-2005-
([n. d.]). TN-044. Software Engineering Institute, Carnegie Mellon University, Pittsburgh,
[65] [n. d.]. Trusting all certificates using HttpClient over PA. http://resources.sei.cmu.edu/library/asset-view.cfm?AssetID=7573
HTTPS. https://stackoverflow.com/questions/2642777/ [89] Adrian Mettler, David Wagner, and Tyler Close. 2010. Joe-E: A Security-Oriented
trusting-all-certificates-using-httpclient-over-https. ([n. d.]). Subset of Java. In Network and Distributed Systems Symposium. Internet Society.
[66] [n. d.]. Use of ECC in Java SE 1.7. https://stackoverflow.com/questions/24383637/ http://www.truststc.org/pubs/652.html
use-of-ecc-in-java-se-1-7. ([n. d.]). [90] J. C. Mitchell, M. Mitchell, and U. Stern. 1997. Automated Analysis of Crypto-
[67] [n. d.]. Using public key from authorized_keys with graphic Protocols Using Mur/Spl Phi/. In Proceedings of the 1997 IEEE Symposium
Java security. https://stackoverflow.com/questions/3531506/ on Security and Privacy (SP ’97). IEEE Computer Society, Washington, DC, USA,
using-public-key-from-authorized-keys-with-java-security. ([n. d.]). 141–. http://dl.acm.org/citation.cfm?id=882493.884384
[68] [n. d.]. Web Security Samples. https://github.com/ [91] Bodo Möller, Thai Duong, and Krzysztof Kotowicz. 2014. This POODLE bites:
spring-projects/spring-security-javaconfig/blob/master/samples-web.md# exploiting the SSL 3.0 fallback. PDF online (2014), 1–4.
sample-multi-http-web-configuration. ([n. d.]). [92] Sarah Nadi, Stefan Krüger, Mira Mezini, and Eric Bodden. 2016. Jumping Through
[69] [n. d.]. WebSphere Application Server - IBM. http://www-03.ibm.com/software/ Hoops: Why Do Java Developers Struggle with Cryptography APIs?. In Proceed-
products/en/appserv-was. ([n. d.]). ings of the 38th International Conference on Software Engineering (ICSE ’16). ACM,
[70] [n. d.]. When a TrustManagerFactory is not a TrustManager- New York, NY, USA, 935–946. https://doi.org/10.1145/2884781.2884790
Factory (Java). https://stackoverflow.com/questions/14654639/ [93] Scott Oaks. 1998. Java Security. O’Reilly & Associates, Inc., Sebastopol, CA, USA.
when-a-trustmanagerfactory-is-not-a-trustmanagerfactory-java. ([n. d.]). [94] Lucky Onwuzurike and Emiliano De Cristofaro. 2015. Danger is My Middle
[71] [n. d.]. WildFly. http://wildfly.org. ([n. d.]). Name: Experimenting with SSL Vulnerabilities in Android Apps. In Proceedings
[72] [n. d.]. Wildfly 9 security domains won’t work. https://stackoverflow.com/ of the 8th ACM Conference on Security & Privacy in Wireless and Mobile Networks
questions/37425056/wildfly-9-security-domains-wont-work. ([n. d.]). (WiSec ’15). ACM, New York, NY, USA, Article 15, 6 pages. https://doi.org/10.
[73] [n. d.]. Your implementation of PreferenceActivity is vulnerable to 1145/2766498.2766522
fragment injection. https://stackoverflow.com/questions/41278592/ [95] Fahmida Y. Rashid. [n. d.]. Library misuse exposes leading Java
your-implementation-of-preferenceactivity-is-vulnerable-to-fragment-injection. platforms to attack. http://www.infoworld.com/article/3003197/security/
([n. d.]). library-misuse-exposes-leading-java-platforms-to-attack.html. ([n. d.]).
[74] Sirapat Boonkrong. 2012. Security of passwords. Information Technology Journal [96] Shao Shuai, Dong Guowei, Guo Tao, Yang Tianchang, and Shi Chenjie. 2014.
8, 2 (2012), 112–117. Modelling Analysis and Auto-detection of Cryptographic Misuse in Android
[75] Alexia Chatzikonstantinou, Christoforos Ntantogian, Georgios Karopoulos, and Applications. In Proceedings of the 2014 IEEE 12th International Conference on
Christos Xenakis. 2016. Evaluation of Cryptography Usage in Android Appli- Dependable, Autonomic and Secure Computing (DASC ’14). IEEE Computer Society,
cations. In Proceedings of the 9th EAI International Conference on Bio-inspired Washington, DC, USA, 75–80. https://doi.org/10.1109/DASC.2014.22
Information and Communications Technologies (Formerly BIONETICS) (BICT’15). [97] E. Smith and D. L. Dill. 2008. Automatic Formal Verification of Block Cipher
ICST (Institute for Computer Sciences, Social-Informatics and Telecommunica- Implementations. In 2008 Formal Methods in Computer-Aided Design. 1–7. https:
tions Engineering), ICST, Brussels, Belgium, Belgium, 83–90. https://doi.org/10. //doi.org/10.1109/FMCAD.2008.ECP.10
4108/eai.3-12-2015.2262471 [98] J Steven. 2017. Password storage cheat sheet. (2017). https://www.owasp.org/
[76] Anupam Datta, Ante Derek, John C. Mitchell, and Arnab Roy. 2007. Protocol Com- index.php/Password_Storage_Cheat_Sheet
position Logic (PCL). Electronic Notes in Theoretical Computer Science 172 (2007), [99] Marc Stevens, Elie Bursztein, Pierre Karpman, Ange Albertini, and Yarik Markov.
311 – 358. https://doi.org/10.1016/j.entcs.2007.02.012 Computation, Meaning, 2017. The first collision for full SHA-1. IACR Cryptology ePrint Archive 2017
and Logic: Articles dedicated to Gordon Plotkin. (2017), 190.
[77] Arkajit Dey and Stephen Weis. [n. d.]. Keyczar: A Cryptographic Toolkit. [100] Xiaoyun Wang, Dengguo Feng, Xuejia Lai, and Hongbo Yu. 2004. Collisions for
[78] Manuel Egele, David Brumley, Yanick Fratantonio, and Christopher Kruegel. 2013. Hash Functions MD4, MD5, HAVAL-128 and RIPEMD. IACR Cryptology ePrint
An Empirical Study of Cryptographic Misuse in Android Applications. In Pro- Archive 2004 (2004), 199.
ceedings of the 2013 ACM SIGSAC Conference on Computer &#38; Communications [101] Xin-Li Yang, David Lo, Xin Xia, Zhi-Yuan Wan, and Jian-Ling Sun. 2016. What
Security (CCS ’13). ACM, New York, NY, USA, 73–84. https://doi.org/10.1145/ Security Questions Do Developers Ask? A Large-Scale Study of Stack Overflow
2508859.2516693 Posts. Journal of Computer Science and Technology 31, 5 (01 Sep 2016), 910–924.
https://doi.org/10.1007/s11390-016-1672-0
Secure Coding Practices in Java: Challenges and Vulnerabilities Conference’17, July 2017, Washington, DC, USA

[102] William Zeller and Edward W Felten. [n. d.]. Cross-Site Request Forgeries:
Exploitation and prevention, 2008. ([n. d.]).

Potrebbero piacerti anche