Skip to content

Commit 6acf45a

Browse files
beardeddragon5wing328
authored andcommitted
x-www-form-urlencoded-body-fix (#1187)
* [go] x-www-form-urlencoded-body-fix * [go] run scripts in bin and bin/security
1 parent 68a8f29 commit 6acf45a

40 files changed

+209
-164
lines changed

modules/openapi-generator/src/main/resources/go/client.mustache

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func (c *APIClient) prepareRequest(
188188
}
189189

190190
// add form parameters and file if available.
191-
if len(formParams) > 0 || (len(fileBytes) > 0 && fileName != "") {
191+
if strings.HasPrefix(headerParams["Content-Type"], "multipart/form-data") && len(formParams) > 0 || (len(fileBytes) > 0 && fileName != "") {
192192
if body != nil {
193193
return nil, errors.New("Cannot specify postBody and multipart form at the same time.")
194194
}
@@ -227,6 +227,16 @@ func (c *APIClient) prepareRequest(
227227
w.Close()
228228
}
229229

230+
if strings.HasPrefix(headerParams["Content-Type"], "application/x-www-form-urlencoded") && len(formParams) > 0 {
231+
if body != nil {
232+
return nil, errors.New("Cannot specify postBody and x-www-form-urlencoded form at the same time.")
233+
}
234+
body = &bytes.Buffer{}
235+
body.WriteString(formParams.Encode())
236+
// Set Content-Length
237+
headerParams["Content-Length"] = fmt.Sprintf("%d", body.Len())
238+
}
239+
230240
// Setup path and query parameters
231241
url, err := url.Parse(path)
232242
if err != nil {
@@ -469,4 +479,4 @@ func (e GenericOpenAPIError) Body() []byte {
469479
// Model returns the unpacked model of the error
470480
func (e GenericOpenAPIError) Model() interface{} {
471481
return e.model
472-
}
482+
}

modules/openapi-generator/src/test/resources/2_0/petstore-security-test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ paths:
3636
parameters:
3737
- name: test code inject */ ' " =end -- \r\n \n \r
3838
type: string
39-
in: formData
39+
in: formData
4040
description: To test code injection */ ' " =end -- \r\n \n \r
4141
responses:
4242
'400':
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.0.0-SNAPSHOT
1+
3.3.1-SNAPSHOT

samples/client/petstore-security-test/go/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import "./openapi"
2626

2727
## Documentation for API Endpoints
2828

29-
All URIs are relative to *petstore.swagger.io *_/ ' \" =end -- \\r\\n \\n \\r/v2 *_/ ' \" =end -- \\r\\n \\n \\r*
29+
All URIs are relative to *http://petstore.swagger.io *_/ ' \" =end -- \\r\\n \\n \\r/v2 *_/ ' \" =end -- \\r\\n \\n \\r*
3030

3131
Class | Method | HTTP request | Description
3232
------------ | ------------- | ------------- | -------------
Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,75 @@
11
openapi: 3.0.1
22
info:
3-
title: OpenAPI Petstore */ ' " =end -- \r\n \n \r
3+
contact:
4+
email: [email protected] */ ' " =end -- \r\n \n \r
45
description: "This spec is mainly for testing Petstore server and contains fake\
56
\ endpoints, models. Please do not use this for any other purpose. Special characters:\
67
\ \" \\ */ ' \" =end -- \r\n \n \r"
7-
termsOfService: http://url.to/terms/ */ ' " =end -- \r\n \n \r
8-
contact:
9-
email: [email protected] */ ' " =end -- \r\n \n \r
108
license:
119
name: Apache-2.0 */ ' " =end -- \r\n \n \r
1210
url: http://www.apache.org/licenses/LICENSE-2.0.html */ ' " =end -- \r\n \n \r
11+
termsOfService: http://url.to/terms/ */ ' " =end -- \r\n \n \r
12+
title: OpenAPI Petstore */ ' " =end -- \r\n \n \r
1313
version: 1.0.0 */ ' " =end -- \r\n \n \r
1414
externalDocs:
1515
description: Find out more about OpenAPI */ ' " =end -- \r\n \n \r
1616
url: https://openapis.org
1717
servers:
18-
- url: petstore.swagger.io */ ' " =end -- \r\n \n \r/v2 */ ' " =end -- \r\n \n \r
18+
- url: //petstore.swagger.io */ ' " =end -- \r\n \n \r/v2 */ ' " =end -- \r\n \n \r
1919
tags:
20-
- name: fake
21-
description: Everything about your Pets */ ' " =end -- \r\n \n \r
20+
- description: Everything about your Pets */ ' " =end -- \r\n \n \r
2221
externalDocs:
2322
description: Find out more */ ' " =end -- \r\n \n \r
2423
url: https://openapis.org
24+
name: fake
2525
paths:
2626
/fake:
2727
put:
28-
tags:
29-
- fake
30-
summary: To test code injection */ ' " =end -- \r\n \n \r
28+
description: To test code injection */ ' " =end -- \r\n \n \r
3129
operationId: testCodeInject */ ' " =end -- \r\n \n \r
3230
requestBody:
3331
content:
3432
application/json:
3533
schema:
3634
properties:
3735
test code inject */ ' " =end -- \r\n \n \r:
38-
type: string
3936
description: To test code injection */ ' " =end -- \r\n \n \r
37+
type: string
4038
? "*/ ' \" =end -- \r\n \n \r"
4139
: schema:
4240
properties:
4341
test code inject */ ' " =end -- \r\n \n \r:
44-
type: string
4542
description: To test code injection */ ' " =end -- \r\n \n \r
43+
type: string
4644
responses:
4745
400:
48-
description: To test code injection */ ' " =end -- \r\n \n \r
4946
content: {}
47+
description: To test code injection */ ' " =end -- \r\n \n \r
48+
summary: To test code injection */ ' " =end -- \r\n \n \r
49+
tags:
50+
- fake
5051
components:
5152
schemas:
5253
Return:
53-
type: object
54+
description: Model for testing reserved words */ ' " =end -- \r\n \n \r
5455
properties:
5556
return:
56-
type: integer
5757
description: property description */ ' " =end -- \r\n \n \r
5858
format: int32
59-
description: Model for testing reserved words */ ' " =end -- \r\n \n \r
59+
type: integer
60+
type: object
6061
xml:
6162
name: Return
6263
securitySchemes:
6364
petstore_auth:
64-
type: oauth2
6565
flows:
6666
implicit:
6767
authorizationUrl: http://petstore.swagger.io/api/oauth/dialog
6868
scopes:
6969
write:pets: modify pets in your account */ ' " =end -- \r\n \n \r
7070
read:pets: read your pets */ ' " =end -- \r\n \n \r
71+
type: oauth2
7172
api_key:
72-
type: apiKey
73-
name: api_key */ ' " =end -- \r\n \n \r
7473
in: header
74+
name: api_key */ ' " =end -- \r\n \n \r
75+
type: apiKey

samples/client/petstore-security-test/go/api_fake.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"net/http"
1717
"net/url"
1818
"strings"
19+
"github.com/antihax/optional"
1920
)
2021

2122
// Linger please
@@ -25,23 +26,25 @@ var (
2526

2627
type FakeApiService service
2728

28-
/*
29-
FakeApiService To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
29+
/*
30+
FakeApiService To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
31+
To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
3032
* @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
3133
* @param optional nil or *TestCodeInjectEndRnNROpts - Optional Parameters:
32-
* @param "UNKNOWNBASETYPE" (optional.Interface of interface{}) -
34+
* @param "UNKNOWNBASETYPE" (optional.Interface of UNKNOWN_BASE_TYPE) -
3335
*/
3436

3537
type TestCodeInjectEndRnNROpts struct {
36-
UNKNOWNBASETYPE optional.Interface
38+
UNKNOWNBASETYPE optional.Interface
3739
}
3840

3941
func (a *FakeApiService) TestCodeInjectEndRnNR(ctx context.Context, localVarOptionals *TestCodeInjectEndRnNROpts) (*http.Response, error) {
4042
var (
41-
localVarHttpMethod = strings.ToUpper("Put")
42-
localVarPostBody interface{}
43-
localVarFileName string
44-
localVarFileBytes []byte
43+
localVarHttpMethod = strings.ToUpper("Put")
44+
localVarPostBody interface{}
45+
localVarFormFileName string
46+
localVarFileName string
47+
localVarFileBytes []byte
4548
)
4649

4750
// create path and map variables
@@ -70,14 +73,14 @@ func (a *FakeApiService) TestCodeInjectEndRnNR(ctx context.Context, localVarOpti
7073
}
7174
// body params
7275
if localVarOptionals != nil && localVarOptionals.UNKNOWNBASETYPE.IsSet() {
73-
localVarOptionalUNKNOWNBASETYPE, localVarOptionalUNKNOWNBASETYPEok := localVarOptionals.UNKNOWNBASETYPE.Value().(interface{})
76+
localVarOptionalUNKNOWNBASETYPE, localVarOptionalUNKNOWNBASETYPEok := localVarOptionals.UNKNOWNBASETYPE.Value().(UNKNOWN_BASE_TYPE)
7477
if !localVarOptionalUNKNOWNBASETYPEok {
75-
return nil, reportError("uNKNOWNBASETYPE should be interface{}")
78+
return nil, reportError("uNKNOWNBASETYPE should be UNKNOWN_BASE_TYPE")
7679
}
7780
localVarPostBody = &localVarOptionalUNKNOWNBASETYPE
7881
}
7982

80-
r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)
83+
r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFormFileName, localVarFileName, localVarFileBytes)
8184
if err != nil {
8285
return nil, err
8386
}
@@ -95,7 +98,7 @@ func (a *FakeApiService) TestCodeInjectEndRnNR(ctx context.Context, localVarOpti
9598

9699
if localVarHttpResponse.StatusCode >= 300 {
97100
newErr := GenericOpenAPIError{
98-
body: localVarBody,
101+
body: localVarBody,
99102
error: localVarHttpResponse.Status,
100103
}
101104
return localVarHttpResponse, newErr

samples/client/petstore-security-test/go/client.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ func parameterToString(obj interface{}, collectionFormat string) string {
139139

140140
if reflect.TypeOf(obj).Kind() == reflect.Slice {
141141
return strings.Trim(strings.Replace(fmt.Sprint(obj), " ", delimiter, -1), "[]")
142+
} else if t, ok := obj.(time.Time); ok {
143+
return t.Format(time.RFC3339)
142144
}
143145

144146
return fmt.Sprintf("%v", obj)
@@ -162,6 +164,7 @@ func (c *APIClient) prepareRequest(
162164
headerParams map[string]string,
163165
queryParams url.Values,
164166
formParams url.Values,
167+
formFileName string,
165168
fileName string,
166169
fileBytes []byte) (localVarRequest *http.Request, err error) {
167170

@@ -182,7 +185,7 @@ func (c *APIClient) prepareRequest(
182185
}
183186

184187
// add form parameters and file if available.
185-
if len(formParams) > 0 || (len(fileBytes) > 0 && fileName != "") {
188+
if strings.HasPrefix(headerParams["Content-Type"], "multipart/form-data") && len(formParams) > 0 || (len(fileBytes) > 0 && fileName != "") {
186189
if body != nil {
187190
return nil, errors.New("Cannot specify postBody and multipart form at the same time.")
188191
}
@@ -204,7 +207,7 @@ func (c *APIClient) prepareRequest(
204207
if len(fileBytes) > 0 && fileName != "" {
205208
w.Boundary()
206209
//_, fileNm := filepath.Split(fileName)
207-
part, err := w.CreateFormFile("file", filepath.Base(fileName))
210+
part, err := w.CreateFormFile(formFileName, filepath.Base(fileName))
208211
if err != nil {
209212
return nil, err
210213
}
@@ -221,6 +224,16 @@ func (c *APIClient) prepareRequest(
221224
w.Close()
222225
}
223226

227+
if strings.HasPrefix(headerParams["Content-Type"], "application/x-www-form-urlencoded") && len(formParams) > 0 {
228+
if body != nil {
229+
return nil, errors.New("Cannot specify postBody and x-www-form-urlencoded form at the same time.")
230+
}
231+
body = &bytes.Buffer{}
232+
body.WriteString(formParams.Encode())
233+
// Set Content-Length
234+
headerParams["Content-Length"] = fmt.Sprintf("%d", body.Len())
235+
}
236+
224237
// Setup path and query parameters
225238
url, err := url.Parse(path)
226239
if err != nil {
@@ -424,8 +437,9 @@ func CacheExpires(r *http.Response) time.Time {
424437
lifetime, err := time.ParseDuration(maxAge + "s")
425438
if err != nil {
426439
expires = now
440+
} else {
441+
expires = now.Add(lifetime)
427442
}
428-
expires = now.Add(lifetime)
429443
} else {
430444
expiresHeader := r.Header.Get("Expires")
431445
if expiresHeader != "" {
@@ -462,4 +476,4 @@ func (e GenericOpenAPIError) Body() []byte {
462476
// Model returns the unpacked model of the error
463477
func (e GenericOpenAPIError) Model() interface{} {
464478
return e.model
465-
}
479+
}

samples/client/petstore-security-test/go/configuration.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (c contextKey) String() string {
2525
}
2626

2727
var (
28-
// ContextOAuth2 takes a oauth2.TokenSource as authentication for the request.
28+
// ContextOAuth2 takes an oauth2.TokenSource as authentication for the request.
2929
ContextOAuth2 = contextKey("token")
3030

3131
// ContextBasicAuth takes BasicAuth as authentication for the request.
@@ -61,7 +61,7 @@ type Configuration struct {
6161

6262
func NewConfiguration() *Configuration {
6363
cfg := &Configuration{
64-
BasePath: "petstore.swagger.io *_/ ' \" =end -- \\r\\n \\n \\r/v2 *_/ ' \" =end -- \\r\\n \\n \\r",
64+
BasePath: "http://petstore.swagger.io *_/ ' \" =end -- \\r\\n \\n \\r/v2 *_/ ' \" =end -- \\r\\n \\n \\r",
6565
DefaultHeader: make(map[string]string),
6666
UserAgent: "OpenAPI-Generator/1.0.0/go",
6767
}

samples/client/petstore-security-test/go/docs/FakeApi.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# \FakeApi
22

3-
All URIs are relative to *petstore.swagger.io *_/ ' \" =end -- \\r\\n \\n \\r/v2 *_/ ' \" =end -- \\r\\n \\n \\r*
3+
All URIs are relative to *http://petstore.swagger.io *_/ ' \" =end -- \\r\\n \\n \\r/v2 *_/ ' \" =end -- \\r\\n \\n \\r*
44

55
Method | HTTP request | Description
66
------------- | ------------- | -------------
@@ -11,6 +11,8 @@ Method | HTTP request | Description
1111
> TestCodeInjectEndRnNR(ctx, optional)
1212
To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
1313

14+
To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
15+
1416
### Required Parameters
1517

1618
Name | Type | Description | Notes
@@ -23,7 +25,7 @@ Optional parameters are passed through a pointer to a TestCodeInjectEndRnNROpts
2325

2426
Name | Type | Description | Notes
2527
------------- | ------------- | ------------- | -------------
26-
**uNKNOWNBASETYPE** | [**optional.Interface of interface{}**](UNKNOWN_BASE_TYPE.md)| |
28+
**uNKNOWNBASETYPE** | [**optional.Interface of UNKNOWN_BASE_TYPE**](UNKNOWN_BASE_TYPE.md)| |
2729

2830
### Return type
2931

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.3.0-SNAPSHOT
1+
3.3.1-SNAPSHOT

0 commit comments

Comments
 (0)