-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: check for policies attached after RaiseFault
- Loading branch information
1 parent
12e2bdb
commit 8bd4ce8
Showing
21 changed files
with
619 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
lib/package/plugins/ST008-unreachable-policies-after-raisefault.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
/* | ||
Copyright 2019-2024 Google LLC | ||
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 | ||
https://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. | ||
*/ | ||
|
||
const ruleId = require("../lintUtil.js").getRuleId(), | ||
debug = require("debug")("apigeelint:" + ruleId), | ||
util = require("util"), | ||
xpath = require("xpath"); | ||
|
||
const plugin = { | ||
ruleId, | ||
name: "Unreachable Steps after RaiseFault", | ||
message: "Steps in the same flow after a RaiseFault cannot be reached.", | ||
fatal: false, | ||
severity: 1, //1=warning, 2=error | ||
nodeType: "Step", | ||
enabled: true, | ||
}; | ||
|
||
let bundle = null; | ||
|
||
class PolicyFlowChecker { | ||
constructor(plugin, debug) { | ||
debug(`PolicyFlowChecker ctor`); | ||
this.plugin = plugin; | ||
this.debug = debug; | ||
} | ||
|
||
checkSequence(stepParent, entity) { | ||
let flagged = false; | ||
const allSteps = stepParent.getSteps(); | ||
// get the array of indexes | ||
const isUnconditionalRaiseFaults = allSteps.map((step) => { | ||
if (step.getCondition()) return false; | ||
let referredPolicy = bundle | ||
.getPolicies() | ||
.find((p) => p.getSteps().find((s) => s == step)); | ||
if (!referredPolicy || referredPolicy.getType() !== "RaiseFault") | ||
return false; | ||
let enabled = referredPolicy.select("@enabled"); | ||
enabled = enabled && enabled[0]; | ||
if (!enabled || enabled == "true") return true; | ||
return false; | ||
}); | ||
|
||
// just look at the FIRST one, skip all subsequent unconditional RF | ||
isUnconditionalRaiseFaults.find((found, ix) => { | ||
if (found) { | ||
const step = allSteps[ix]; | ||
debug( | ||
`found an attached, unconditional RaiseFault policy, line ${step.getElement().lineNumber}`, | ||
); | ||
if (ix < allSteps.length - 1) { | ||
debug(`not last in flow`); | ||
allSteps.slice(ix + 1).forEach((unreachableStep) => { | ||
flagged = true; | ||
entity.addMessage({ | ||
plugin: checker.plugin, | ||
source: unreachableStep.getElement().toString(), | ||
line: unreachableStep.getElement().lineNumber, | ||
column: unreachableStep.getElement().columnNumber, | ||
message: `Step ${unreachableStep.getName()} is attached after a RaiseFault, cannot be reached.`, | ||
}); | ||
}); | ||
} | ||
return true; | ||
} | ||
return false; | ||
}); | ||
return flagged; | ||
} | ||
|
||
check(sequence) { | ||
let flagged = false; | ||
try { | ||
let debug = this.debug; | ||
let bundlePolicies = bundle.getPolicies(); | ||
let checker = this; | ||
let sequenceType = sequence.constructor.name; | ||
debug(`thingType: ${sequenceType}`); | ||
if (sequenceType == "Flow") { | ||
const flow = sequence; | ||
debug(`flow.name: ${flow.name}`); | ||
|
||
["FlowRequest", "FlowResponse", "SharedFlow"].forEach((m) => { | ||
const method = `get${m}`; | ||
debug(`calling flow.${method}()`); | ||
const flowPhase = flow[method](); | ||
if (flowPhase) { | ||
if (this.checkSequence(flowPhase, flow)) { | ||
flagged = true; | ||
} | ||
} | ||
}); | ||
} else { | ||
if (this.checkSequence(sequence, sequence.getParent())) { | ||
flagged = true; | ||
} | ||
} | ||
} catch (exc1) { | ||
flagged = true; | ||
sequence.addMessage({ | ||
plugin: checker.plugin, | ||
message: exc1.message + exc1.stack, | ||
}); | ||
} | ||
|
||
return flagged; | ||
} | ||
} | ||
|
||
const checker = new PolicyFlowChecker(plugin, debug); | ||
|
||
const checkEndpoint = function (endpoint, cb) { | ||
let flagged = false; | ||
try { | ||
endpoint.getAllFlows().forEach((sequence, ix) => { | ||
if (checker.check(sequence)) { | ||
flagged = true; | ||
} | ||
}); | ||
endpoint.getFaultRules().forEach((sequence) => { | ||
if (checker.check(sequence)) { | ||
flagged = true; | ||
} | ||
}); | ||
if (endpoint.getDefaultFaultRule()) { | ||
if (checker.check(endpoint.getDefaultFaultRule())) { | ||
flagged = true; | ||
} | ||
} | ||
} catch (exc1) { | ||
endpoint.addMessage({ | ||
plugin: checker.plugin, | ||
message: exc1.message + " " + exc1.stack, | ||
}); | ||
} | ||
|
||
if (typeof cb == "function") { | ||
cb(null, flagged); | ||
} | ||
}; | ||
|
||
const onProxyEndpoint = function (endpoint, cb) { | ||
debug("onProxyEndpoint"); | ||
checkEndpoint(endpoint, cb); | ||
}; | ||
|
||
const onTargetEndpoint = function (endpoint, cb) { | ||
debug("onTargetEndpoint"); | ||
checkEndpoint(endpoint, cb); | ||
}; | ||
|
||
const onBundle = function (b, cb) { | ||
bundle = b; | ||
}; | ||
|
||
module.exports = { | ||
plugin, | ||
onBundle, | ||
onProxyEndpoint, | ||
onTargetEndpoint, | ||
}; |
11 changes: 11 additions & 0 deletions
11
...s/ST008-unreached-policies-after-raisefault/apiproxy/policies/AM-CleanResponseHeaders.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<AssignMessage name='AM-CleanResponseHeaders' enabled='false'> | ||
<!-- | ||
This policy is not enabled. Using it with CORS on a loopback | ||
policy, even as the first policy in the response preflow, will | ||
erase the response headers that the cORS policy sets. | ||
--> | ||
<Remove> | ||
<Headers/> | ||
</Remove> | ||
<IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables> | ||
</AssignMessage> |
7 changes: 7 additions & 0 deletions
7
...resources/ST008-unreached-policies-after-raisefault/apiproxy/policies/AM-Extra-Header.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<AssignMessage name="AM-Extra-Header"> | ||
<Set> | ||
<Headers> | ||
<Header name='extra'>{messageid}</Header> | ||
</Headers> | ||
</Set> | ||
</AssignMessage> |
7 changes: 7 additions & 0 deletions
7
...008-unreached-policies-after-raisefault/apiproxy/policies/AM-InjectProxyVersionHeader.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<AssignMessage name="AM-InjectProxyVersionHeader"> | ||
<Set> | ||
<Headers> | ||
<Header name="APIProxy">{apiproxy.name} r{apiproxy.revision}</Header> | ||
</Headers> | ||
</Set> | ||
</AssignMessage> |
9 changes: 9 additions & 0 deletions
9
...res/resources/ST008-unreached-policies-after-raisefault/apiproxy/policies/AM-Response.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<AssignMessage name="AM-Response"> | ||
<Set> | ||
<Payload contentType="text/plain"> | ||
request verb: {request.verb} | ||
</Payload> | ||
<StatusCode>200</StatusCode> | ||
<ReasonPhrase>OK</ReasonPhrase> | ||
</Set> | ||
</AssignMessage> |
13 changes: 13 additions & 0 deletions
13
...es/ST008-unreached-policies-after-raisefault/apiproxy/policies/JS-RemoveCopiedHeaders.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Javascript name="JS-RemoveCopiedHeaders" enabled='true'> | ||
<!-- | ||
This policy removes the request headers that Apigee | ||
implicitly copies to the response, on a oopback proxy. | ||
--> | ||
<Source> | ||
var names = String(context.getVariable('request.headers.names')); | ||
names = names.substring(1, names.length-1); | ||
names.split(',').forEach(function(name) { | ||
context.removeVariable('response.header.' + name.trim()); | ||
}); | ||
</Source> | ||
</Javascript> |
9 changes: 9 additions & 0 deletions
9
...rces/ST008-unreached-policies-after-raisefault/apiproxy/policies/RF-Error-Not-Enabled.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<RaiseFault enabled="false" name="RF-Error-Not-Enabled"> | ||
<FaultResponse> | ||
<Set> | ||
<Payload contentType="text/plain">Error</Payload> | ||
<StatusCode>500</StatusCode> | ||
</Set> | ||
</FaultResponse> | ||
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables> | ||
</RaiseFault> |
9 changes: 9 additions & 0 deletions
9
...xtures/resources/ST008-unreached-policies-after-raisefault/apiproxy/policies/RF-Error.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<RaiseFault name="RF-Error"> | ||
<FaultResponse> | ||
<Set> | ||
<Payload contentType="text/plain">Error</Payload> | ||
<StatusCode>500</StatusCode> | ||
</Set> | ||
</FaultResponse> | ||
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables> | ||
</RaiseFault> |
10 changes: 10 additions & 0 deletions
10
...ources/ST008-unreached-policies-after-raisefault/apiproxy/policies/RF-Invalid-Request.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<RaiseFault name="RF-Invalid-Request"> | ||
<FaultResponse> | ||
<Set> | ||
<Payload contentType="text/plain">Bad Request</Payload> | ||
<StatusCode>400</StatusCode> | ||
<ReasonPhrase>Bad Request</ReasonPhrase> | ||
</Set> | ||
</FaultResponse> | ||
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables> | ||
</RaiseFault> |
98 changes: 98 additions & 0 deletions
98
...xtures/resources/ST008-unreached-policies-after-raisefault/apiproxy/proxies/endpoint1.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<ProxyEndpoint name="endpoint1"> | ||
<HTTPProxyConnection> | ||
<BasePath>/unreached-policies</BasePath> | ||
</HTTPProxyConnection> | ||
|
||
<DefaultFaultRule name="default-fault-rule"> | ||
<Step> | ||
<Name>AM-InjectProxyVersionHeader</Name> | ||
</Step> | ||
<AlwaysEnforce>true</AlwaysEnforce> | ||
</DefaultFaultRule> | ||
|
||
<FaultRules> | ||
<FaultRule name='rule1'> | ||
<Step> | ||
<Name>RF-Error</Name> | ||
</Step> | ||
<!-- error --> | ||
<Step> | ||
<Name>AM-CleanResponseHeaders</Name> | ||
</Step> | ||
<Step> | ||
<Name>AM-Extra-Header</Name> | ||
</Step> | ||
<Condition>conditional_statement</Condition> | ||
</FaultRule> | ||
</FaultRules> | ||
|
||
|
||
<PreFlow name="PreFlow"> | ||
<Request> | ||
</Request> | ||
<Response> | ||
<Step> | ||
<Name>AM-CleanResponseHeaders</Name> | ||
</Step> | ||
<Step> | ||
<Name>JS-RemoveCopiedHeaders</Name> | ||
</Step> | ||
<Step> | ||
<Name>AM-InjectProxyVersionHeader</Name> | ||
</Step> | ||
</Response> | ||
</PreFlow> | ||
|
||
<Flows> | ||
<Flow name="f1"> | ||
<Description>a contrived response</Description> | ||
<Request> | ||
</Request> | ||
<Response> | ||
</Response> | ||
<Condition>proxy.pathsuffix MatchesPath "/f1"</Condition> | ||
</Flow> | ||
|
||
<Flow name="f2"> | ||
<Description>possibly a contrived response, possibly a fault</Description> | ||
<Request> | ||
<Step> | ||
<Condition>request.header.accept = "*/*"</Condition> | ||
<Name>RF-Invalid-Request</Name> | ||
</Step> | ||
<!-- not an error --> | ||
<Step> | ||
<Name>AM-Extra-Header</Name> | ||
</Step> | ||
</Request> | ||
<Response/> | ||
<Condition>proxy.pathsuffix MatchesPath "/f2"</Condition> | ||
</Flow> | ||
|
||
<Flow name="f3"> | ||
<Description>unknown request - raise a fault</Description> | ||
<Request> | ||
<Step> | ||
<Name>RF-Invalid-Request</Name> | ||
</Step> | ||
<!-- error: unreachable --> | ||
<Step> | ||
<Name>AM-Extra-Header</Name> | ||
</Step> | ||
</Request> | ||
<Response/> | ||
</Flow> | ||
</Flows> | ||
|
||
<PostFlow name="PostFlow"> | ||
<Request/> | ||
<Response> | ||
<Step> | ||
<Name>AM-Response</Name> | ||
<Condition>request.verb != "OPTIONS"</Condition> | ||
</Step> | ||
</Response> | ||
</PostFlow> | ||
|
||
<RouteRule name="noroute"/> | ||
</ProxyEndpoint> |
Oops, something went wrong.