Skip to content

Commit d777c3e

Browse files
authored
Fix buffer permissions for legacy reasons (#377)
This fixes a legacy case of apps on a broken state where the app asks for complete protection but the buffer was created before we changed the directory permissions.
1 parent 1f20006 commit d777c3e

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

platform/swift/source/LoggerBridge.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,33 @@ func makeDirectoryAndDisableProtection(at path: String) throws {
2626
var fileProtection: AnyObject?
2727
try url.getResourceValue(&fileProtection, forKey: .fileProtectionKey)
2828
if let protection = fileProtection as? URLFileProtection, protection != .none {
29+
// Remove any restrictions from to the top level folder
2930
try url.setResourceValue(URLFileProtection.none, forKey: .fileProtectionKey)
3031
}
32+
33+
// We now check if the buffers directory has the right permission, this might not be the case
34+
// for cases where the app ran before we set the right permissions.
35+
//
36+
// TODO(Fz): Having the `buffers` hardcoded here is not ideal and can come and bite us in the
37+
// future, we should remove this once newer sdk versions are widespread.
38+
guard let buffers = url.appendingPathComponent("buffers", isDirectory: true),
39+
manager.fileExists(atPath: buffers.path)
40+
else {
41+
return
42+
}
43+
44+
// Check if the buffers/ directory has the right permissions, otherwise we'll recursively
45+
// remove file restrictions (for legacy reasons)
46+
let bufferAttributes = try buffers.resourceValues(forKeys: [.fileProtectionKey])
47+
if bufferAttributes.fileProtection != URLFileProtection.none {
48+
// Remove any restrictions from the top level buffers directory
49+
try (buffers as NSURL).setResourceValue(URLFileProtection.none, forKey: .fileProtectionKey)
50+
51+
// Remove any restrictions for buffers/*
52+
for item in try manager.contentsOfDirectory(at: buffers, includingPropertiesForKeys: []) {
53+
try (item as NSURL).setResourceValue(URLFileProtection.none, forKey: .fileProtectionKey)
54+
}
55+
}
3156
}
3257

3358
/// A wrapper around Rust logger ID that makes it possible to call Rust logger methods.

test/platform/swift/unit_integration/core/LoggerTests.swift

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,28 +351,44 @@ final class LoggerTests: XCTestCase {
351351
try! FileManager.default.createDirectory(at: existingWithComplete,
352352
withIntermediateDirectories: true)
353353

354-
// Make sure protections are correct
355-
XCTAssertEqual(.complete, try! protection(at: root.path))
356-
XCTAssertEqual(.complete, try! protection(at: existingWithComplete.path))
357-
XCTAssertNil(try! protection(at: existingWithNone.path))
354+
// Create a buffer directory inside the root path (should inherit complete protection)
355+
let buffers = root.appendingPathComponent("buffers")
356+
try! FileManager.default.createDirectory(at: buffers,
357+
withIntermediateDirectories: true)
358+
359+
// Create a file under buffers to pretend like it's the ring buffer
360+
let bufferFile = buffers.appendingPathComponent("bufferFile")
361+
FileManager.default.createFile(atPath: bufferFile.path,
362+
contents: "foobar".data(using: .ascii))
363+
try! (bufferFile as NSURL).setResourceValue(URLFileProtection.complete, forKey: .fileProtectionKey)
358364

359365
// Test to see if disable protection works on a new path
366+
XCTAssertEqual(.complete, try! protection(at: root.path))
360367
let newPath = root.appendingPathComponent("newPath")
361368
XCTAssertFalse(FileManager.default.fileExists(atPath: newPath.path))
362369
try! makeDirectoryAndDisableProtection(at: newPath.path)
363370
XCTAssertEqual(.completeUntilFirstUserAuthentication,
364371
try! protection(at: newPath.path))
365372

366373
// Test to see if disable protection works on an existing path with complete
374+
XCTAssertEqual(.complete, try! protection(at: existingWithComplete.path))
367375
XCTAssertTrue(FileManager.default.fileExists(atPath: existingWithComplete.path))
368376
try! makeDirectoryAndDisableProtection(at: existingWithComplete.path)
369377
XCTAssertEqual(.completeUntilFirstUserAuthentication,
370378
try! protection(at: existingWithComplete.path))
371379

372380
// Test to see if disable protection works on an existing path with none
381+
XCTAssertNil(try! protection(at: existingWithNone.path))
373382
XCTAssertTrue(FileManager.default.fileExists(atPath: existingWithNone.path))
374383
try! makeDirectoryAndDisableProtection(at: existingWithNone.path)
375384
XCTAssertNil(try! protection(at: existingWithNone.path))
385+
386+
// Test to see if disable protection works on all files under buffers
387+
XCTAssertEqual(.complete, try! protection(at: bufferFile.path))
388+
XCTAssertTrue(FileManager.default.fileExists(atPath: bufferFile.path))
389+
try! makeDirectoryAndDisableProtection(at: root.path)
390+
XCTAssertEqual(.completeUntilFirstUserAuthentication,
391+
try! protection(at: bufferFile.path))
376392
}
377393
}
378394

0 commit comments

Comments
 (0)