Skip to content

Commit

Permalink
Check router rule recursion depth
Browse files Browse the repository at this point in the history
  • Loading branch information
sisidovski committed Jan 30, 2025
1 parent 9f380a1 commit 629a3ec
Showing 1 changed file with 56 additions and 2 deletions.
58 changes: 56 additions & 2 deletions docs/index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -1606,6 +1606,10 @@ spec: storage; urlPrefix: https://storage.spec.whatwg.org/

Note: {{RouterCondition/_or}} and {{RouterCondition/not}} might have the other {{RouterCondition/_or}} or {{RouterCondition/not}} inside. To avoid spending much resources by the nested condition or performance penalty on evaluation, depth of such nested conditions can be limited.

A <dfn export id="dfn-count-router-condition-result">count router condition result</dfn> is a [=struct=] that consists of:
* A <dfn export id="dfn-count-router-condition-result-total-count" for="count router condition result">total count</dfn> (a number).
* A <dfn export id="dfn-dfn-count-router-condition-result-depth" for="count router condition result">depth</dfn> (a number).

<section>
<h4 id="register-router-method">{{InstallEvent/addRoutes(rules)|event.addRoutes(rules)}}</h4>

Expand All @@ -1620,6 +1624,7 @@ spec: storage; urlPrefix: https://storage.spec.whatwg.org/
1. If running the [=Verify Router Condition=] algorithm with |rule|["{{RouterRule/condition}}"] and |serviceWorker| returns false, return [=a promise rejected with=] a {{TypeError}}.
1. Append |rule| to |routerRules|.
1. If |routerRules| [=list/contains=] a {{RouterRule}} whose {{RouterRule/source}} is either of "{{RouterSourceEnum/fetch-event}}" or "{{RouterSourceEnum/race-network-and-fetch-handler}}", and |serviceWorker|'s [=set of event types to handle=] does not [=set/contain=] {{ServiceWorkerGlobalScope/fetch!!event}}, return [=a promise rejected with=] a {{TypeError}}.
1. If running the [=Check Router Registration Limit=] with |serviceWorker| returns false, return [=a promise rejected with=] a {{TypeError}}.
1. Set |serviceWorker|'s [=service worker/list of router rules=] to |routerRules|.
1. Return [=a promise resolved with=] undefined.

Expand Down Expand Up @@ -3409,14 +3414,14 @@ spec: storage; urlPrefix: https://storage.spec.whatwg.org/
Note: To limit the resource usage and a condition evaluation time, |orConditions|'s [=list/size=] can be limited.

1. For each |orCondition| of |orConditions|:
1. If running the [=Verify Router Condition=] algorithm with |orCondition| and |serviceWorker| returns false, return false.
1. If running the [=Verify Router Condition=] algorithm with |orCondition| and |serviceWorker| and |registeredRuleCount| returns false, return false.
1. Set |hasCondition| to true.
1. If |condition|["{{RouterCondition/not}}"] [=map/exists=], then:
1. If |hasCondition| is true, return false.

Note: For ease of understanding the router rule, the "not" condition is mutually exclusive with other conditions.

1. If running the [=Verify Router Condition=] algorithm with |condition|["{{RouterCondition/not}}"] and |serviceWorker| returns false, return false.
1. If running the [=Verify Router Condition=] algorithm with |condition|["{{RouterCondition/not}}"] and |serviceWorker| and |registeredRuleCount| returns false, return false.
1. Set |hasCondition| to true.
1. If |hasCondition| is true, then:
1. Increament |serviceWorker|'s [=service worker/router rule count=] by one.
Expand Down Expand Up @@ -3468,6 +3473,55 @@ spec: storage; urlPrefix: https://storage.spec.whatwg.org/
1. Return true.
</section>

<section algorithm>
<h3 id="check-router-registration-limit"><dfn>Check Router Registration Limit</dfn></h3>
: Input
:: |serviceWorker|, a [=/service worker=]
: Output
:: a boolean

1. Let |currentResult| be a [=count router condition result=].
1. Set |currentResult|'s [=count router condition result/total count=] to 0.
1. Set |currentResult|'s [=count router condition result/depth=] to 1.
1. Let |maxRuleCount| be 1024.
1. Let |maxDepth| be 10.
1. [=list/For each=] |rule| of |serviceWorker|'s [=service worker/list of router rules=]:
1. Set |result| to be the result of running [=Count Router Inner Conditions=] with |rule|["{{RouterRule/condition}}"], |currentResult|, |maxRuleCount|, and |maxDepth|.
1. If |result|'s [=count router condition result/total count=] exceeds |maxRuleCount|, return false.
1. If |result|'s [=count router condition result/depth=] exceeds |maxDepth|, return false.
1. Set |currentResult|'s [=count router condition result/total count=] to |result|'s [=count router condition result/total count=].
1. return true.
</section>

<section algorithm>
<h3 id="count-router-inner-conditions"><dfn>Count Router Inner Conditions</dfn></h3>
: Input
:: |condition|, a {{RouterCondition}}
:: |result|, a [=count router condition result=]
:: |maxCount|, a number
:: |maxDepth|, a number
: Output
:: |result|, a [=count router condition result=]

1. Increment |result|'s [=count router condition result/total count=] by one.
1. If |result|'s [=count router condition result/total count=] exceeds |maxCount|, return |result|.
1. If |condition|["{{RouterCondition/_or}}"] [=map/exists=], then:
1. Increment |result|'s [=count router condition result/depth=] by one.
1. If |result|'s [=count router condition result/depth=] exceeds |maxDepth|, return |result|.
1. For each |orCondition| of |condition|["{{RouterCondition/_or}}"]:
1. Set |result| to be the result of running [=Count Router Inner Conditions=] with |orCondition|, |result|, |maxCount|, and |maxDepth|.
1. If |result|'s [=count router condition result/total count=] exceeds |maxCount|, return |result|.
1. If |result|'s [=count router condition result/depth=] exceeds |maxDepth|, return |result|.
1. Else if |condition|["{{RouterCondition/not}}"] [=map/exists=], then:
1. Increment |result|'s [=count router condition result/depth=] by one.
1. If |result|'s [=count router condition result/depth=] exceeds |maxDepth|, return |result|.
1. Let |notCondition| be |condition|["{{RouterCondition/not}}"].
1. Let |result| be the result of running [=Count Router Inner Conditions=] with |condition|["{{RouterCondition/not}}"], |result|, |maxCount|, and |maxDepth|.
1. If |result|'s [=count router condition result/total count=] exceeds |maxCount|, return |result|.
1. If |result|'s [=count router condition result/depth=] exceeds |maxDepth|, return |result|.
1. Return |result|.
</section>

<section algorithm>
<h3 id="get-router-source-algorithm"><dfn>Get Router Source</dfn></h3>
: Input
Expand Down

0 comments on commit 629a3ec

Please sign in to comment.