[PATCH] 0016 - Fix BasicConstraints min/max path length check
Ping. Who wants to review this :) On Thu, Sep 25, 2014 at 04:07:56PM +1000, Fraser Tweedale wrote:
This patch fixes https://fedorahosted.org/pki/ticket/1035
From ee503f8fc08243906c130149f41dc8d77442d9c7 Mon Sep 17 00:00:00 2001 From: Fraser Tweedale <ftweedal@redhat.com> Date: Thu, 25 Sep 2014 01:39:40 -0400 Subject: [PATCH] Fix BasicConstraints min/max path length check
The BasicConstraintsExtConstraint min/max path length validity check ensures that the max length is greater than the min length, however, when a negative value is used to represent "no max", the check fails.
Only compare the min and max length if the max length is non-negative.
Ticket #1035 --- .../netscape/cms/profile/constraint/BasicConstraintsExtConstraint.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/base/server/cms/src/com/netscape/cms/profile/constraint/BasicConstraintsExtConstraint.java b/base/server/cms/src/com/netscape/cms/profile/constraint/BasicConstraintsExtConstraint.java index ca2668f7db305122f330fca058b27801820a75b4..8fbea435825194d7d31ecda5e65414f1081eeb01 100644 --- a/base/server/cms/src/com/netscape/cms/profile/constraint/BasicConstraintsExtConstraint.java +++ b/base/server/cms/src/com/netscape/cms/profile/constraint/BasicConstraintsExtConstraint.java @@ -211,7 +211,7 @@ public class BasicConstraintsExtConstraint extends EnrollConstraint {
int maxLen = getInt(value);
- if (minLen >= maxLen) { + if (maxLen >= 0 && minLen >= maxLen) { CMS.debug("BasicConstraintExt: minPathLen >= maxPathLen!");
throw new EPropertyException("bad value"); -- 1.9.3
_______________________________________________ Pki-devel mailing list Pki-devel@redhat.com https://www.redhat.com/mailman/listinfo/pki-devel
On 11/20/2014 12:58 AM, Fraser Tweedale wrote:
Ping. Who wants to review this :)
Looks short enough, I'll bite. :) The changes seem to be fine, so it's ACKed. There are some related issues/questions. Feel free to address them as part of this ticket, or maybe file a separate ticket. 1. The exception message is not very helpful. It probably should have said something like: Max path length cannot be smaller than min path length: <maxLen value> 2. According to the ticket the ca-profile-add failed but the profile actually got added. Suppose there's a real constraint violation I think it should display the above exception message, and the profile should not be added. 3. Can we specify any negative value to indicate no min/max, or does it have to be -1? It should be consistent with the documentation. 4. The code seems to assume that the MinPathLen is already added before adding MaxPathLen. Suppose the MinPathLen is missing or added later will this constraint still be validated? Also, maybe we should keep track the list of unreviewed patches on the top of etherpad so we don't miss anything. -- Endi S. Dewata
On Thu, Nov 20, 2014 at 10:06:52AM -0600, Endi Sukma Dewata wrote:
On 11/20/2014 12:58 AM, Fraser Tweedale wrote:
Ping. Who wants to review this :)
Looks short enough, I'll bite. :)
The changes seem to be fine, so it's ACKed.
Thanks Endi; I'll reroll the patch to address those points.
There are some related issues/questions. Feel free to address them as part of this ticket, or maybe file a separate ticket.
1. The exception message is not very helpful. It probably should have said something like:
Max path length cannot be smaller than min path length: <maxLen value>
Yep, I'll do that.
2. According to the ticket the ca-profile-add failed but the profile actually got added. Suppose there's a real constraint violation I think it should display the above exception message, and the profile should not be added.
3. Can we specify any negative value to indicate no min/max, or does it have to be -1? It should be consistent with the documentation.
Any negative value is considered "no min/max". AFAICT this is consistent with the documentation. If I'm overlooking something please point it out.
4. The code seems to assume that the MinPathLen is already added before adding MaxPathLen. Suppose the MinPathLen is missing or added later will this constraint still be validated?
Good catch. I'll perform the validation each time setConfig is called.
Also, maybe we should keep track the list of unreviewed patches on the top of etherpad so we don't miss anything.
+1 ; any objections?
-- Endi S. Dewata
New patch attached. Re point 1., despite the improved ``EPropertyException`` thrown in the new code, the error reported by the CLI is ``PKIException: Error changing profile data``. I filed https://fedorahosted.org/pki/ticket/1212 to address this later. 3. is not an issue and 2. and 4. have been addressed in the new patch. On Thu, Nov 20, 2014 at 10:06:52AM -0600, Endi Sukma Dewata wrote:
On 11/20/2014 12:58 AM, Fraser Tweedale wrote:
Ping. Who wants to review this :)
Looks short enough, I'll bite. :)
The changes seem to be fine, so it's ACKed.
There are some related issues/questions. Feel free to address them as part of this ticket, or maybe file a separate ticket.
1. The exception message is not very helpful. It probably should have said something like:
Max path length cannot be smaller than min path length: <maxLen value>
2. According to the ticket the ca-profile-add failed but the profile actually got added. Suppose there's a real constraint violation I think it should display the above exception message, and the profile should not be added.
3. Can we specify any negative value to indicate no min/max, or does it have to be -1? It should be consistent with the documentation.
4. The code seems to assume that the MinPathLen is already added before adding MaxPathLen. Suppose the MinPathLen is missing or added later will this constraint still be validated?
Also, maybe we should keep track the list of unreviewed patches on the top of etherpad so we don't miss anything.
-- Endi S. Dewata
On 11/24/2014 12:03 AM, Fraser Tweedale wrote:
New patch attached.
Re point 1., despite the improved ``EPropertyException`` thrown in the new code, the error reported by the CLI is ``PKIException: Error changing profile data``. I filed https://fedorahosted.org/pki/ticket/1212 to address this later.
3. is not an issue and 2. and 4. have been addressed in the new patch.
I'm not sure if we should do this: String origValue = getConfig(name); cs.putString(name, value); try { validateConfig(); } catch (EPropertyException e) { cs.putString(name, origValue); throw e; } In case validateConfig() throws a different exception, the origValue may not be restored, so the new (possibly invalid) value will remain in the config store. Another thing, in the following code suppose the CONFIG_MIN_PATH_LEN contains an invalid value, the CONFIG_MAX_PATH_LEN will not be read: int minLen = -1; int maxLen = -1; try { minLen = getConfigInt(CONFIG_MIN_PATH_LEN); maxLen = getConfigInt(CONFIG_MAX_PATH_LEN); } catch (NumberFormatException e) { // one of the path len configs is empty - treat as unset } It may not make a difference now since the validation is only done if both variables are not negative, but in case the validation code is expanded to check for other things, the maxLen might not have the right value. Also, I think the original code would throw an error if the value is not a valid integer while here it's swallowed. I think the code should either validate each parameter as it gets added, or validate all parameters after all parameters get added. So in this case the setConfig() might look like this: validateParam(name, value); cs.putString(name, value); and the validateParam() might look like this: // if the new param is minLen if (name == CONFIG_MIN_PATH_LEN) { // if minLen is unset, return if (value is empty) return; // validate minLen is an integer int minLen = getInt(value); // if minLen is negative, return if (minLen < 0) return; // if maxLen is unset, return String maxLenString = getConfig(CONFIG_MAX_PATH_LEN); if (maxLenString is empty) return; // if maxLen is negative, return int maxLen = getInt(maxLenString); if (maxLen < 0) return; // validate minLen is less than maxLen if (minLen >= maxLen) throw exception } Same thing for maxLen. So the code is a bit longer, but I'm not sure it can be simplified without compromising the validation. By the way, this is a separate issue and feel free to open a ticket. I think the validation should check for minLen > maxLen instead of >=. This way the minLen can be equal to maxLen, meaning the parameter has to be a specific length. -- Endi S. Dewata
On Mon, Nov 24, 2014 at 01:08:26PM -0600, Endi Sukma Dewata wrote:
On 11/24/2014 12:03 AM, Fraser Tweedale wrote:
New patch attached.
Re point 1., despite the improved ``EPropertyException`` thrown in the new code, the error reported by the CLI is ``PKIException: Error changing profile data``. I filed https://fedorahosted.org/pki/ticket/1212 to address this later.
3. is not an issue and 2. and 4. have been addressed in the new patch.
I'm not sure if we should do this:
String origValue = getConfig(name); cs.putString(name, value); try { validateConfig(); } catch (EPropertyException e) { cs.putString(name, origValue); throw e; }
In case validateConfig() throws a different exception, the origValue may not be restored, so the new (possibly invalid) value will remain in the config store.
Good point - I will catch all exceptions.
Another thing, in the following code suppose the CONFIG_MIN_PATH_LEN contains an invalid value, the CONFIG_MAX_PATH_LEN will not be read:
int minLen = -1; int maxLen = -1; try { minLen = getConfigInt(CONFIG_MIN_PATH_LEN); maxLen = getConfigInt(CONFIG_MAX_PATH_LEN); } catch (NumberFormatException e) { // one of the path len configs is empty - treat as unset }
It may not make a difference now since the validation is only done if both variables are not negative, but in case the validation code is expanded to check for other things, the maxLen might not have the right value. Also, I think the original code would throw an error if the value is not a valid integer while here it's swallowed.
I think the code should either validate each parameter as it gets added, or validate all parameters after all parameters get added. So in this case the setConfig() might look like this:
validateParam(name, value); cs.putString(name, value);
and the validateParam() might look like this:
// if the new param is minLen if (name == CONFIG_MIN_PATH_LEN) {
// if minLen is unset, return if (value is empty) return;
// validate minLen is an integer int minLen = getInt(value);
// if minLen is negative, return if (minLen < 0) return;
// if maxLen is unset, return String maxLenString = getConfig(CONFIG_MAX_PATH_LEN); if (maxLenString is empty) return;
// if maxLen is negative, return int maxLen = getInt(maxLenString); if (maxLen < 0) return;
// validate minLen is less than maxLen if (minLen >= maxLen) throw exception }
Same thing for maxLen. So the code is a bit longer, but I'm not sure it can be simplified without compromising the validation.
Ah, the perils of objects whose initialisation/validity check is not contained in the constructor - or even better, the types! I think that validating each parameter when it is set is too much code, and logic will be duplicated. A single validity check guarded by the condition that all parameters have been set is the approach I will take (the second approach you mention above).
By the way, this is a separate issue and feel free to open a ticket. I think the validation should check for minLen > maxLen instead of >=. This way the minLen can be equal to maxLen, meaning the parameter has to be a specific length.
Yes, I noticed this but have preserved the original semantics. I agree; will file a new ticket. Cheers, Fraser
-- Endi S. Dewata
v3 patch. In this patch, validateConfig() is short-circuited until all params have been set, to avoid repetition of logic in the validity check. Also, a config that causes validation to fail is now reverted on any exception, not just EPropertyException. Finally, the profile still gets created when there is bad config so I filed another ticket for that: https://fedorahosted.org/pki/ticket/1215 Cheers, Fraser On Mon, Nov 24, 2014 at 01:08:26PM -0600, Endi Sukma Dewata wrote:
On 11/24/2014 12:03 AM, Fraser Tweedale wrote:
New patch attached.
Re point 1., despite the improved ``EPropertyException`` thrown in the new code, the error reported by the CLI is ``PKIException: Error changing profile data``. I filed https://fedorahosted.org/pki/ticket/1212 to address this later.
3. is not an issue and 2. and 4. have been addressed in the new patch.
I'm not sure if we should do this:
String origValue = getConfig(name); cs.putString(name, value); try { validateConfig(); } catch (EPropertyException e) { cs.putString(name, origValue); throw e; }
In case validateConfig() throws a different exception, the origValue may not be restored, so the new (possibly invalid) value will remain in the config store.
Another thing, in the following code suppose the CONFIG_MIN_PATH_LEN contains an invalid value, the CONFIG_MAX_PATH_LEN will not be read:
int minLen = -1; int maxLen = -1; try { minLen = getConfigInt(CONFIG_MIN_PATH_LEN); maxLen = getConfigInt(CONFIG_MAX_PATH_LEN); } catch (NumberFormatException e) { // one of the path len configs is empty - treat as unset }
It may not make a difference now since the validation is only done if both variables are not negative, but in case the validation code is expanded to check for other things, the maxLen might not have the right value. Also, I think the original code would throw an error if the value is not a valid integer while here it's swallowed.
I think the code should either validate each parameter as it gets added, or validate all parameters after all parameters get added. So in this case the setConfig() might look like this:
validateParam(name, value); cs.putString(name, value);
and the validateParam() might look like this:
// if the new param is minLen if (name == CONFIG_MIN_PATH_LEN) {
// if minLen is unset, return if (value is empty) return;
// validate minLen is an integer int minLen = getInt(value);
// if minLen is negative, return if (minLen < 0) return;
// if maxLen is unset, return String maxLenString = getConfig(CONFIG_MAX_PATH_LEN); if (maxLenString is empty) return;
// if maxLen is negative, return int maxLen = getInt(maxLenString); if (maxLen < 0) return;
// validate minLen is less than maxLen if (minLen >= maxLen) throw exception }
Same thing for maxLen. So the code is a bit longer, but I'm not sure it can be simplified without compromising the validation.
By the way, this is a separate issue and feel free to open a ticket. I think the validation should check for minLen > maxLen instead of >=. This way the minLen can be equal to maxLen, meaning the parameter has to be a specific length.
-- Endi S. Dewata
On 11/26/2014 1:02 PM, Fraser Tweedale wrote:
v3 patch.
In this patch, validateConfig() is short-circuited until all params have been set, to avoid repetition of logic in the validity check.
Also, a config that causes validation to fail is now reverted on any exception, not just EPropertyException.
Finally, the profile still gets created when there is bad config so I filed another ticket for that: https://fedorahosted.org/pki/ticket/1215
This is getting much more complicated than I anticipated :) The patch still has some issues: 1. To ensure we revert all invalid config changes, the code should catch Throwable instead of Exception when calling the validateConfig(), but see below. 2. I'm not sure if allConfigsSet() is the right way to do this. Let's say we add a config with an invalid value, it may actually accept the invalid value because the validation only happens after all configs are set. If a validation error happens later, it will only revert the last config set, not the config with the invalid value. Let's say the profile doesn't specify all defined configs, the validation may never happen. It's also kind of inefficient because it's iterating through all defined configs on each config set. I think ideally the validation should happen after the loop that calls setConfig(), for example: for (String name : names) { String value = ... constraint.setConfig(name, value); } constraint.validateConfig(); We may not even need to revert the invalid config if the code discards the constraint object. If you'd like, feel free to push the original patch since it's already ACKed. We can improve the constraint config validation as a separate ticket. -- Endi S. Dewata
On Fri, Dec 05, 2014 at 03:58:32AM +0700, Endi Sukma Dewata wrote:
On 11/26/2014 1:02 PM, Fraser Tweedale wrote:
v3 patch.
In this patch, validateConfig() is short-circuited until all params have been set, to avoid repetition of logic in the validity check.
Also, a config that causes validation to fail is now reverted on any exception, not just EPropertyException.
Finally, the profile still gets created when there is bad config so I filed another ticket for that: https://fedorahosted.org/pki/ticket/1215
This is getting much more complicated than I anticipated :) The patch still has some issues:
1. To ensure we revert all invalid config changes, the code should catch Throwable instead of Exception when calling the validateConfig(), but see below.
2. I'm not sure if allConfigsSet() is the right way to do this. Let's say we add a config with an invalid value, it may actually accept the invalid value because the validation only happens after all configs are set. If a validation error happens later, it will only revert the last config set, not the config with the invalid value. Let's say the profile doesn't specify all defined configs, the validation may never happen. It's also kind of inefficient because it's iterating through all defined configs on each config set.
I think ideally the validation should happen after the loop that calls setConfig(), for example:
for (String name : names) { String value = ... constraint.setConfig(name, value); } constraint.validateConfig();
We may not even need to revert the invalid config if the code discards the constraint object.
If you'd like, feel free to push the original patch since it's already ACKed. We can improve the constraint config validation as a separate ticket.
Thanks Endi, I'll push the original patch, give this thread a good shake and file tickets for whatever falls out. Ideally objects would be initialised (or throw if invalid) at construction, and be immutable thereafter. We need to be couragous and push the code in this direction to prevent these sorts of bugs and improve maintainability. Changing even one interface such as IPolicyConstraint would be a big change, but it's worth doing for the long run. Fraser
On Thu, Nov 20, 2014 at 10:06:52AM -0600, Endi Sukma Dewata wrote:
On 11/20/2014 12:58 AM, Fraser Tweedale wrote:
Ping. Who wants to review this :)
Looks short enough, I'll bite. :)
The changes seem to be fine, so it's ACKed.
Pushed: master f955714b64a41026915ce328484181d76e456318 DOGTAG_10_2_0_BRANCH 299ed158e7bc2ebfda8b73c9e8c2883d08556ad7
There are some related issues/questions. Feel free to address them as part of this ticket, or maybe file a separate ticket.
1. The exception message is not very helpful. It probably should have said something like:
Max path length cannot be smaller than min path length: <maxLen value>
2. According to the ticket the ca-profile-add failed but the profile actually got added. Suppose there's a real constraint violation I think it should display the above exception message, and the profile should not be added.
3. Can we specify any negative value to indicate no min/max, or does it have to be -1? It should be consistent with the documentation.
4. The code seems to assume that the MinPathLen is already added before adding MaxPathLen. Suppose the MinPathLen is missing or added later will this constraint still be validated?
Also, maybe we should keep track the list of unreviewed patches on the top of etherpad so we don't miss anything.
-- Endi S. Dewata
participants (2)
-
Endi Sukma Dewata -
Fraser Tweedale