Skip to content

Commit

Permalink
Merge pull request keycloak#2380 from thomasdarimont/issue/KEYCLOAK-2664
Browse files Browse the repository at this point in the history
KEYCLOAK-2664 - QRCodeResource should prohibit caching of the generated image
  • Loading branch information
stianst committed Mar 21, 2016
2 parents 50ed089 + 3e6b650 commit 973619d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import org.keycloak.services.util.CacheControlUtil;

import javax.servlet.ServletException;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.CacheControl;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.IOException;
Expand Down Expand Up @@ -87,7 +89,18 @@ public void write(OutputStream os) throws IOException,
}
};

return Response.ok(stream).build();
}
/*
* This response is served with extra headers that tell the browser to not do any caching.
* The reason is that this page will include a QR code that can give an attacker access to
* the time based tokens, so it's best to take precautions and make sure there are no copies
* of the QR code lost in a cache.
*/
CacheControl cacheControl = CacheControlUtil.noCache();

return Response.ok(stream) //
.cacheControl(cacheControl) //
.header("Pragma","no-cache") //
.header("Expires", "0") //
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,14 @@ public static CacheControl getDefaultCacheControl() {
}

public static CacheControl noCache() {

CacheControl cacheControl = new CacheControl();
cacheControl.setMustRevalidate(true);
cacheControl.setNoCache(true);
cacheControl.setNoStore(true);

return cacheControl;
}


}

0 comments on commit 973619d

Please sign in to comment.