fix: improve detection accuracy on no-server-example.com rule (#1270)

This commit is contained in:
Adam Altman
2023-09-22 08:05:03 -05:00
committed by GitHub
parent cfe66a79a9
commit eb82725463
34 changed files with 82 additions and 40 deletions

View File

@@ -0,0 +1,6 @@
---
'@redocly/openapi-core': patch
'@redocly/cli': patch
---
Fixed pattern for no-server-example.com rule to improve detection accuracy.

View File

@@ -1,6 +1,6 @@
openapi: 3.1.0 openapi: 3.1.0
servers: servers:
- url: http://localhost - url: http://redocly-example.com
info: info:
title: Sample API title: Sample API
version: 1.0.0 version: 1.0.0

View File

@@ -1,6 +1,6 @@
openapi: 3.1.0 openapi: 3.1.0
servers: servers:
- url: https://api.example.com/v1 - url: https://api.redocly-example.com/v1
security: [] security: []
info: info:
title: Title title: Title

View File

@@ -1,6 +1,6 @@
openapi: 3.1.0 openapi: 3.1.0
servers: servers:
- url: https://api.example.com/v1 - url: https://api.redocly-example.com/v1
security: [] security: []
info: info:

View File

@@ -10,7 +10,7 @@ info:
description: Description description: Description
version: 1.0.0 version: 1.0.0
servers: servers:
- url: https://api.example.com/v1 - url: https://api.redocly-example.com/v1
security: [] security: []
paths: paths:
/my_post: /my_post:

View File

@@ -397,7 +397,7 @@ describe('E2E', () => {
(<any>expect(result)).toMatchSpecificSnapshot(join(testPath, 'snapshot.js')); (<any>expect(result)).toMatchSpecificSnapshot(join(testPath, 'snapshot.js'));
expect(fs.existsSync(join(testPath, 'nested/redoc-static.html'))).toEqual(true); expect(fs.existsSync(join(testPath, 'nested/redoc-static.html'))).toEqual(true);
expect(fs.statSync(join(testPath, 'nested/redoc-static.html')).size).toEqual(32996); expect(fs.statSync(join(testPath, 'nested/redoc-static.html')).size).toEqual(33016);
}); });
}); });
}); });

View File

@@ -4,7 +4,7 @@ info:
description: This is an example API. description: This is an example API.
version: 1.0.0 version: 1.0.0
servers: servers:
- url: https://example.com/api - url: https://redocly-example.com/api
paths: paths:
/users/{userId}: /users/{userId}:
parameters: parameters:

View File

@@ -4,7 +4,7 @@ info:
description: This is an example API. description: This is an example API.
version: 1.0.0 version: 1.0.0
servers: servers:
- url: https://example.com/api - url: https://redocly-example.com/api
paths: paths:
/users/{userId}/orders/{orderId}: /users/{userId}/orders/{orderId}:
parameters: parameters:

View File

@@ -8,7 +8,7 @@ info:
description: This is an example API. description: This is an example API.
version: 1.0.0 version: 1.0.0
servers: servers:
- url: https://example.com/api - url: https://redocly-example.com/api
tags: tags:
- name: bar_other - name: bar_other
x-displayName: other x-displayName: other

View File

@@ -4,7 +4,7 @@ info:
description: This is an example API. description: This is an example API.
version: 1.0.0 version: 1.0.0
servers: servers:
- url: https://example.com/api - url: https://redocly-example.com/api
paths: paths:
/users/{userId}: /users/{userId}:
parameters: parameters:

View File

@@ -4,7 +4,7 @@ info:
description: This is an example API. description: This is an example API.
version: 1.0.0 version: 1.0.0
servers: servers:
- url: https://example.com/api - url: https://redocly-example.com/api
paths: paths:
/users/{userId}/orders/{orderId}: /users/{userId}/orders/{orderId}:
parameters: parameters:

View File

@@ -8,7 +8,7 @@ info:
description: This is an example API. description: This is an example API.
version: 1.0.0 version: 1.0.0
servers: servers:
- url: https://example.com/api - url: https://redocly-example.com/api
tags: tags:
- name: foo_other - name: foo_other
x-displayName: other x-displayName: other

View File

@@ -4,7 +4,7 @@ info:
description: This is an example API. description: This is an example API.
version: 1.0.0 version: 1.0.0
servers: servers:
- url: https://example.com/api - url: https://redocly-example.com/api
paths: paths:
/users/{userId}/orders/{orderId}: /users/{userId}/orders/{orderId}:
parameters: parameters:

View File

@@ -4,7 +4,7 @@ info:
description: This is an example API. description: This is an example API.
version: 1.0.0 version: 1.0.0
servers: servers:
- url: https://example.com/api - url: https://redocly-example.com/api
paths: paths:
/users/{userId}/products/{productId}: /users/{userId}/products/{productId}:
parameters: parameters:

View File

@@ -8,7 +8,7 @@ info:
description: This is an example API. description: This is an example API.
version: 1.0.0 version: 1.0.0
servers: servers:
- url: https://example.com/api - url: https://redocly-example.com/api
tags: tags:
- name: foo_other - name: foo_other
x-displayName: other x-displayName: other

View File

@@ -1,6 +1,6 @@
openapi: 3.0.0 openapi: 3.0.0
servers: servers:
- url: http://localhost:8080 - url: http://redocly-example.com:8080
info: info:
description: example test description: example test
version: 1.0.0 version: 1.0.0

View File

@@ -14,8 +14,8 @@ info:
name: Apache 2.0 name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html url: http://www.apache.org/licenses/LICENSE-2.0.html
servers: servers:
- url: http://localhost:8081 - url: http://redocly-example.com:8081
- url: http://localhost:8080 - url: http://redocly-example.com:8080
tags: tags:
- name: user - name: user
x-displayName: user x-displayName: user

View File

@@ -1,6 +1,6 @@
openapi: 3.0.0 openapi: 3.0.0
servers: servers:
- url: http://localhost:8081 - url: http://redocly-example.com:8081
info: info:
description: test description: test
version: 1.0.0 version: 1.0.0

View File

@@ -8,7 +8,7 @@ tags:
- name: sometag - name: sometag
description: sometag description: sometag
servers: servers:
- url: https://example.com - url: https://redocly-example.com
security: [] security: []
paths: paths:
/someGet: /someGet:

View File

@@ -27,7 +27,7 @@ paths:
description: summary example description: summary example
externalDocs: externalDocs:
description: 'Find more info here' description: 'Find more info here'
url: 'https://example.com' url: 'https://redocly-example.com'
requestBody: requestBody:
content: content:
application/json: application/json:

View File

@@ -188,7 +188,7 @@ rule/anyType failed because the any description didn't meet the assertions: Shou
28 | externalDocs: 28 | externalDocs:
29 | description: 'Find more info here' 29 | description: 'Find more info here'
| ^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^
30 | url: 'https://example.com' 30 | url: 'https://redocly-example.com'
31 | requestBody: 31 | requestBody:
Error was generated by the rule/anyType rule. Error was generated by the rule/anyType rule.

View File

@@ -25,7 +25,7 @@ paths:
summary: My resource summary: My resource
externalDocs: externalDocs:
description: 'Find more info here' description: 'Find more info here'
url: 'https://example.com' url: 'https://redocly-example.com'
requestBody: requestBody:
content: content:
application/json: application/json:

View File

@@ -3,14 +3,14 @@ info:
title: Example OpenAPI 3 definition. Valid. title: Example OpenAPI 3 definition. Valid.
version: 1.0 version: 1.0
servers: servers:
- url: https://example.com/user/{id}/place/{placeId} - url: https://redocly-example.com/user/{id}/place/{placeId}
variables: variables:
id: id:
enum: [] enum: []
placeId: placeId:
enum: [b] enum: [b]
default: a default: a
- url: https://example.com/{var} - url: https://redocly-example.com/{var}
variables: {} variables: {}
paths: paths:

View File

@@ -11,7 +11,7 @@ Server variable with \`enum\` must be a non-empty array.
4 | version: 1.0 4 | version: 1.0
5 | servers: 5 | servers:
| ^^^^^^^ | ^^^^^^^
6 | - url: https://example.com/user/{id}/place/{placeId} 6 | - url: https://redocly-example.com/user/{id}/place/{placeId}
7 | variables: 7 | variables:
Error was generated by the no-server-variables-empty-enum rule. Error was generated by the no-server-variables-empty-enum rule.
@@ -25,7 +25,7 @@ Server variable define \`enum\` and \`default\`. \`enum\` must include default v
4 | version: 1.0 4 | version: 1.0
5 | servers: 5 | servers:
| ^^^^^^^ | ^^^^^^^
6 | - url: https://example.com/user/{id}/place/{placeId} 6 | - url: https://redocly-example.com/user/{id}/place/{placeId}
7 | variables: 7 | variables:
Error was generated by the no-server-variables-empty-enum rule. Error was generated by the no-server-variables-empty-enum rule.

View File

@@ -3,14 +3,14 @@ info:
title: Example OpenAPI 3 definition. Valid. title: Example OpenAPI 3 definition. Valid.
version: 1.0 version: 1.0
servers: servers:
- url: https://example.com/user/{id}/place/{placeId} - url: https://redocly-example.com/user/{id}/place/{placeId}
variables: variables:
id: id:
enum: [c] enum: [c]
placeId: placeId:
enum: [b, a] enum: [b, a]
default: a default: a
- url: https://example.com/{var} - url: https://redocly-example.com/{var}
variables: {} variables: {}
paths: paths:

View File

@@ -13,7 +13,7 @@ tags:
- name: Gameplay - name: Gameplay
description: gameplay tag description: gameplay tag
servers: servers:
- url: https://example.com/{var} - url: https://redocly-example.com/{var}
variables: variables:
var: var:
enum: enum:

View File

@@ -13,7 +13,7 @@ tags:
- name: Gameplay - name: Gameplay
description: gameplay tag description: gameplay tag
servers: servers:
- url: https://example.com/{var} - url: https://redocly-example.com/{var}
variables: variables:
var: var:
enum: enum:

View File

@@ -1,6 +1,6 @@
openapi: 3.0.1 openapi: 3.0.1
servers: servers:
- url: http://localhost:8080 - url: http://redocly-example.com:8080
info: info:
title: Test title: Test
version: 1.0.0 version: 1.0.0

View File

@@ -6,7 +6,7 @@ info:
title: Test title: Test
version: 1.0.0 version: 1.0.0
servers: servers:
- url: http://localhost:8080 - url: http://redocly-example.com:8080
security: [] security: []
paths: paths:
/items: /items:
@@ -57,7 +57,7 @@ components:
Info object should contain \`license\` field. Info object should contain \`license\` field.
2 | servers: 2 | servers:
3 | - url: http://localhost:8080 3 | - url: http://redocly-example.com:8080
4 | info: 4 | info:
| ^^^^ | ^^^^
5 | title: Test 5 | title: Test
@@ -82,7 +82,7 @@ validating openapi.yaml...
Info object should contain \`license\` field. Info object should contain \`license\` field.
2 | servers: 2 | servers:
3 | - url: http://localhost:8080 3 | - url: http://redocly-example.com:8080
4 | info: 4 | info:
| ^^^^ | ^^^^
5 | title: Test 5 | title: Test

View File

@@ -1,6 +1,6 @@
openapi: 3.1.0 openapi: 3.1.0
servers: servers:
- url: http://localhost - url: http://redocly-example.com
info: info:
title: Sample API title: Sample API
version: 1.0.0 version: 1.0.0

View File

@@ -20,7 +20,7 @@ describe('lint', () => {
license: Fail license: Fail
servers: servers:
- url: http://example.com - url: http://redocly-example.com
paths: {} paths: {}
`, `,
config: await loadConfig(), config: await loadConfig(),

View File

@@ -30,7 +30,7 @@ describe('Oas3 oas3-no-server-example.com', () => {
"source": "foobar.yaml", "source": "foobar.yaml",
}, },
], ],
"message": "Server \`url\` should not point at example.com.", "message": "Server \`url\` should not point to example.com or localhost.",
"ruleId": "no-server-example.com", "ruleId": "no-server-example.com",
"severity": "error", "severity": "error",
"suggest": Array [], "suggest": Array [],
@@ -57,4 +57,39 @@ describe('Oas3 oas3-no-server-example.com', () => {
expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`Array []`); expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`Array []`);
}); });
it('oas3-no-server-example.com: should report on server object with "foo.example.com" url', async () => {
const document = parseYamlToDocument(
outdent`
openapi: 3.0.0
servers:
- url: foo.example.com
`,
'foobar.yaml'
);
const results = await lintDocument({
externalRefResolver: new BaseResolver(),
document,
config: await makeConfig({ 'no-server-example.com': 'error' }),
});
expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`
Array [
Object {
"location": Array [
Object {
"pointer": "#/servers/0/url",
"reportOnKey": false,
"source": "foobar.yaml",
},
],
"message": "Server \`url\` should not point to example.com or localhost.",
"ruleId": "no-server-example.com",
"severity": "error",
"suggest": Array [],
},
]
`);
});
}); });

View File

@@ -17,7 +17,7 @@ describe('Oas3 Structural visitor basic', () => {
- 25.3 - 25.3
- test - test
servers: servers:
- url: 'http://example.com' - url: 'http://redocly-example.com'
variables: variables:
a: a:
default: test default: test

View File

@@ -3,9 +3,10 @@ import { Oas3Rule } from '../../visitors';
export const NoServerExample: Oas3Rule = () => { export const NoServerExample: Oas3Rule = () => {
return { return {
Server(server, { report, location }) { Server(server, { report, location }) {
if (['example.com', 'localhost'].indexOf(server.url) !== -1) { const pattern = /^(.*[\/.])?(example\.com|localhost)([\/:?].*|$)/;
if (server.url && pattern.test(server.url)) {
report({ report({
message: 'Server `url` should not point at example.com.', message: 'Server `url` should not point to example.com or localhost.',
location: location.child(['url']), location: location.child(['url']),
}); });
} }