Skip to content

Commit f427b1a

Browse files
committed
Check router rule recursion depth
1 parent 9f380a1 commit f427b1a

File tree

1 file changed

+55
-11
lines changed

1 file changed

+55
-11
lines changed

docs/index.bs

+55-11
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,6 @@ spec: storage; urlPrefix: https://storage.spec.whatwg.org/
212212

213213
A [=/service worker=] has an associated <dfn>list of router rules</dfn> (a [=list=] of {{RouterRule}}s). It is initially an empty [=list=].
214214

215-
A [=/service worker=] has an associated <dfn>router rule count</dfn> (the number of registered {{RouterRule}}s). It is initially set to zero.
216-
217-
Note: [=router rule count=] is defined as the limit of the total number of registered router rules. This prevents the user agent from spending much resources by evaluating too many router rules. The limit is 1024.
218-
219215
A [=/service worker=] is said to be <dfn>running</dfn> if its [=event loop=] is running.
220216

221217
<section>
@@ -1604,7 +1600,9 @@ spec: storage; urlPrefix: https://storage.spec.whatwg.org/
16041600
};
16051601
</pre>
16061602

1607-
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.
1603+
A <dfn export id="dfn-count-router-condition-result">count router condition result</dfn> is a [=struct=] that consists of:
1604+
* A <dfn export id="dfn-count-router-condition-result-total-count" for="count router condition result">total count</dfn> (a number).
1605+
* A <dfn export id="dfn-dfn-count-router-condition-result-depth" for="count router condition result">depth</dfn> (a number).
16081606

16091607
<section>
16101608
<h4 id="register-router-method">{{InstallEvent/addRoutes(rules)|event.addRoutes(rules)}}</h4>
@@ -1620,6 +1618,7 @@ spec: storage; urlPrefix: https://storage.spec.whatwg.org/
16201618
1. If running the [=Verify Router Condition=] algorithm with |rule|["{{RouterRule/condition}}"] and |serviceWorker| returns false, return [=a promise rejected with=] a {{TypeError}}.
16211619
1. Append |rule| to |routerRules|.
16221620
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}}.
1621+
1. If running the [=Check Router Registration Limit=] with |serviceWorker| returns false, return [=a promise rejected with=] a {{TypeError}}.
16231622
1. Set |serviceWorker|'s [=service worker/list of router rules=] to |routerRules|.
16241623
1. Return [=a promise resolved with=] undefined.
16251624

@@ -3405,9 +3404,6 @@ spec: storage; urlPrefix: https://storage.spec.whatwg.org/
34053404
Note: For ease of understanding the router rule, the "or" condition is mutually exclusive with other conditions.
34063405

34073406
1. Let |orConditions| be |condition|["{{RouterCondition/_or}}"].
3408-
3409-
Note: To limit the resource usage and a condition evaluation time, |orConditions|'s [=list/size=] can be limited.
3410-
34113407
1. For each |orCondition| of |orConditions|:
34123408
1. If running the [=Verify Router Condition=] algorithm with |orCondition| and |serviceWorker| returns false, return false.
34133409
1. Set |hasCondition| to true.
@@ -3418,9 +3414,6 @@ spec: storage; urlPrefix: https://storage.spec.whatwg.org/
34183414

34193415
1. If running the [=Verify Router Condition=] algorithm with |condition|["{{RouterCondition/not}}"] and |serviceWorker| returns false, return false.
34203416
1. Set |hasCondition| to true.
3421-
1. If |hasCondition| is true, then:
3422-
1. Increament |serviceWorker|'s [=service worker/router rule count=] by one.
3423-
1. If |serviceWorker|'s [=service worker/router rule count=] exceeds 1024, which is the limit of registered router rules, return false.
34243417
1. Return |hasCondition|.
34253418
</section>
34263419

@@ -3468,6 +3461,57 @@ spec: storage; urlPrefix: https://storage.spec.whatwg.org/
34683461
1. Return true.
34693462
</section>
34703463

3464+
<section algorithm>
3465+
<h3 id="check-router-registration-limit"><dfn>Check Router Registration Limit</dfn></h3>
3466+
3467+
: Input
3468+
:: |serviceWorker|, a [=/service worker=]
3469+
: Output
3470+
:: a boolean
3471+
3472+
Note: Router conditions can be complex and nested using {{RouterCondition/_or}} and {{RouterCondition/not}}. To prevent excessive processing, this algorithm introduces two limits. First, the total number of conditions, counting all nested conditions, cannot exceed 1024. Second, the nesting depth is limited to 10 levels to avoid exponential computation.
3473+
3474+
1. Let |result| be a [=count router condition result=].
3475+
1. Set |result|'s [=count router condition result/total count=] to 0.
3476+
1. Set |result|'s [=count router condition result/depth=] to 1.
3477+
1. Let |maxCount| be 1024.
3478+
1. Let |maxDepth| be 10.
3479+
1. [=list/For each=] |rule| of |serviceWorker|'s [=service worker/list of router rules=]:
3480+
1. Let |currentResult| be the result of running [=Count Router Inner Conditions=] with |rule|["{{RouterRule/condition}}"], |result|, |maxCount|, and |maxDepth|.
3481+
1. If |currentResult|'s [=count router condition result/total count=] exceeds |maxCount|, return false.
3482+
1. If |currentResult|'s [=count router condition result/depth=] exceeds |maxDepth|, return false.
3483+
1. Set |result|'s [=count router condition result/total count=] to |currentResult|'s [=count router condition result/total count=].
3484+
1. return true.
3485+
</section>
3486+
3487+
<section algorithm>
3488+
<h3 id="count-router-inner-conditions"><dfn>Count Router Inner Conditions</dfn></h3>
3489+
3490+
: Input
3491+
:: |condition|, a {{RouterCondition}}
3492+
:: |result|, a [=count router condition result=]
3493+
:: |maxCount|, a number
3494+
:: |maxDepth|, a number
3495+
: Output
3496+
:: |result|, a [=count router condition result=]
3497+
3498+
1. Increment |result|'s [=count router condition result/total count=] by one.
3499+
1. If |result|'s [=count router condition result/total count=] exceeds |maxCount|, return |result|.
3500+
1. If |result|'s [=count router condition result/depth=] exceeds |maxDepth|, return |result|.
3501+
1. If |condition|["{{RouterCondition/_or}}"] [=map/exists=], then:
3502+
1. Increment |result|'s [=count router condition result/depth=] by one.
3503+
1. For each |orCondition| of |condition|["{{RouterCondition/_or}}"]:
3504+
1. Set |result| to be the result of running [=Count Router Inner Conditions=] with |orCondition|, |result|, |maxCount|, and |maxDepth|.
3505+
1. If |result|'s [=count router condition result/total count=] exceeds |maxCount|, return |result|.
3506+
1. If |result|'s [=count router condition result/depth=] exceeds |maxDepth|, return |result|.
3507+
1. Else if |condition|["{{RouterCondition/not}}"] [=map/exists=], then:
3508+
1. Increment |result|'s [=count router condition result/depth=] by one.
3509+
1. Set |result| to be the result of running [=Count Router Inner Conditions=] with |condition|["{{RouterCondition/not}}"], |result|, |maxCount|, and |maxDepth|.
3510+
1. If |result|'s [=count router condition result/total count=] exceeds |maxCount|, return |result|.
3511+
1. If |result|'s [=count router condition result/depth=] exceeds |maxDepth|, return |result|.
3512+
1. Return |result|.
3513+
</section>
3514+
34713515
<section algorithm>
34723516
<h3 id="get-router-source-algorithm"><dfn>Get Router Source</dfn></h3>
34733517
: Input

0 commit comments

Comments
 (0)