Skip to content

Commit c5724a4

Browse files
authored
Merge pull request #3237 from wing328/java-security-fix
[Java] Better code injection handling for Java-related generators
2 parents cbaa577 + f88b941 commit c5724a4

File tree

101 files changed

+7115
-2212
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+7115
-2212
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/sh
2+
3+
SCRIPT="$0"
4+
5+
while [ -h "$SCRIPT" ] ; do
6+
ls=`ls -ld "$SCRIPT"`
7+
link=`expr "$ls" : '.*-> \(.*\)$'`
8+
if expr "$link" : '/.*' > /dev/null; then
9+
SCRIPT="$link"
10+
else
11+
SCRIPT=`dirname "$SCRIPT"`/"$link"
12+
fi
13+
done
14+
15+
if [ ! -d "${APP_DIR}" ]; then
16+
APP_DIR=`dirname "$SCRIPT"`/..
17+
APP_DIR=`cd "${APP_DIR}"; pwd`
18+
fi
19+
20+
executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar"
21+
22+
if [ ! -f "$executable" ]
23+
then
24+
mvn clean package
25+
fi
26+
27+
# if you've executed sbt assembly previously it will use that instead.
28+
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
29+
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"
30+
31+
rm -rf samples/client/petstore-security-test/java/okhttp-gson/src/main
32+
find samples/client/petstore-security-test/java/okhttp-gson -maxdepth 1 -type f ! -name "README.md" -exec rm {} +
33+
java $JAVA_OPTS -jar $executable $ags

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ public List<File> generate() {
188188
} else {
189189
scheme = "https";
190190
}
191+
scheme = config.escapeText(scheme);
191192
hostBuilder.append(scheme);
192193
hostBuilder.append("://");
193194
if (swagger.getHost() != null) {
@@ -198,9 +199,9 @@ public List<File> generate() {
198199
if (swagger.getBasePath() != null) {
199200
hostBuilder.append(swagger.getBasePath());
200201
}
201-
String contextPath = swagger.getBasePath() == null ? "" : swagger.getBasePath();
202-
String basePath = hostBuilder.toString();
203-
String basePathWithoutHost = swagger.getBasePath();
202+
String contextPath = config.escapeText(swagger.getBasePath() == null ? "" : swagger.getBasePath());
203+
String basePath = config.escapeText(hostBuilder.toString());
204+
String basePathWithoutHost = config.escapeText(swagger.getBasePath());
204205

205206
// resolve inline models
206207
InlineModelResolver inlineModelResolver = new InlineModelResolver();

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractJavaCodegen.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,4 +833,16 @@ public void setFullJavaUtil(boolean fullJavaUtil) {
833833
public void setDateLibrary(String library) {
834834
this.dateLibrary = library;
835835
}
836+
837+
@Override
838+
public String escapeQuotationMark(String input) {
839+
// remove " to avoid code injection
840+
return input.replace("\"", "");
841+
}
842+
843+
@Override
844+
public String escapeUnsafeCharacters(String input) {
845+
return input.replace("*/", "");
846+
}
847+
836848
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaClientCodegen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,5 @@ public Map<String, Object> postProcessModelsEnum(Map<String, Object> objs) {
211211
public void setUseRxJava(boolean useRxJava) {
212212
this.useRxJava = useRxJava;
213213
}
214+
214215
}

modules/swagger-codegen/src/main/resources/Java/libraries/feign/ApiClient.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class ApiClient {
2929
public interface Api {}
3030

3131
protected ObjectMapper objectMapper;
32-
private String basePath = "{{basePath}}";
32+
private String basePath = "{{{basePath}}}";
3333
private Map<String, RequestInterceptor> apiAuthorizations;
3434
private Feign.Builder feignBuilder;
3535

modules/swagger-codegen/src/main/resources/Java/libraries/jersey2/ApiClient.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import {{invokerPackage}}.auth.OAuth;
5151
{{>generatedAnnotation}}
5252
public class ApiClient {
5353
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
54-
private String basePath = "{{basePath}}";
54+
private String basePath = "{{{basePath}}}";
5555
private boolean debugging = false;
5656
private int connectionTimeout = 0;
5757

modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public class ApiClient {
101101
*/
102102
public static final String LENIENT_DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
103103

104-
private String basePath = "{{basePath}}";
104+
private String basePath = "{{{basePath}}}";
105105
private boolean lenientOnJson = false;
106106
private boolean debugging = false;
107107
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
@@ -169,7 +169,7 @@ public class ApiClient {
169169
/**
170170
* Set base path
171171
*
172-
* @param basePath Base path of the URL (e.g {{basePath}})
172+
* @param basePath Base path of the URL (e.g {{{basePath}}}
173173
* @return An instance of OkHttpClient
174174
*/
175175
public ApiClient setBasePath(String basePath) {

modules/swagger-codegen/src/main/resources/Java/libraries/retrofit/ApiClient.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public class ApiClient {
123123
124124
adapterBuilder = new RestAdapter
125125
.Builder()
126-
.setEndpoint("{{basePath}}")
126+
.setEndpoint("{{{basePath}}}")
127127
.setClient(new OkClient(okClient))
128128
.setConverter(new GsonConverterWrapper(gson));
129129
}
@@ -405,4 +405,4 @@ class LocalDateTypeAdapter extends TypeAdapter<LocalDate> {
405405
return formatter.parseLocalDate(date);
406406
}
407407
}
408-
}
408+
}

modules/swagger-codegen/src/main/resources/Java/libraries/retrofit2/ApiClient.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public class ApiClient {
132132

133133
okClient = new OkHttpClient();
134134

135-
String baseUrl = "{{basePath}}";
135+
String baseUrl = "{{{basePath}}}";
136136
if(!baseUrl.endsWith("/"))
137137
baseUrl = baseUrl + "/";
138138

@@ -487,4 +487,4 @@ class LocalDateTypeAdapter extends TypeAdapter<LocalDate> {
487487
}
488488
}
489489
}
490-
{{/java8}}
490+
{{/java8}}

modules/swagger-codegen/src/main/resources/Java/modelEnum.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{{description}}}{{/description}}
33
*/
4-
public enum {{#datatypeWithEnum}}{{.}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}} {
4+
public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} {
55
{{#gson}}
66
{{#allowableValues}}{{#enumVars}}
77
@SerializedName({{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}}{{{value}}}{{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}})
@@ -14,9 +14,9 @@ public enum {{#datatypeWithEnum}}{{.}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}
1414
{{/-last}}{{#-last}};{{/-last}}{{/enumVars}}{{/allowableValues}}
1515
{{/gson}}
1616

17-
private {{dataType}} value;
17+
private {{{dataType}}} value;
1818

19-
{{#datatypeWithEnum}}{{.}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}({{dataType}} value) {
19+
{{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}({{{dataType}}} value) {
2020
this.value = value;
2121
}
2222

0 commit comments

Comments
 (0)