Skip to content

Instantly share code, notes, and snippets.

@numist
Created July 6, 2013 18:54
Show Gist options
  • Save numist/5940864 to your computer and use it in GitHub Desktop.
Save numist/5940864 to your computer and use it in GitHub Desktop.
NSCoder's rect encoding/decoding is not compliant with NSSecureCoding. Here's a category that provides methods for encoding/decoding rects in a secure manner.
//
// NSCoder+NNSecureRectCoding.h
// Switch
//
// Created by Scott Perry on 07/06/13.
// Copyright © 2013 Scott Perry.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#import <Foundation/Foundation.h>
static NSString *kNNUniqueString = @"87F57FF7-D5A7-47AF-8596-4D0854565D63";
@interface NSCoder (NNSecureRectCoding)
- (void)NN_encodeRect:(NSRect)rect forKey:(NSString *)key;
- (NSRect)NN_decodeRectForKey:(NSString *)key;
@end
//
// NSCoder+NNSecureRectCoding.m
// Switch
//
// Created by Scott Perry on 07/06/13.
// Copyright © 2013 Scott Perry.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#import "NSCoder+NNSecureRectCoding.h"
@implementation NSCoder (NNSecureRectCoding)
- (void)NN_encodeRect:(NSRect)rect forKey:(NSString *)key;
{
/*
* When you call:
*
* [aCoder encodeRect:rect forKey:key];
*
* You're actually calling:
*
* [aCoder encodeObject:NSStringFromRect(rect) forKey:key];
*/
[self encodeDouble:rect.origin.x forKey:[self NN_xKeyForKey:key]];
[self encodeDouble:rect.origin.y forKey:[self NN_yKeyForKey:key]];
[self encodeDouble:rect.size.width forKey:[self NN_widthKeyForKey:key]];
[self encodeDouble:rect.size.height forKey:[self NN_heightKeyForKey:key]];
}
- (NSRect)NN_decodeRectForKey:(NSString *)key;
{
/*
* Which means that when you call:
*
* [aDecoder decodeRectForKey:key];
*
* You're actually calling:
*
* NSNSRectFromString([aDecoder decodeObjectForKey:key]);
*
* Which is not NSSecureCoding-compliant, because of the lack of class information in the decoding call.
*
* This can be used as a workaround:
*
* NSRectFromString([c decodeObjectOfClass:[NSString class] forKey:key]);
*
* But not only is it implementation-dependant, it also throws away the safety of type-checking because the string may be malformed. Which means the only secure way to encode/decode NSRects is to parameterize them properly.
*/
return (NSRect){
.origin.x = [self decodeDoubleForKey:[self NN_xKeyForKey:key]],
.origin.y = [self decodeDoubleForKey:[self NN_yKeyForKey:key]],
.size.width = [self decodeDoubleForKey:[self NN_widthKeyForKey:key]],
.size.height = [self decodeDoubleForKey:[self NN_heightKeyForKey:key]]
};
}
#pragma mark DRY
- (NSString *)NN_xKeyForKey:(NSString *)key;
{
return [NSString stringWithFormat:@"%@:%@:x", key, kNNUniqueString];
}
- (NSString *)NN_yKeyForKey:(NSString *)key;
{
return [NSString stringWithFormat:@"%@:%@:y", key, kNNUniqueString];
}
- (NSString *)NN_widthKeyForKey:(NSString *)key;
{
return [NSString stringWithFormat:@"%@:%@:width", key, kNNUniqueString];
}
- (NSString *)NN_heightKeyForKey:(NSString *)key;
{
return [NSString stringWithFormat:@"%@:%@:height", key, kNNUniqueString];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment