SlideShare a Scribd company logo
Secure code with 3rd Party Library
● Avoid rolling your own cryptographic code (read - this to know why)
● Don’t reinvent the wheel! - Always follow DRY, KISS approach
● Less is better - Use of tried-and-tested 3rd party libraries means you will have
less things to worry; your code will have less number of bugs.
Also read the secure code guild from Oracle:
http://www.oracle.com/technetwork/java/seccodeguide-139067.html
Find the commons mistakes developers make
http://find-sec-bugs.github.io/bugs.htm
Secure code with 3rd Party Library
Some very common 3rd party libraries -
● Apache commons Lang and IO
● Google Guava to compliment Java Collections API
● Joda Datetime Library (for Java Version <= 7)
● And many more
Some sample code snippets from our repository where we could have used 3rd
library methods -
commons.lang.StringEscapeUtils
Before:
After:
StringEscapeUtils.escapeXml(value);
StringBuilder result = new StringBuilder(value.length());
for (int i = 0; i < value.length(); ++i) {
switch (value.charAt(i)) {
case '<':
result.append("&lt;");
break;
case '>':
result.append("&gt;");
break;
case '"':
result.append("&quot;");
break;
default:
result.append(value.charAt(i));
break;
}
}
return result.toString();
Also hundreds of practical uses of String manipulation (join, replace,
conversion, etc) from:
http://commons.apache.org/proper/commons-lang/javadocs/api-
3.1/org/apache/commons/lang3/StringUtils.html
http://docs.spring.io/spring/docs/current/javadoc-
api/org/springframework/util/StringUtils.html
org.apache.commons.io.IOUtils (similar FileUtils)
Before :
After:
IOUtils.copy(new FileReader(indexFile), sw);
StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter(sw);
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(indexFile));
String line = in.readLine();
while (line != null) {
out.println(line);
line = in.readLine();
}
} finally {
if (in != null) {
try {
in.close();
} catch (Exception t) {
log.warn("", t);
} finally {
in = null;
}
}
out.close();
}
More from:
https://commons.apache.org/proper/commons-io/bestpractices.html
After:
Date firstDayOfMonth = new DateTime().withTimeAtStartOfDay()
.dayOfMonth().withMinimumValue()
.toDate();
Date lastDayOfMonth = new DateTime().withTimeAtStartOfDay()
.dayOfMonth().withMaximumValue()
.toDate();
org.joda.DateTime (or Java 8 Date API)
Before:
Calendar fromCal = Calendar.getInstance();
fromCal.set(Calendar.DAY_OF_MONTH, 1);
if (spec.getMonth() > 0) {
fromCal.set(Calendar.MONTH, spec.getMonth() - 1);
}
if (spec.getYear() > 0) {
fromCal.set(Calendar.YEAR, spec.getYear());
}
fromCal.set(Calendar.HOUR_OF_DAY, 0);
fromCal.set(Calendar.MINUTE, 0);
fromCal.set(Calendar.SECOND, 0);
fromCal.set(Calendar.MILLISECOND, 0);
Calendar toCal = Calendar.getInstance();
if (spec.getMonth() > 0) {
toCal.set(Calendar.MONTH, spec.getMonth() - 1);
}
toCal.set(Calendar.DAY_OF_MONTH,
toCal.getActualMaximum(Calendar.DAY_OF_MONTH));
if (spec.getYear() > 0) {
toCal.set(Calendar.YEAR, spec.getYear());
}
toCal.set(Calendar.HOUR_OF_DAY, 0);
toCal.set(Calendar.MINUTE, 0);
toCal.set(Calendar.SECOND, 0);
toCal.set(Calendar.MILLISECOND, 0);
More from:
http://stackoverflow.com/questions/589870/should-i-use-java-
date-and-time-classes-or-go-with-a-3rd-party-library-like-joda
After:
filterMap = Splitter.on(",").withKeyValueSeparator("=")
.split(Globals.getProperty(commaSepKeyVals));
Google Guava: com.google.common.base.Splitter
Before:
Sample value prop1=value1,prop2=value2,prop3=value3
HashSet set = new HashSet();
String property = Globals.getProperty(commaSepKeyVals);
if(property != null && property.length() > 0) {
Vector v = RegexUtil.split("/,/", property);
set.addAll(v);
}
Iterator<String> iter = set.iterator();
while (iter.hasNext()) {
String paramFilterKeyVal = iter.next();
String[] keyValue = paramFilterKeyVal.split("=");
if (keyValue.length == 2) {
filterMap.put(keyValue[0], keyValue[1]);
}
}
More from:
http://stackoverflow.com/questions/3759440/the-guava-library-for-java-what-
are-its-most-useful-and-or-hidden-features
Thanks
Naimul Huda
mdnhuda@gmail.com

More Related Content

What's hot (20)

PPTX
Why learn Internals?
Shaul Rosenzwieg
 
PDF
Rust All Hands Winter 2011
Patrick Walton
 
PDF
Node intro
cloudhead
 
ODP
Virtual domains
Luca Pescatore
 
PDF
Spark Day 2017- Spark 의 과거, 현재, 미래
Moon Soo Lee
 
ODP
Matthew Vignau: Memory Management in SharePoint 2007 Development
SharePoint Saturday NY
 
PDF
#win8acad : Building Metro Style Apps with XAML for .NET Developers
Frederik De Bruyne
 
PPTX
Logging, Serilog, Structured Logging, Seq
Doruk Uluçay
 
PPTX
Calling python from r
Barry DeCicco
 
TXT
Codigo java
weberson2012
 
PDF
Configuring Syslog by Octavio
Rowell Dionicio
 
ZIP
Workshop@naha val3
Shusaku Fukumine
 
PDF
Macros in nemerle
Kota Mizushima
 
ODP
Kick-off Project 2: Presentatie Linux
Patrick Koning
 
PDF
Restinio (actual aug 2018)
Nicolai Grodzitski
 
PDF
Logstash: Get to know your logs
SmartLogic
 
PDF
Sphinx && Perl Houston Perl Mongers - May 8th, 2014
Brett Estrade
 
PDF
Centralized Logging with syslog
amiable_indian
 
PDF
Pharo Hands-On: 02 syntax
Pharo
 
PDF
Scheming Defaults
Ashton Williams
 
Why learn Internals?
Shaul Rosenzwieg
 
Rust All Hands Winter 2011
Patrick Walton
 
Node intro
cloudhead
 
Virtual domains
Luca Pescatore
 
Spark Day 2017- Spark 의 과거, 현재, 미래
Moon Soo Lee
 
Matthew Vignau: Memory Management in SharePoint 2007 Development
SharePoint Saturday NY
 
#win8acad : Building Metro Style Apps with XAML for .NET Developers
Frederik De Bruyne
 
Logging, Serilog, Structured Logging, Seq
Doruk Uluçay
 
Calling python from r
Barry DeCicco
 
Codigo java
weberson2012
 
Configuring Syslog by Octavio
Rowell Dionicio
 
Workshop@naha val3
Shusaku Fukumine
 
Macros in nemerle
Kota Mizushima
 
Kick-off Project 2: Presentatie Linux
Patrick Koning
 
Restinio (actual aug 2018)
Nicolai Grodzitski
 
Logstash: Get to know your logs
SmartLogic
 
Sphinx && Perl Houston Perl Mongers - May 8th, 2014
Brett Estrade
 
Centralized Logging with syslog
amiable_indian
 
Pharo Hands-On: 02 syntax
Pharo
 
Scheming Defaults
Ashton Williams
 

Viewers also liked (18)

PDF
Story_2_Kosciuszko_Bridge
Theresa Casey, FSMPS, CPSM
 
PPT
CADA UM NA SUA
CLAUCRUZ
 
PDF
Teksec Velogate
Stefan Haynes
 
PPTX
Trabajo sebastian gonzalez 2c
seba123
 
PDF
RESUME_December2014
Nick Brower
 
PDF
CustomCertificatelevel2
Vinnie Lester
 
PDF
Ch fr comb_seniors_2014
emiliomerayo
 
PDF
Calendario2012 2013-1
Milton Pabel Cazas Gonzales
 
PPT
Comunidades Virtuales
keiner monroy
 
PDF
Combat Lifesavers Course - Advanced lifesaving techniques for c
Marcus Walters
 
PDF
kevins-kudos-page
Kevin Kinney
 
PDF
Webflyer poster roadshow entreprise blue
Antoine GRATIAN
 
PDF
Folha Dominical - 26.09.11 Nº 391
Comunidades Vivas
 
PDF
حربي محمد..تطور الحركة الوطنية في ايران من سنة 1890 حتى سنة 1953
Mountasser Choukri
 
PDF
Calendario escolar cra 16 17
crasadaba
 
PDF
Projeto mural escolar estágios do desenvolvimento humano
Maike Zaniolo
 
Story_2_Kosciuszko_Bridge
Theresa Casey, FSMPS, CPSM
 
CADA UM NA SUA
CLAUCRUZ
 
Teksec Velogate
Stefan Haynes
 
Trabajo sebastian gonzalez 2c
seba123
 
RESUME_December2014
Nick Brower
 
CustomCertificatelevel2
Vinnie Lester
 
Ch fr comb_seniors_2014
emiliomerayo
 
Calendario2012 2013-1
Milton Pabel Cazas Gonzales
 
Comunidades Virtuales
keiner monroy
 
Combat Lifesavers Course - Advanced lifesaving techniques for c
Marcus Walters
 
kevins-kudos-page
Kevin Kinney
 
Webflyer poster roadshow entreprise blue
Antoine GRATIAN
 
Folha Dominical - 26.09.11 Nº 391
Comunidades Vivas
 
حربي محمد..تطور الحركة الوطنية في ايران من سنة 1890 حتى سنة 1953
Mountasser Choukri
 
Calendario escolar cra 16 17
crasadaba
 
Projeto mural escolar estágios do desenvolvimento humano
Maike Zaniolo
 
Ad

Similar to Secure code 3rd_party_libs (20)

PPS
Packages and inbuilt classes of java
kamal kotecha
 
PPT
Apache Utilities At Work V5
Tom Marrs
 
PDF
JAVA 4.pdfdhfvksfvhsjfbjhdjhbjshjshjvcjdbh
KusumitaSahoo1
 
PDF
55j7
swein2
 
PPT
55 New Features in Java 7
Boulder Java User's Group
 
PDF
55 new things in Java 7 - Devoxx France
David Delabassee
 
PPT
Java 7
Bipul Sinha
 
PPTX
Java Tips, Tricks and Pitfalls
Maksym Chuhaievskyi
 
DOCX
Java programs
jojeph
 
PDF
Pebank java handsout
PE-BANK
 
DOCX
(674335607) cs2309 java-lab-manual
Chandrapriya Jayabal
 
PDF
Java I/O Part 1
AshishSingh Bhatia
 
PDF
I/O in java Part 1
ashishspace
 
PPTX
Java best practices
Ray Toal
 
PPT
Jug java7
Dmitry Buzdin
 
PDF
What`s new in Java 7
Georgian Micsa
 
PPT
file handling in object oriented programming through java
Parameshwar Maddela
 
PDF
Mobile Software Engineering Crash Course - C02 Java Primer
Mohammad Shaker
 
PPTX
Use of Apache Commons and Utilities
Pramod Kumar
 
Packages and inbuilt classes of java
kamal kotecha
 
Apache Utilities At Work V5
Tom Marrs
 
JAVA 4.pdfdhfvksfvhsjfbjhdjhbjshjshjvcjdbh
KusumitaSahoo1
 
55j7
swein2
 
55 New Features in Java 7
Boulder Java User's Group
 
55 new things in Java 7 - Devoxx France
David Delabassee
 
Java 7
Bipul Sinha
 
Java Tips, Tricks and Pitfalls
Maksym Chuhaievskyi
 
Java programs
jojeph
 
Pebank java handsout
PE-BANK
 
(674335607) cs2309 java-lab-manual
Chandrapriya Jayabal
 
Java I/O Part 1
AshishSingh Bhatia
 
I/O in java Part 1
ashishspace
 
Java best practices
Ray Toal
 
Jug java7
Dmitry Buzdin
 
What`s new in Java 7
Georgian Micsa
 
file handling in object oriented programming through java
Parameshwar Maddela
 
Mobile Software Engineering Crash Course - C02 Java Primer
Mohammad Shaker
 
Use of Apache Commons and Utilities
Pramod Kumar
 
Ad

Recently uploaded (20)

PPTX
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PDF
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PDF
Add Background Images to Charts in IBM SPSS Statistics Version 31.pdf
Version 1 Analytics
 
PDF
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PPTX
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PDF
Adobe Premiere Pro Crack / Full Version / Free Download
hashhshs786
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Add Background Images to Charts in IBM SPSS Statistics Version 31.pdf
Version 1 Analytics
 
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
Adobe Premiere Pro Crack / Full Version / Free Download
hashhshs786
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 

Secure code 3rd_party_libs

  • 1. Secure code with 3rd Party Library ● Avoid rolling your own cryptographic code (read - this to know why) ● Don’t reinvent the wheel! - Always follow DRY, KISS approach ● Less is better - Use of tried-and-tested 3rd party libraries means you will have less things to worry; your code will have less number of bugs. Also read the secure code guild from Oracle: http://www.oracle.com/technetwork/java/seccodeguide-139067.html Find the commons mistakes developers make http://find-sec-bugs.github.io/bugs.htm
  • 2. Secure code with 3rd Party Library Some very common 3rd party libraries - ● Apache commons Lang and IO ● Google Guava to compliment Java Collections API ● Joda Datetime Library (for Java Version <= 7) ● And many more Some sample code snippets from our repository where we could have used 3rd library methods -
  • 3. commons.lang.StringEscapeUtils Before: After: StringEscapeUtils.escapeXml(value); StringBuilder result = new StringBuilder(value.length()); for (int i = 0; i < value.length(); ++i) { switch (value.charAt(i)) { case '<': result.append("&lt;"); break; case '>': result.append("&gt;"); break; case '"': result.append("&quot;"); break; default: result.append(value.charAt(i)); break; } } return result.toString(); Also hundreds of practical uses of String manipulation (join, replace, conversion, etc) from: http://commons.apache.org/proper/commons-lang/javadocs/api- 3.1/org/apache/commons/lang3/StringUtils.html http://docs.spring.io/spring/docs/current/javadoc- api/org/springframework/util/StringUtils.html
  • 4. org.apache.commons.io.IOUtils (similar FileUtils) Before : After: IOUtils.copy(new FileReader(indexFile), sw); StringWriter sw = new StringWriter(); PrintWriter out = new PrintWriter(sw); BufferedReader in = null; try { in = new BufferedReader(new FileReader(indexFile)); String line = in.readLine(); while (line != null) { out.println(line); line = in.readLine(); } } finally { if (in != null) { try { in.close(); } catch (Exception t) { log.warn("", t); } finally { in = null; } } out.close(); } More from: https://commons.apache.org/proper/commons-io/bestpractices.html
  • 5. After: Date firstDayOfMonth = new DateTime().withTimeAtStartOfDay() .dayOfMonth().withMinimumValue() .toDate(); Date lastDayOfMonth = new DateTime().withTimeAtStartOfDay() .dayOfMonth().withMaximumValue() .toDate(); org.joda.DateTime (or Java 8 Date API) Before: Calendar fromCal = Calendar.getInstance(); fromCal.set(Calendar.DAY_OF_MONTH, 1); if (spec.getMonth() > 0) { fromCal.set(Calendar.MONTH, spec.getMonth() - 1); } if (spec.getYear() > 0) { fromCal.set(Calendar.YEAR, spec.getYear()); } fromCal.set(Calendar.HOUR_OF_DAY, 0); fromCal.set(Calendar.MINUTE, 0); fromCal.set(Calendar.SECOND, 0); fromCal.set(Calendar.MILLISECOND, 0); Calendar toCal = Calendar.getInstance(); if (spec.getMonth() > 0) { toCal.set(Calendar.MONTH, spec.getMonth() - 1); } toCal.set(Calendar.DAY_OF_MONTH, toCal.getActualMaximum(Calendar.DAY_OF_MONTH)); if (spec.getYear() > 0) { toCal.set(Calendar.YEAR, spec.getYear()); } toCal.set(Calendar.HOUR_OF_DAY, 0); toCal.set(Calendar.MINUTE, 0); toCal.set(Calendar.SECOND, 0); toCal.set(Calendar.MILLISECOND, 0); More from: http://stackoverflow.com/questions/589870/should-i-use-java- date-and-time-classes-or-go-with-a-3rd-party-library-like-joda
  • 6. After: filterMap = Splitter.on(",").withKeyValueSeparator("=") .split(Globals.getProperty(commaSepKeyVals)); Google Guava: com.google.common.base.Splitter Before: Sample value prop1=value1,prop2=value2,prop3=value3 HashSet set = new HashSet(); String property = Globals.getProperty(commaSepKeyVals); if(property != null && property.length() > 0) { Vector v = RegexUtil.split("/,/", property); set.addAll(v); } Iterator<String> iter = set.iterator(); while (iter.hasNext()) { String paramFilterKeyVal = iter.next(); String[] keyValue = paramFilterKeyVal.split("="); if (keyValue.length == 2) { filterMap.put(keyValue[0], keyValue[1]); } } More from: http://stackoverflow.com/questions/3759440/the-guava-library-for-java-what- are-its-most-useful-and-or-hidden-features