Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-26165] iOS: Fix requiring a JSON file with single quotes #10238

Merged
merged 6 commits into from
Aug 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 14 additions & 16 deletions iphone/Classes/KrollBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -887,22 +887,20 @@ - (NSString *)loadFile:(NSString *)path

- (KrollWrapper *)loadJavascriptObject:(NSString *)data fromFile:(NSString *)filename withContext:(KrollContext *)kroll
{
// We could cheat and just do "module.exports = %data%", but that wouldn't validate that the passed in content was JSON
// and may open a security hole.

// TODO It'd be good to try and handle things more gracefully if the JSON is "bad"/malformed

// Take JSON and turn into JS program that assigns module.exports to the parsed JSON
// 1. trim leading and trailing newlines and whitespace from JSON file
data = [data stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
// 2. Escape single quotes
data = [data stringByReplacingOccurrencesOfString:@"'" withString:@"\'"];
// 3. assign module.exports as JSON.parse call on the JSON
data = [@"module.exports = JSON.parse('" stringByAppendingString:data];
// 4. Replace newlines with "' +\n'"
data = [data stringByReplacingOccurrencesOfString:@"\n" withString:@"' +\n'"];
// 5. close the JSON string and end the JSON.parse call
data = [data stringByAppendingString:@"');"];
NSError *jsonParseError = nil;
NSError *jsonStringifyError = nil;

// 1. Parse JSON
__unused NSDictionary *parsedJSON = [TiUtils jsonParse:data error:&jsonParseError];

// 2. Validate parsed JSON
if (jsonParseError != nil) {
DebugLog(@"[ERROR] Unable to parse JSON input!");
return nil;
}

// 3. Assign valid JSON to module.exports
data = [NSString stringWithFormat:@"module.exports = %@;", data];

return [self loadJavascriptText:data fromFile:filename withContext:kroll];
}
Expand Down
19 changes: 19 additions & 0 deletions tests/Resources/ti.require.addontest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Appcelerator Titanium Mobile
* Copyright (c) 2011-Present by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
/* eslint-env mocha */
/* global Ti */
/* eslint no-unused-expressions: "off" */
'use strict';
var should = require('./utilities/assertions');

describe('require()', function () {
it('JSON-based require() with single-quotes', function () {
var amber = require('./json_files/amber');
should(amber).have.property('sdk');
should(amber.sdk).be.eql('7.4.0.v20180627024922');
});
});