Skip to content

Commit 9ee10e2

Browse files
authored
Merge pull request #3249 from wing328/csharp_security_fix
[C#] better code injection handling for C# API client
2 parents cb53ea1 + 7951c06 commit 9ee10e2

35 files changed

+2766
-11
lines changed

bin/security/csharp-petstore.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 -i modules/swagger-codegen/src/test/resources/2_0/petstore-security-test.yaml -l csharp -o samples/client/petstore-security-test/csharp/SwaggerClient"
30+
31+
java $JAVA_OPTS -jar $executable $ags

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,4 +656,16 @@ public String toEnumName(CodegenProperty property) {
656656
public String testPackageName() {
657657
return this.packageName + ".Test";
658658
}
659+
660+
@Override
661+
public String escapeQuotationMark(String input) {
662+
// remove " to avoid code injection
663+
return input.replace("\"", "");
664+
}
665+
666+
@Override
667+
public String escapeUnsafeCharacters(String input) {
668+
return input.replace("*/", "");
669+
}
670+
659671
}

modules/swagger-codegen/src/main/resources/aspnet5/controller.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace {{packageName}}.Controllers
1616
/// <summary>
1717
/// {{description}}
1818
/// </summary>{{#description}}{{#basePath}}
19-
[Route("{{basePath}}")]
19+
[Route("{{{basePath}}}")]
2020
{{/basePath}}[Description("{{description}}")]{{/description}}
2121
public class {{classname}}Controller : Controller
2222
{ {{#operation}}

modules/swagger-codegen/src/main/resources/csharp/ApiClient.mustache

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@ namespace {{packageName}}.Client
4141

4242
/// <summary>
4343
/// Initializes a new instance of the <see cref="ApiClient" /> class
44-
/// with default configuration and base path ({{basePath}}).
44+
/// with default configuration and base path ({{{basePath}}}).
4545
/// </summary>
4646
public ApiClient()
4747
{
4848
Configuration = Configuration.Default;
49-
RestClient = new RestClient("{{basePath}}");
49+
RestClient = new RestClient("{{{basePath}}}");
5050
}
5151

5252
/// <summary>
5353
/// Initializes a new instance of the <see cref="ApiClient" /> class
54-
/// with default base path ({{basePath}}).
54+
/// with default base path ({{{basePath}}}).
5555
/// </summary>
5656
/// <param name="config">An instance of Configuration.</param>
5757
public ApiClient(Configuration config = null)
@@ -61,15 +61,15 @@ namespace {{packageName}}.Client
6161
else
6262
Configuration = config;
6363
64-
RestClient = new RestClient("{{basePath}}");
64+
RestClient = new RestClient("{{{basePath}}}");
6565
}
6666

6767
/// <summary>
6868
/// Initializes a new instance of the <see cref="ApiClient" /> class
6969
/// with default configuration.
7070
/// </summary>
7171
/// <param name="basePath">The base path.</param>
72-
public ApiClient(String basePath = "{{basePath}}")
72+
public ApiClient(String basePath = "{{{basePath}}}")
7373
{
7474
if (String.IsNullOrEmpty(basePath))
7575
throw new ArgumentException("basePath cannot be empty");

modules/swagger-codegen/src/main/resources/csharp/Configuration.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,16 +281,16 @@ namespace {{packageName}}.Client
281281
/// </summary>
282282
public static String ToDebugReport()
283283
{
284-
String report = "C# SDK ({{packageName}}) Debug Report:\n";
284+
String report = "C# SDK ({{{packageName}}}) Debug Report:\n";
285285
{{^supportsUWP}}
286286
report += " OS: " + Environment.OSVersion + "\n";
287287
report += " .NET Framework Version: " + Assembly
288288
.GetExecutingAssembly()
289289
.GetReferencedAssemblies()
290290
.Where(x => x.Name == "System.Core").First().Version.ToString() + "\n";
291291
{{/supportsUWP}}
292-
report += " Version of the API: {{version}}\n";
293-
report += " SDK Package Version: {{packageVersion}}\n";
292+
report += " Version of the API: {{{version}}}\n";
293+
report += " SDK Package Version: {{{packageVersion}}}\n";
294294

295295
return report;
296296
}

modules/swagger-codegen/src/main/resources/csharp/README.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ namespace Example
107107

108108
## Documentation for API Endpoints
109109

110-
All URIs are relative to *{{basePath}}*
110+
All URIs are relative to *{{{basePath}}}*
111111

112112
Class | Method | HTTP request | Description
113113
------------ | ------------- | ------------- | -------------

modules/swagger-codegen/src/main/resources/csharp/api_doc.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# {{packageName}}.Api.{{classname}}{{#description}}
22
{{description}}{{/description}}
33

4-
All URIs are relative to *{{basePath}}*
4+
All URIs are relative to *{{{basePath}}}*
55

66
Method | HTTP request | Description
77
------------- | ------------- | -------------
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# Ref: https://gist.github.com/kmorcinek/2710267
2+
# Download this file using PowerShell v3 under Windows with the following comand
3+
# Invoke-WebRequest https://gist.githubusercontent.com/kmorcinek/2710267/raw/ -OutFile .gitignore
4+
5+
# User-specific files
6+
*.suo
7+
*.user
8+
*.sln.docstates
9+
10+
# Build results
11+
12+
[Dd]ebug/
13+
[Rr]elease/
14+
x64/
15+
build/
16+
[Bb]in/
17+
[Oo]bj/
18+
19+
# NuGet Packages
20+
*.nupkg
21+
# The packages folder can be ignored because of Package Restore
22+
**/packages/*
23+
# except build/, which is used as an MSBuild target.
24+
!**/packages/build/
25+
# Uncomment if necessary however generally it will be regenerated when needed
26+
#!**/packages/repositories.config
27+
28+
# MSTest test Results
29+
[Tt]est[Rr]esult*/
30+
[Bb]uild[Ll]og.*
31+
32+
*_i.c
33+
*_p.c
34+
*.ilk
35+
*.meta
36+
*.obj
37+
*.pch
38+
*.pdb
39+
*.pgc
40+
*.pgd
41+
*.rsp
42+
*.sbr
43+
*.tlb
44+
*.tli
45+
*.tlh
46+
*.tmp
47+
*.tmp_proj
48+
*.log
49+
*.vspscc
50+
*.vssscc
51+
.builds
52+
*.pidb
53+
*.log
54+
*.scc
55+
56+
# OS generated files #
57+
.DS_Store*
58+
ehthumbs.db
59+
Icon?
60+
Thumbs.db
61+
62+
# Visual C++ cache files
63+
ipch/
64+
*.aps
65+
*.ncb
66+
*.opensdf
67+
*.sdf
68+
*.cachefile
69+
70+
# Visual Studio profiler
71+
*.psess
72+
*.vsp
73+
*.vspx
74+
75+
# Guidance Automation Toolkit
76+
*.gpState
77+
78+
# ReSharper is a .NET coding add-in
79+
_ReSharper*/
80+
*.[Rr]e[Ss]harper
81+
82+
# TeamCity is a build add-in
83+
_TeamCity*
84+
85+
# DotCover is a Code Coverage Tool
86+
*.dotCover
87+
88+
# NCrunch
89+
*.ncrunch*
90+
.*crunch*.local.xml
91+
92+
# Installshield output folder
93+
[Ee]xpress/
94+
95+
# DocProject is a documentation generator add-in
96+
DocProject/buildhelp/
97+
DocProject/Help/*.HxT
98+
DocProject/Help/*.HxC
99+
DocProject/Help/*.hhc
100+
DocProject/Help/*.hhk
101+
DocProject/Help/*.hhp
102+
DocProject/Help/Html2
103+
DocProject/Help/html
104+
105+
# Click-Once directory
106+
publish/
107+
108+
# Publish Web Output
109+
*.Publish.xml
110+
111+
# Windows Azure Build Output
112+
csx
113+
*.build.csdef
114+
115+
# Windows Store app package directory
116+
AppPackages/
117+
118+
# Others
119+
sql/
120+
*.Cache
121+
ClientBin/
122+
[Ss]tyle[Cc]op.*
123+
~$*
124+
*~
125+
*.dbmdl
126+
*.[Pp]ublish.xml
127+
*.pfx
128+
*.publishsettings
129+
modulesbin/
130+
tempbin/
131+
132+
# EPiServer Site file (VPP)
133+
AppData/
134+
135+
# RIA/Silverlight projects
136+
Generated_Code/
137+
138+
# Backup & report files from converting an old project file to a newer
139+
# Visual Studio version. Backup files are not needed, because we have git ;-)
140+
_UpgradeReport_Files/
141+
Backup*/
142+
UpgradeLog*.XML
143+
UpgradeLog*.htm
144+
145+
# vim
146+
*.txt~
147+
*.swp
148+
*.swo
149+
150+
# svn
151+
.svn
152+
153+
# SQL Server files
154+
**/App_Data/*.mdf
155+
**/App_Data/*.ldf
156+
**/App_Data/*.sdf
157+
158+
159+
#LightSwitch generated files
160+
GeneratedArtifacts/
161+
_Pvt_Extensions/
162+
ModelManifest.xml
163+
164+
# =========================
165+
# Windows detritus
166+
# =========================
167+
168+
# Windows image file caches
169+
Thumbs.db
170+
ehthumbs.db
171+
172+
# Folder config file
173+
Desktop.ini
174+
175+
# Recycle Bin used on file shares
176+
$RECYCLE.BIN/
177+
178+
# Mac desktop service store files
179+
.DS_Store
180+
181+
# SASS Compiler cache
182+
.sass-cache
183+
184+
# Visual Studio 2014 CTP
185+
**/*.sln.ide
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Swagger Codegen Ignore
2+
# Generated by swagger-codegen https://github.com/swagger-api/swagger-codegen
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell Swagger Codgen to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#
2+
# Generated by: https://github.com/swagger-api/swagger-codegen.git
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
language: csharp
17+
mono:
18+
- latest
19+
solution: IO.Swagger.sln
20+
script:
21+
- /bin/sh ./mono_nunit_test.sh

0 commit comments

Comments
 (0)