Showing posts with label Happy Hacking. Show all posts
Showing posts with label Happy Hacking. Show all posts

Mix Spring Data Solr and SolrJ in Solr Cloud 5


We are migrating from Solr 4(part of DataStax) to Solr Cloud 5(without Datastax). 
In previous code, we are using Spring Data Solr to talk with Solr, we are using SolrCrudRepository and also map closed set value to java enum class(not use solr.EnumField), map multiple value field to Set.

As Spring Data Solr doesn't work with Solr Cloud 5, we have to replace the DAO layer(SolrCrudRepository) to Use SolrJ directly.
But SolrJ doesn't support map field to Java Enum or map multiple value field to Set.

We don't want to change the Enum to String, change set to List, as if so, we need change a lot of code. 

We want keep the Model class untouched. So I decided to mix them: Use Spring Data Solr to convert between SolrInputDocument and Java object, use SolrJ to update or query Solr Server.
public abstract class XModelRepository<T extends XModel> implements IXModelRepository<T> {
    @Value("${solr.server.core.collection_name}")
    private String collection;

    @Autowired
    protected SolrClient solrServer;

    @Autowired
    protected SolrConverter converter;

    public final void save(final XModel model) {
        try {
            final Iterable<SolrInputDocument> docs = converter.write(Lists.newArrayList(model));
            final UpdateResponse response = solrServer.add(getCollection(), docs.iterator());
            softCommit();
        } catch (IOException | SolrServerException e) {
            throw new WrappedException(ErrorCode.INTERNAL_ERROR, e);
        }
    }
    public XModel findOne(final String id) {
        try {
            final SolrDocument doc = solrServer.getById(collection, id);
            return converter.read(XModel.class, doc);
        } catch (SolrServerException | IOException e) {
            throw new WrappedException(ErrorCode.INTERNAL_ERROR, e);
        }
    }
} 

This works in most case. But there is one issue: when it stores enum to solr, it saves something like xx.enums.UserType:TypeA, when it reads from solr server, tries to convert the string to enum, it fails with  the following error.
Java.lang.IllegalArgumentException: No enum constant xx.enums.UserType:TypeA
at java.lang.Enum.valueOf(Enum.java:236)


To fix this, I have to copy Spring org.springframework.core.convert.support.EnumToStringConverter and register it in SolrConverter in the configuration class like below:
    @Bean
    public static SolrConverter mappingSolrConverter() {
        final MappingContext<? extends SolrPersistentEntity<?>, SolrPersistentProperty> mappingContext =
                new SimpleSolrMappingContext();
        final MappingSolrConverter converter = new MappingSolrConverter(mappingContext);

        final List<Object> converters = new ArrayList<Object>();
        final DefaultConversionService conversionService = new DefaultConversionService();

        converters.add(new EnumToStringConverter(conversionService));

        final CustomConversions customConversions = new CustomConversions(converters) {
            @Override
            public boolean isSimpleType(final Class<?> clazz) {
                if (Enum.class.equals(clazz) || Enum.class.isAssignableFrom(clazz)) {
                    return false;
                }
                return super.isSimpleType(clazz);
            }

        };
        converter.setCustomConversions(customConversions);
        return converter;
    }
 // Copied from Spring org.springframework.core.convert.support.EnumToStringConverter 
 final class EnumToStringConverter implements Converter<Enum<?>, String>, ConditionalConverter {
     private final ConversionService conversionService;

     public EnumToStringConverter(final ConversionService conversionService) {
         this.conversionService = conversionService;
     }

     public boolean matches(final TypeDescriptor sourceType, final TypeDescriptor targetType) {
         for (@SuppressWarnings("rawtypes")
         final Class interfaceType : ClassUtils.getAllInterfacesForClass(sourceType.getType())) {
             if (this.conversionService.canConvert(TypeDescriptor.valueOf(interfaceType), targetType)) {
                 return false;
             }
         }
         return true;
     }

     public String convert(final Enum<?> source) {
         return source.name();
     }
 }

Using Chrome DevTools to Bring Default Context Menu: Enable Copy Text


The Problem
Sometimes site owners disable right-click context menu for some reason: to disallow copy text, or to protect better user experience such as Google Drive and Dropbox:  remove browser context menu and add customized context menu.

As web developer, or hackers, we use Chrome Devtools often, especially its "Inspect Element" function, so sometimes we want to remove this limitation: to bring back the browser default context menu.


Enable Copy - Chrome extension
Simple Solution that Works Most Time: document.oncontextmenu = null
or document.designMode = "on"
Site owner usually add javascript like below to disable context menu: <body oncontextmenu="return false;">
Or:
document.oncontextmenu = function() {
    return false;
};
In this case, we can just execute document.oncontextmenu = null in Chrome Devtools to remove the handler.

But sometimes, things are harder. Let's take a look at how to show default context menu in Google Drive as an example.

If this doesn't work you may run the following javascript code in devtools:
window.oncontextmenu=null;;
window.onmousedown=null;
window.onkeydown=null;
window.onclick=null;

document.oncontextmenu=null;
document.onmousedown=null;
document.onkeydown=null;
document.onclick=null;

document.onselectstart=null;
Re-enable Default Context Menu in Google Drive: by Changing Definition of hca function
To restore the default browser context menu in Google Drive, we first open Chrome Devtools(Using Ctrl+Shift+I), in the bottom panel, go to search tab, type "contextmenu".

It will find matches in js file: 2222407164-doclist_modularized_core.js


Clicking one match will bring you to the line, but this js file is minified, and ubale to read. SO click on the pretty button {}, this will format the js file to make it more readable.


Then search "contextmenu" again in the button search tab. Now it will show matches in the formatted js file. 


The following code seems interesting to us. e.Yqa prevent context menu default behaviour.
e.initialize = function(a) {
    b.listen(a, "contextmenu", this.Yqa);
};
e.Yqa = function(a) {
    a.shiftKey || (a.stopPropagation(), a.preventDefault(), pCa(this, a.clientX, a.clientY, a.target))
};
Let's add a breakpoint in the e.Yqa function, right click on Google Drive, now it stops at the Yqa function. This is great.

From the Call Stack view, we can see that e.Yqa function is called by function hca.
function hca(a, b) {
    var c = a.Gn, d = a.Xd || a.src;
    a.Ooa && Ze(a);
    return c.call(d, b)
}
hca is a global function, we can change its definition in the console to change its behavior.


Check the value of parameter a, and b. Found out when we right click in Google Drive, the value of a.type is "contextmenu".

So we can change hca like below: if a.type="contextmenu", do nothing, return directly.
function hca(a, b) {
   if( a.type == "contextmenu" ) return;
    var c = a.Gn, d = a.Xd || a.src;
    a.Ooa && Ze(a);
    return c.call(d, b)
}
So every time, we want to show Chrome's default context menu in Google Drive, run the previous js code in the console to change the definition of hca. Then right click on Google Drive, it will show default context menu.

Happy hacking!!!

Resource
Don’t Disable Right Click!

Using Chrome DevTools to Hack Client-Side Only Validation


Client-side validation can provide faster response and better user experience, and reduce server load. But relying client-side validation alone is a terrible idea.  

As user can easily bypass the validation, change value of javascript object using tools like Chrome DevTools or Firebug. We must always appropriate server-side validation as well.
Using Chrome DevTools to Hack it
One website limits the max value of one field, and I am wondering whether I can bypass the verification.
1. Found the pattern
I noticed that when I input invalid value, and go to next field, it will change the color of the field to red.
2. Beak on DOM Attributes Modifications 
So when the color of the field changes, I want Chrome to stop at that moment, so I can figure out how this website do the verification.

So I right click the input textbox, click "Inspect Element", this will open Chrome DevTools, and select the checked element in "Elements" tab. 
<input title="" class="input_field_" type="text" name="Memory" id="memory">
Right click it, and select "Break on..." -> "Attributes modifications".

This will cause Chrome pauses when attribute of this input textbox changes.
It would be better if Chrome DevTools allows us to stop when a particular attribute changes, in this case: class field. or even better to stop when a particular attribute changes to a particular value. But anyhow this works.
3. Reproduce It
Now input the invalid value, then trigger the event that will cause the client side validation. Usually this will happen when we focus leaves current field or we click next or submit.

This will cause Chrome stops execution, and switch to "Sources" tab and stops at the code that change 
4. Check the Call Stack
Now we can check the call stack. 
To make  javascript much easier to read, we can press the "Pretty print" button (marked with curly braces{}) from the bottom of the "Sources" tab.

Follow the call stack, I found it do verification like below:
function validateSteps(step) 
{
    var isStepValid = true;
  // omitted
    if (step == step_config) {
        return validateField('#memory', selectedPolicy.minMemory, selectedPolicy.maxMemory) &&...);
    }
    return isStepValid; // we can set a breakpoint value here to change isStepValid to true. 
}
The code compare the value from memory textbox with selectedPolicy.minMemory, selectedPolicy.maxMemory.
5. Hack it
Now there are several ways to hack it: we can step into validateField method and change return value.
The simpler way is to directly modify the object selectedPolicy, change maxMemory to a much bigger value. We go to the "Console" tab, and type selectedPolicy.maxMemory, this will print current value, it's 4. We can type selectedPolicy.maxMemory=100.

Now unclick all breakpoints, and press resume, and submit the form. It works, this site doesn't check the value in server side at all.

Later if I want to so the same trick: bypass the verification, I can go to "Console" tab directly, and type selectedPolicy.maxMemory=100.

In Console tab, we can do many things, run any valid javascript code in current scope.
We can check windows object, and detect any custom properties and functions.

Happy Hacking!!!
Resources
Tips And Tricks

Labels

ANT (6) Algorithm (69) Algorithm Series (35) Android (7) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) JSON (7) Java (186) JavaScript (27) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) adsense (5) bat (8) regex (5) xml (5)