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

SRI support #235

Merged
merged 6 commits into from
Jun 30, 2017
Merged

SRI support #235

merged 6 commits into from
Jun 30, 2017

Conversation

SanBlig
Copy link
Contributor

@SanBlig SanBlig commented Jun 22, 2017

Closes #234

This provides partial support for Subresource Integrity.

The following are yet to be considered:

  1. Cache:
    • A response served through cache must undergo integrity check.
    • If integrity check for a response fails, it shouldn't be added to the cache.
  2. Check eligibility of response for integrity validation - CORS.
  3. Check the syntax before parsing integrity tokens (probably redundant).
  4. Support for options as part of the integrity metadata.

@hrj
Copy link
Member

hrj commented Jun 22, 2017

Excellent !

Adding to the list of things to do:

  • enumerate all the types of requests that can be checked for integrity. Top of my mind: requests from img and iframe elements
  • enumerate the protocols that can be checked for integrity. http:// and https:// would be already covered. Check data as well.

@@ -128,6 +128,10 @@ public void setType(final String type) {
this.setAttribute("type", type);
}

public String getIntegrity() {
return this.getAttribute("integrity");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: check indent

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, make the function private, unless public is necessary.

@@ -108,6 +108,10 @@ public void setType(final String type) {
this.setAttribute("type", type);
}

public String getIntegrity() {
return this.getAttribute("integrity");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: check indent

also check visibility (private / public)

logger.warning("Unable to parse CSS. URI=[" + cssURI + "]. Response status was " + status + ".");
return getEmptyStyleSheet();
}
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove the commented code

import java.util.Map;
import java.util.stream.Collectors;

public class AlgorithmDigest implements Comparable<AlgorithmDigest> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check visibility of class: package private / public

Also make it final.

public static boolean validate(final byte[] input, final String integrity) {

final List<AlgorithmDigest> algDigests = parseMetadata(integrity);
System.out.println("Strongest algorithms: " + algDigests);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nuke all debug prints in this file

System.out.println("The value of the integrity attribute is: " + integrity);

if (algDigests == null)
return true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code style: surround with braces.

For a classic example of the problem that braces protect against, see: https://nakedsecurity.sophos.com/2014/02/24/anatomy-of-a-goto-fail-apples-ssl-bug-explained-plus-an-unofficial-patch/

private static List<AlgorithmDigest> getHashes(final String[] tokens) {
final List<AlgorithmDigest> hashes = new ArrayList<AlgorithmDigest>();

for (int i = 0; i < tokens.length; i++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try converting to enhanced for loop. In eclipse Ctrl 1 should suggest it automatically.

} catch (final Exception e) {
System.out.println("Exception: " + e.getMessage());
}
return "";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is better to return null on failure, instead of empty string.

@@ -251,7 +256,7 @@ private void setResponse(final ClientletResponse response) {
}
try {
this.changeReadyState(NetworkRequest.STATE_LOADING);
final LocalResponse newResponse = new LocalResponse(response);
LocalResponse newResponse = new LocalResponse(response); //was final before
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it can be converted back to final.

SanBlig added 4 commits June 29, 2017 23:25
The following actions take place:
  - If response is from cache, it undergoes integrity check.
  - If response fails integrity check, it is not added to cache.
Used the predefined functional interface Consumer<T> instead of Valid. The latter has been deleted. setResponse now takes Consumer<Boolean> as a parameter and calls accept() instead of returning a boolean value.
Copy link
Member

@hrj hrj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR has come far. Kudos!

Just a few changes remaining.

final Runnable runnable = () -> {
if (response.isFromCache()) {
final Object cachedResponse = response.getTransientCachedObject();
if (cachedResponse instanceof CacheableResponse) {
System.out.println("Cached response for " + response.getResponseURL());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove debug print

}

/*
* (non-Javadoc)
*
* @see net.sourceforge.xamj.http.RequestHandler#handleProgress(int,
* java.net.URL, int, int)
*void java.net.URL, int, int)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks like an accidental change

@@ -97,7 +98,7 @@ public boolean handleException(final ClientletResponse response, final Throwable
* .ClientletResponse)
*/
@Override
public void processResponse(final ClientletResponse response) throws ClientletException, IOException {
public void processResponse(final ClientletResponse response, Consumer<Boolean> consumer) throws ClientletException, IOException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In all these other processResponse implementations, the response is "valid", so you will have to call consumer.accept(true) somewhere in the function (most likely at the end of the function).

* @param userName
* The HTTP authentication user name.
* @param integrity
* Integrity value to be checked.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also looks like a spurious change.

Accidental and spurious changes undone. Added calls to consumer.accept(true) where absent.
@hrj hrj changed the title [WIP] SRI support SRI support Jun 30, 2017
@hrj hrj merged commit 044f400 into gngrOrg:master Jun 30, 2017
@hrj
Copy link
Member

hrj commented Jun 30, 2017

Thanks @SanBlig

I added support for options in f74e263

Other pending stuff has been copied into a new issue: #237

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants