From 013e1367b07b04a6f103af2fba6834438d36147e Mon Sep 17 00:00:00 2001 From: Yoshisato Yanagisawa Date: Tue, 30 Apr 2024 05:46:55 +0000 Subject: [PATCH 1/6] Editorial: Add notice on the _or condition depth. --- docs/index.bs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/index.bs b/docs/index.bs index 00c46726..6020235c 100644 --- a/docs/index.bs +++ b/docs/index.bs @@ -1600,6 +1600,8 @@ 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. +

{{InstallEvent/addRoutes(rules)|event.addRoutes(rules)}}

@@ -3399,6 +3401,9 @@ spec: storage; urlPrefix: https://storage.spec.whatwg.org/ Note: For ease of understanding the router rule, the "or" condition is mutually exclusive with other conditions. 1. Let |orConditions| be |condition|["{{RouterCondition/_or}}"]. + + 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. Set |hasCondition| to true. From 9f380a11b89bf6809d9ac0e8d1362710825e7917 Mon Sep 17 00:00:00 2001 From: Shunya Shishido Date: Thu, 30 Jan 2025 13:59:03 +0900 Subject: [PATCH 2/6] Define 1024 as a upper limit of total registered router rules --- docs/index.bs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/docs/index.bs b/docs/index.bs index 6020235c..0a280725 100644 --- a/docs/index.bs +++ b/docs/index.bs @@ -212,6 +212,10 @@ spec: storage; urlPrefix: https://storage.spec.whatwg.org/ A [=/service worker=] has an associated list of router rules (a [=list=] of {{RouterRule}}s). It is initially an empty [=list=]. + A [=/service worker=] has an associated router rule count (the number of registered {{RouterRule}}s). It is initially set to zero. + + 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. + A [=/service worker=] is said to be running if its [=event loop=] is running.
@@ -3401,9 +3405,9 @@ spec: storage; urlPrefix: https://storage.spec.whatwg.org/ Note: For ease of understanding the router rule, the "or" condition is mutually exclusive with other conditions. 1. Let |orConditions| be |condition|["{{RouterCondition/_or}}"]. - + 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. Set |hasCondition| to true. @@ -3414,6 +3418,9 @@ spec: storage; urlPrefix: https://storage.spec.whatwg.org/ 1. If running the [=Verify Router Condition=] algorithm with |condition|["{{RouterCondition/not}}"] and |serviceWorker| 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. + 1. If |serviceWorker|'s [=service worker/router rule count=] exceeds 1024, which is the limit of registered router rules, return false. 1. Return |hasCondition|.
From b5a530c9658e1af75b5c9adbeb7dd27edb6c99a1 Mon Sep 17 00:00:00 2001 From: Shunya Shishido Date: Fri, 31 Jan 2025 00:18:22 +0900 Subject: [PATCH 3/6] Check router rule recursion depth --- docs/index.bs | 66 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 11 deletions(-) diff --git a/docs/index.bs b/docs/index.bs index 0a280725..4fb60314 100644 --- a/docs/index.bs +++ b/docs/index.bs @@ -212,10 +212,6 @@ spec: storage; urlPrefix: https://storage.spec.whatwg.org/ A [=/service worker=] has an associated list of router rules (a [=list=] of {{RouterRule}}s). It is initially an empty [=list=]. - A [=/service worker=] has an associated router rule count (the number of registered {{RouterRule}}s). It is initially set to zero. - - 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. - A [=/service worker=] is said to be running if its [=event loop=] is running.
@@ -1604,7 +1600,9 @@ 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 count router condition result is a [=struct=] that consists of: + * A total count (a number). + * A depth (a number).

{{InstallEvent/addRoutes(rules)|event.addRoutes(rules)}}

@@ -1620,6 +1618,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 |routerRules| 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. @@ -3405,9 +3404,6 @@ spec: storage; urlPrefix: https://storage.spec.whatwg.org/ Note: For ease of understanding the router rule, the "or" condition is mutually exclusive with other conditions. 1. Let |orConditions| be |condition|["{{RouterCondition/_or}}"]. - - 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. Set |hasCondition| to true. @@ -3418,9 +3414,6 @@ spec: storage; urlPrefix: https://storage.spec.whatwg.org/ 1. If running the [=Verify Router Condition=] algorithm with |condition|["{{RouterCondition/not}}"] and |serviceWorker| 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. - 1. If |serviceWorker|'s [=service worker/router rule count=] exceeds 1024, which is the limit of registered router rules, return false. 1. Return |hasCondition|.
@@ -3468,6 +3461,57 @@ spec: storage; urlPrefix: https://storage.spec.whatwg.org/ 1. Return true.
+
+

Check Router Registration Limit

+ + : Input + :: |routerRules|, a [=list of router rules=] + : Output + :: a boolean + + 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. + + 1. Let |result| be a [=count router condition result=]. + 1. Set |result|'s [=count router condition result/total count=] to 0. + 1. Set |result|'s [=count router condition result/depth=] to 1. + 1. Let |maxCount| be 1024. + 1. Let |maxDepth| be 10. + 1. [=list/For each=] |rule| of |routerRules|: + 1. Let |currentResult| be the result of running [=Count Router Inner Conditions=] with |rule|["{{RouterRule/condition}}"], |result|, |maxCount|, and |maxDepth|. + 1. If |currentResult|'s [=count router condition result/total count=] exceeds |maxCount|, return false. + 1. If |currentResult|'s [=count router condition result/depth=] exceeds |maxDepth|, return false. + 1. Set |result|'s [=count router condition result/total count=] to |currentResult|'s [=count router condition result/total count=]. + 1. return true. +
+ +
+

Count Router Inner Conditions

+ + : 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 |result|'s [=count router condition result/depth=] exceeds |maxDepth|, return |result|. + 1. If |condition|["{{RouterCondition/_or}}"] [=map/exists=], then: + 1. Increment |result|'s [=count router condition result/depth=] by one. + 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. Set |result| to 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|. +
+

Get Router Source

: Input From b1bdb83cb9ce6dab3e70a5abb9b23a88a796f32b Mon Sep 17 00:00:00 2001 From: Shunya Shishido Date: Fri, 31 Jan 2025 13:12:19 +0900 Subject: [PATCH 4/6] Remove export from the struct --- docs/index.bs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/index.bs b/docs/index.bs index 4fb60314..39de74ab 100644 --- a/docs/index.bs +++ b/docs/index.bs @@ -1600,9 +1600,9 @@ spec: storage; urlPrefix: https://storage.spec.whatwg.org/ }; - A count router condition result is a [=struct=] that consists of: - * A total count (a number). - * A depth (a number). + A count router condition result is a [=struct=] that consists of: + * A total count (a number). + * A depth (a number).

{{InstallEvent/addRoutes(rules)|event.addRoutes(rules)}}

From cfe04286ee444d27584d69cd9d5de202450c89ea Mon Sep 17 00:00:00 2001 From: Shunya Shishido Date: Wed, 5 Feb 2025 00:23:04 +0900 Subject: [PATCH 5/6] Add error boolean and remove depth from the struct, and simplify algorithms not to pass maxCount and maxDepth --- docs/index.bs | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/docs/index.bs b/docs/index.bs index 39de74ab..a84afe31 100644 --- a/docs/index.bs +++ b/docs/index.bs @@ -1601,8 +1601,8 @@ spec: storage; urlPrefix: https://storage.spec.whatwg.org/ A count router condition result is a [=struct=] that consists of: - * A total count (a number). - * A depth (a number). + * A condition count (a number). + * A quota exceeded (a boolean).

{{InstallEvent/addRoutes(rules)|event.addRoutes(rules)}}

@@ -3472,15 +3472,11 @@ spec: storage; urlPrefix: https://storage.spec.whatwg.org/ 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. 1. Let |result| be a [=count router condition result=]. - 1. Set |result|'s [=count router condition result/total count=] to 0. - 1. Set |result|'s [=count router condition result/depth=] to 1. - 1. Let |maxCount| be 1024. - 1. Let |maxDepth| be 10. + 1. Set |result|'s [=count router condition result/condition count=] to 1024. + 1. Set |result|'s [=count router condition result/quota exceeded=] to false. 1. [=list/For each=] |rule| of |routerRules|: - 1. Let |currentResult| be the result of running [=Count Router Inner Conditions=] with |rule|["{{RouterRule/condition}}"], |result|, |maxCount|, and |maxDepth|. - 1. If |currentResult|'s [=count router condition result/total count=] exceeds |maxCount|, return false. - 1. If |currentResult|'s [=count router condition result/depth=] exceeds |maxDepth|, return false. - 1. Set |result|'s [=count router condition result/total count=] to |currentResult|'s [=count router condition result/total count=]. + 1. Set |result| to be the result of running [=Count Router Inner Conditions=] with |rule|["{{RouterRule/condition}}"], |result|, and 10. + 1. If |result|'s [=count router condition result/quota exceeded=] is true, return false. 1. return true.
@@ -3490,25 +3486,23 @@ spec: storage; urlPrefix: https://storage.spec.whatwg.org/ : Input :: |condition|, a {{RouterCondition}} :: |result|, a [=count router condition result=] - :: |maxCount|, a number - :: |maxDepth|, a number + :: |depth|, 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 |result|'s [=count router condition result/depth=] exceeds |maxDepth|, return |result|. + 1. Decrement |result|'s [=count router condition result/condition count=] by one. + 1. If |result|'s [=count router condition result/condition count=] is zero, or |depth| is zero, then: + 1. Set |result|'s [=count router condition result/quota exceeded=] to be true. + 1. Return |result|. 1. If |condition|["{{RouterCondition/_or}}"] [=map/exists=], then: - 1. Increment |result|'s [=count router condition result/depth=] by one. + 1. Decrement |depth| by one. 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. Set |result| to be the result of running [=Count Router Inner Conditions=] with |orCondition|, |result|, and |depth|. + 1. If |result|'s [=count router condition result/quota exceeded=] is true, return |result|. 1. Else if |condition|["{{RouterCondition/not}}"] [=map/exists=], then: - 1. Increment |result|'s [=count router condition result/depth=] by one. - 1. Set |result| to 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. Decrement |depth| by one. + 1. Set |result| to be the result of running [=Count Router Inner Conditions=] with |condition|["{{RouterCondition/not}}"], |result|, and |depth|. + 1. If |result|'s [=count router condition result/quota exceeded=] is true, return |result|. 1. Return |result|.
From 645a4003b63741ae3f0a294e5d185334149681e4 Mon Sep 17 00:00:00 2001 From: Shunya Shishido Date: Thu, 6 Feb 2025 14:43:25 +0900 Subject: [PATCH 6/6] Update docs/index.bs Co-authored-by: Domenic Denicola --- docs/index.bs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/index.bs b/docs/index.bs index abf22b28..2813b1c8 100644 --- a/docs/index.bs +++ b/docs/index.bs @@ -3494,7 +3494,7 @@ spec: storage; urlPrefix: https://storage.spec.whatwg.org/ 1. [=list/For each=] |rule| of |routerRules|: 1. Set |result| to be the result of running [=Count Router Inner Conditions=] with |rule|["{{RouterRule/condition}}"], |result|, and 10. 1. If |result|'s [=count router condition result/quota exceeded=] is true, return false. - 1. return true. + 1. Return true.