Skip to content

Commit

Permalink
Remove writable shared files
Browse files Browse the repository at this point in the history
  • Loading branch information
Berenz committed Jun 15, 2019
1 parent 2d35b6d commit 1035883
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 55 deletions.
16 changes: 8 additions & 8 deletions src/qz/common/TrayManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,9 @@ public void actionPerformed(ActionEvent e) {
log.debug("Block unsigned: {}", checkBoxState);

if (checkBoxState) {
blackList(Certificate.UNKNOWN, true);
blackList(Certificate.UNKNOWN);
} else {
FileUtilities.deleteFromFile(Constants.BLOCK_FILE, Certificate.UNKNOWN.data(), true);
FileUtilities.deleteFromFile(Constants.BLOCK_FILE, Certificate.UNKNOWN.data());
}
};

Expand Down Expand Up @@ -393,15 +393,15 @@ public boolean showGatewayDialog(final Certificate cert, final String prompt, fi
if (gatewayDialog.isApproved()) {
log.info("Allowed {} to {}", cert.getCommonName(), prompt);
if (gatewayDialog.isPersistent()) {
whiteList(cert, !gatewayDialog.isSharedPersistence());
whiteList(cert);
}
} else {
log.info("Denied {} to {}", cert.getCommonName(), prompt);
if (gatewayDialog.isPersistent()) {
if (Certificate.UNKNOWN.equals(cert)) {
anonymousItem.doClick(); // if always block anonymous requests -> flag menu item
} else {
blackList(cert, !gatewayDialog.isSharedPersistence());
blackList(cert);
}
}
}
Expand All @@ -410,16 +410,16 @@ public boolean showGatewayDialog(final Certificate cert, final String prompt, fi
return gatewayDialog.isApproved();
}

private void whiteList(Certificate cert, boolean local) {
if (FileUtilities.printLineToFile(Constants.ALLOW_FILE, cert.data(), local)) {
private void whiteList(Certificate cert) {
if (FileUtilities.printLineToFile(Constants.ALLOW_FILE, cert.data())) {
displayInfoMessage(String.format(Constants.WHITE_LIST, cert.getOrganization()));
} else {
displayErrorMessage("Failed to write to file (Insufficient user privileges)");
}
}

private void blackList(Certificate cert, boolean local) {
if (FileUtilities.printLineToFile(Constants.BLOCK_FILE, cert.data(), local)) {
private void blackList(Certificate cert) {
if (FileUtilities.printLineToFile(Constants.BLOCK_FILE, cert.data())) {
displayInfoMessage(String.format(Constants.BLACK_LIST, cert.getOrganization()));
} else {
displayErrorMessage("Failed to write to file (Insufficient user privileges)");
Expand Down
32 changes: 3 additions & 29 deletions src/qz/ui/GatewayDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ public class GatewayDialog extends JDialog {
private JPanel optionsPanel;

private JCheckBox persistentCheckBox;
private JCheckBox sharedCheckBox;
private JPanel bottomPanel;
private JPanel bottomCheckPanel;

private JPanel mainPanel;

Expand Down Expand Up @@ -85,29 +83,11 @@ private void initComponents() {
bottomPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
persistentCheckBox = new JCheckBox("Remember this decision", false);
persistentCheckBox.setMnemonic(KeyEvent.VK_R);
persistentCheckBox.addActionListener(e -> {
allowButton.setEnabled(!persistentCheckBox.isSelected() || cert.isTrusted());
sharedCheckBox.setVisible(persistentCheckBox.isSelected());
bottomCheckPanel.invalidate();
repaint();
pack();
});
persistentCheckBox.setAlignmentX(LEFT_ALIGNMENT);

sharedCheckBox = new JCheckBox("Set for all users", false);
sharedCheckBox.setMnemonic(KeyEvent.VK_S);
sharedCheckBox.setAlignmentX(LEFT_ALIGNMENT);
sharedCheckBox.setVisible(false);

bottomCheckPanel = new JPanel();
bottomCheckPanel.setLayout(new BoxLayout(bottomCheckPanel, BoxLayout.Y_AXIS));
bottomCheckPanel.setAlignmentX(RIGHT_ALIGNMENT);

bottomCheckPanel.add(persistentCheckBox);
bottomCheckPanel.add(sharedCheckBox);
persistentCheckBox.addActionListener(e -> allowButton.setEnabled(!persistentCheckBox.isSelected() || cert.isTrusted()));
persistentCheckBox.setAlignmentX(RIGHT_ALIGNMENT);

bottomPanel.add(certInfoLabel);
bottomPanel.add(bottomCheckPanel);
bottomPanel.add(persistentCheckBox);

optionsPanel.add(allowButton);
optionsPanel.add(blockButton);
Expand Down Expand Up @@ -166,8 +146,6 @@ public final void refreshComponents() {

approved = false;
persistentCheckBox.setSelected(false);
sharedCheckBox.setSelected(false);
sharedCheckBox.setVisible(false);
allowButton.setEnabled(true);
allowButton.requestFocusInWindow();
pack();
Expand All @@ -181,10 +159,6 @@ public boolean isPersistent() {
return persistentCheckBox.isSelected();
}

public boolean isSharedPersistence() {
return sharedCheckBox.isSelected();
}

public void setCertificate(Certificate cert) {
this.cert = cert;
}
Expand Down
2 changes: 1 addition & 1 deletion src/qz/ui/SiteManagerDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public Component getListCellRendererComponent(JList list, Object value, int inde
*/
public SiteManagerDialog removeCertificate(CertificateDisplay certificate) {
final ContainerList<CertificateDisplay> certList = getSelectedList();
if (certificate != null && FileUtilities.deleteFromFile(certList.getTag().toString(), certificate.getCert().data(), certificate.isLocal())) {
if (certificate != null && FileUtilities.deleteFromFile(certList.getTag().toString(), certificate.getCert().data())) {
certList.remove(certificate);
} else {
log.warn("Error removing {} from the list of {} sites", certificate, getSelectedTabName().toLowerCase());
Expand Down
25 changes: 8 additions & 17 deletions src/qz/utils/FileUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ public static String readXMLFile(String url, String dataTag) throws DOMException
}


public static boolean printLineToFile(String fileName, String message, boolean local) {
File file = getFile(fileName, local);
public static boolean printLineToFile(String fileName, String message) {
File file = getFile(fileName, true);
if (file == null) { return false; }

try(FileWriter fw = new FileWriter(file, true)) {
Expand Down Expand Up @@ -198,28 +198,19 @@ public static File getFile(String name, boolean local) {
return fileMap.get(name);
}

public static void deleteFile(String name, boolean local) {
File file;
if (local) {
file = localFileMap.get(name);
} else {
file = sharedFileMap.get(name);
}
public static void deleteFile(String name) {
File file = localFileMap.get(name);

if (file != null && !file.delete()) {
log.warn("Unable to delete file {}", name);
file.deleteOnExit();
}

if (local) {
localFileMap.put(name, null);
} else {
sharedFileMap.put(name, null);
}
localFileMap.put(name, null);
}

public static boolean deleteFromFile(String fileName, String deleteLine, boolean local) {
File file = getFile(fileName, local);
public static boolean deleteFromFile(String fileName, String deleteLine) {
File file = getFile(fileName, true);
File temp = getFile(Constants.TEMP_FILE, true);

try(BufferedReader br = new BufferedReader(new FileReader(file)); BufferedWriter bw = new BufferedWriter(new FileWriter(temp))) {
Expand All @@ -234,7 +225,7 @@ public static boolean deleteFromFile(String fileName, String deleteLine, boolean
bw.close();
br.close();

deleteFile(fileName, local);
deleteFile(fileName);
return temp.renameTo(file);
}
catch(IOException e) {
Expand Down

0 comments on commit 1035883

Please sign in to comment.