Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.file.Files;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermissions;

/**
* A temporary file used to cache messages.
Expand All @@ -37,12 +40,34 @@ class TempFile {
* The file will be deleted when the JVM exits.
*/
public TempFile(File dir) throws IOException {
file = File.createTempFile("mbox.", ".mbox", dir);
// XXX - need JDK 6 to set permissions on the file to owner-only
file = createTempFile("mbox.", ".mbox", dir);
file.deleteOnExit();
sf = new WritableSharedFile(file);
}

/**
* Create the cache file with owner-only permissions so that the
* cached message content isn't readable by other local users. On
* file systems without POSIX permissions the platform default is
* used.
*/
private static File createTempFile(String prefix, String suffix, File dir)
throws IOException {
try {
FileAttribute<?> attr = PosixFilePermissions.asFileAttribute(
PosixFilePermissions.fromString("rw-------"));
if (dir != null)
return Files.createTempFile(
dir.toPath(), prefix, suffix, attr).toFile();
return Files.createTempFile(prefix, suffix, attr).toFile();
} catch (UnsupportedOperationException ex) {
if (dir != null)
return Files.createTempFile(
dir.toPath(), prefix, suffix).toFile();
return Files.createTempFile(prefix, suffix).toFile();
}
}

/**
* Return a stream for appending to the temp file.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2009, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.eclipse.angus.mail.mbox;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.junit.Assume.assumeTrue;

/**
* Test that the message cache temp file is created owner-only.
*/
public final class TempFileTest {

@Rule
public final TemporaryFolder folder = new TemporaryFolder();

@Test
public void testCacheFileIsOwnerOnly() throws Exception {
assumeTrue("requires a POSIX file system",
FileSystems.getDefault().supportedFileAttributeViews()
.contains("posix"));

File dir = folder.newFolder();
TempFile tf = new TempFile(dir);
try {
File[] created = dir.listFiles((d, n) ->
n.startsWith("mbox.") && n.endsWith(".mbox"));
assertEquals(1, created.length);
Path p = created[0].toPath();
Set<PosixFilePermission> perms = Files.getPosixFilePermissions(p);
assertEquals("cache file must not be readable by group or others",
Set.of(PosixFilePermission.OWNER_READ,
PosixFilePermission.OWNER_WRITE),
perms);
} finally {
tf.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermissions;

/**
* A temporary file used to cache POP3 messages.
Expand All @@ -32,12 +35,34 @@ class TempFile {
* The file will be deleted when the JVM exits.
*/
public TempFile(File dir) throws IOException {
file = File.createTempFile("pop3.", ".mbox", dir);
// XXX - need JDK 6 to set permissions on the file to owner-only
file = createTempFile("pop3.", ".mbox", dir);
file.deleteOnExit();
sf = new WritableSharedFile(file);
}

/**
* Create the cache file with owner-only permissions so that the
* cached message content isn't readable by other local users. On
* file systems without POSIX permissions the platform default is
* used.
*/
private static File createTempFile(String prefix, String suffix, File dir)
throws IOException {
try {
FileAttribute<?> attr = PosixFilePermissions.asFileAttribute(
PosixFilePermissions.fromString("rw-------"));
if (dir != null)
return Files.createTempFile(
dir.toPath(), prefix, suffix, attr).toFile();
return Files.createTempFile(prefix, suffix, attr).toFile();
} catch (UnsupportedOperationException ex) {
if (dir != null)
return Files.createTempFile(
dir.toPath(), prefix, suffix).toFile();
return Files.createTempFile(prefix, suffix).toFile();
}
}

/**
* Return a stream for appending to the temp file.
*/
Expand Down