Skip to content

Commit

Permalink
Merge pull request #3237 from wing328/java-security-fix
Browse files Browse the repository at this point in the history
[Java] Better code injection handling for Java-related generators
  • Loading branch information
wing328 committed Jun 29, 2016
2 parents cbaa577 + f88b941 commit c5724a4
Show file tree
Hide file tree
Showing 101 changed files with 7,115 additions and 2,212 deletions.
33 changes: 33 additions & 0 deletions bin/security/java-petstore-okhttp-gson.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/sh

SCRIPT="$0"

while [ -h "$SCRIPT" ] ; do
ls=`ls -ld "$SCRIPT"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
SCRIPT="$link"
else
SCRIPT=`dirname "$SCRIPT"`/"$link"
fi
done

if [ ! -d "${APP_DIR}" ]; then
APP_DIR=`dirname "$SCRIPT"`/..
APP_DIR=`cd "${APP_DIR}"; pwd`
fi

executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar"

if [ ! -f "$executable" ]
then
mvn clean package
fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="$@ generate -t modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson -i modules/swagger-codegen/src/test/resources/2_0/petstore-security-test.yaml -l java -c bin/java-petstore-okhttp-gson.json -o samples/client/petstore-security-test/java/okhttp-gson -DhideGenerationTimestamp=true"

rm -rf samples/client/petstore-security-test/java/okhttp-gson/src/main
find samples/client/petstore-security-test/java/okhttp-gson -maxdepth 1 -type f ! -name "README.md" -exec rm {} +
java $JAVA_OPTS -jar $executable $ags
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ public List<File> generate() {
} else {
scheme = "https";
}
scheme = config.escapeText(scheme);
hostBuilder.append(scheme);
hostBuilder.append("://");
if (swagger.getHost() != null) {
Expand All @@ -198,9 +199,9 @@ public List<File> generate() {
if (swagger.getBasePath() != null) {
hostBuilder.append(swagger.getBasePath());
}
String contextPath = swagger.getBasePath() == null ? "" : swagger.getBasePath();
String basePath = hostBuilder.toString();
String basePathWithoutHost = swagger.getBasePath();
String contextPath = config.escapeText(swagger.getBasePath() == null ? "" : swagger.getBasePath());
String basePath = config.escapeText(hostBuilder.toString());
String basePathWithoutHost = config.escapeText(swagger.getBasePath());

// resolve inline models
InlineModelResolver inlineModelResolver = new InlineModelResolver();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -833,4 +833,16 @@ public void setFullJavaUtil(boolean fullJavaUtil) {
public void setDateLibrary(String library) {
this.dateLibrary = library;
}

@Override
public String escapeQuotationMark(String input) {
// remove " to avoid code injection
return input.replace("\"", "");
}

@Override
public String escapeUnsafeCharacters(String input) {
return input.replace("*/", "");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,5 @@ public Map<String, Object> postProcessModelsEnum(Map<String, Object> objs) {
public void setUseRxJava(boolean useRxJava) {
this.useRxJava = useRxJava;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class ApiClient {
public interface Api {}

protected ObjectMapper objectMapper;
private String basePath = "{{basePath}}";
private String basePath = "{{{basePath}}}";
private Map<String, RequestInterceptor> apiAuthorizations;
private Feign.Builder feignBuilder;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import {{invokerPackage}}.auth.OAuth;
{{>generatedAnnotation}}
public class ApiClient {
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
private String basePath = "{{basePath}}";
private String basePath = "{{{basePath}}}";
private boolean debugging = false;
private int connectionTimeout = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public class ApiClient {
*/
public static final String LENIENT_DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";

private String basePath = "{{basePath}}";
private String basePath = "{{{basePath}}}";
private boolean lenientOnJson = false;
private boolean debugging = false;
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
Expand Down Expand Up @@ -169,7 +169,7 @@ public class ApiClient {
/**
* Set base path
*
* @param basePath Base path of the URL (e.g {{basePath}})
* @param basePath Base path of the URL (e.g {{{basePath}}}
* @return An instance of OkHttpClient
*/
public ApiClient setBasePath(String basePath) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public class ApiClient {
adapterBuilder = new RestAdapter
.Builder()
.setEndpoint("{{basePath}}")
.setEndpoint("{{{basePath}}}")
.setClient(new OkClient(okClient))
.setConverter(new GsonConverterWrapper(gson));
}
Expand Down Expand Up @@ -405,4 +405,4 @@ class LocalDateTypeAdapter extends TypeAdapter<LocalDate> {
return formatter.parseLocalDate(date);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public class ApiClient {

okClient = new OkHttpClient();

String baseUrl = "{{basePath}}";
String baseUrl = "{{{basePath}}}";
if(!baseUrl.endsWith("/"))
baseUrl = baseUrl + "/";

Expand Down Expand Up @@ -487,4 +487,4 @@ class LocalDateTypeAdapter extends TypeAdapter<LocalDate> {
}
}
}
{{/java8}}
{{/java8}}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{{description}}}{{/description}}
*/
public enum {{#datatypeWithEnum}}{{.}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}} {
public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} {
{{#gson}}
{{#allowableValues}}{{#enumVars}}
@SerializedName({{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}}{{{value}}}{{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}})
Expand All @@ -14,9 +14,9 @@ public enum {{#datatypeWithEnum}}{{.}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}
{{/-last}}{{#-last}};{{/-last}}{{/enumVars}}{{/allowableValues}}
{{/gson}}

private {{dataType}} value;
private {{{dataType}}} value;

{{#datatypeWithEnum}}{{.}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}({{dataType}} value) {
{{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}({{{dataType}}} value) {
this.value = value;
}

Expand Down
4 changes: 4 additions & 0 deletions modules/swagger-codegen/src/main/resources/Java/pojo.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {{#serializableModel}}implements Serializable{{/serializableModel}} {
{{#vars}}
{{#isEnum}}
{{^isContainer}}
{{>modelInnerEnum}}
{{/isContainer}}
{{/isEnum}}
{{#items.isEnum}}
{{#items}}
{{^isContainer}}
{{>modelInnerEnum}}
{{/isContainer}}
{{/items}}
{{/items.isEnum}}
{{#jackson}}
Expand Down
21 changes: 21 additions & 0 deletions samples/client/petstore-security-test/java/okhttp-gson/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
*.class

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear

# exclude jar for gradle wrapper
!gradle/wrapper/*.jar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# build files
**/target
target
.gradle
build
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Swagger Codegen Ignore
# Generated by swagger-codegen https://github.com/swagger-api/swagger-codegen

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell Swagger Codgen to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
29 changes: 29 additions & 0 deletions samples/client/petstore-security-test/java/okhttp-gson/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#
# Generated by: https://github.com/swagger-api/swagger-codegen.git
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
language: java
jdk:
- oraclejdk8
- oraclejdk7
before_install:
# ensure gradlew has proper permission
- chmod a+x ./gradlew
script:
# test using maven
- mvn test
# uncomment below to test using gradle
# - gradle test
# uncomment below to test using sbt
# - sbt test
Loading

0 comments on commit c5724a4

Please sign in to comment.