A JSON vulnerability allows third party website to turn your JSON resource URL into JSONP request under some conditions. To counter this your server can prefix all JSON requests with following string “)]}‘,\n”. Angular will automatically strip the prefix before processing it as JSON.
publicclassAngularArrayConverter:JsonConverter{publicoverridevoidWriteJson(JsonWriterwriter,objectvalue,JsonSerializerserializer){writer.WriteRawValue(")]}',\n");writer.WriteStartArray();varitems=valueasIEnumerable;if(items==null){return;}foreach(variteminitems){serializer.Serialize(writer,item,item.GetType());}writer.WriteEndArray();}publicoverrideobjectReadJson(JsonReaderreader,TypeobjectType,objectexistingValue,JsonSerializerserializer){thrownewNotImplementedException();}publicoverrideboolCanConvert(TypeobjectType){// string is an IEnumerable. This will ensure this converter is only used for arraysreturn!(objectType==typeof(string))&&typeof(IEnumerable).IsAssignableFrom(objectType);}publicoverrideboolCanRead{get{returnfalse;}}}
Then just plug it in using the WebApiConfig.cs class:
1234567
publicstaticvoidRegister(HttpConfigurationconfig){// other config stuff here...config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(newAngularArrayConverter());}
Now using fiddler or wireshark we can see the results: