728x90

저장소 액세스 API는 웹 애플리케이션이 사용자를 대신하여 자사 및 타사 쿠키는 물론 다른 형태의 영구 저장소(예: IndexedDB)에 액세스할 수 있는 권한을 요청할 수 있도록 하는 JavaScript API입니다. 이 API는 사용자 저장소에 액세스하는 보다 안전한 방법을 제공하고 교차 사이트 추적의 위험을 완화하기 위해 도입되었습니다.

다음은 Storage Access API를 사용하여 자사 쿠키에 대한 액세스를 요청하는 방법의 예입니다.

if ('storage' in navigator && 'estimate' in navigator.storage) {

// Check if the API is supported by the browser

navigator.storage.estimate().then(function(estimate) {

// Check the available storage space

if (estimate.quota < 120000000) {

// Prompt the user to grant storage access if there isn't enough space

navigator.storage.requestPersistent().then(function() {

// The user has granted permission, access the storage as needed

}).catch(function() {

// The user has denied permission, handle the error accordingly

});

} else {

// There is enough space, access the storage as needed

}

});

} else {

// The API is not supported by the browser, handle the error accordingly

}

이 예제에서는 먼저 브라우저에서 Storage Access API를 지원하는지, 그리고 사용 가능한 저장 공간을 확인하기 위해 `Estimate()` 메서드를 지원하는지 확인합니다. 그런 다음 사용 가능한 공간이 충분한지 확인하고 공간이 없으면 사용자에게 저장소 액세스 권한을 부여하라는 메시지를 표시합니다. 사용자가 권한을 부여하면 필요에 따라 저장소에 액세스할 수 있습니다. 사용자가 권한을 거부하거나 API가 지원되지 않는 경우 그에 따라 오류를 처리합니다.

다음은 Storage Access API를 사용하여 타사 쿠키에 대한 액세스를 요청하는 방법을 보여주는 또 다른 예입니다.

const storageAccess = navigator.storageAccess || null;

const iframe = document.createElement('iframe');

iframe.src = 'https://example.com/iframe.html';

document.body.appendChild(iframe);

storageAccess.requestPermission({

// Request permission to access third-party cookies

// from the domain hosting the iframe

// (in this case, example.com)

// as well as any first-party cookies that may be associated with it.

iframe: iframe.contentWindow,

storageArea: 'cookies',

}).then(() => {

// The user has granted permission, access the cookies as needed

}).catch((e) => {

// The user has denied permission, handle the error accordingly

});

이 예에서는 타사 도메인(이 경우 example.com)에서 콘텐츠를 로드하는 iframe을 만든 다음 `requestPermission()` 메서드를 사용하여 해당 도메인에서 타사 쿠키에 대한 액세스를 요청합니다. 사용자가 권한을 부여하면 필요에 따라 쿠키에 액세스할 수 있습니다. 사용자가 권한을 거부하거나 API가 지원되지 않는 경우 그에 따라 오류를 처리합니다.

Storage Access API는 모든 브라우저에서 지원되지 않으므로 지원 여부를 확인하고 오류를 적절하게 처리하는 것이 중요합니다. 또한 이 API를 사용할 때 따라야 할 다양한 보안 고려 사항과 모범 사례가 있습니다. 예를 들어 사용자 동의를 얻고 불필요한 저장소 액세스를 피하는 등이 있습니다.

728x90

'IT' 카테고리의 다른 글

자바스크립트 "Service Workers API"  (0) 2023.03.11
자바스크립트 "Storage API"  (0) 2023.03.11
자바스크립트 "Streams API"  (0) 2023.03.11
자바스크립트 "Touch Events API"  (0) 2023.03.11
자바스크립트 "Trusted Types API"  (0) 2023.03.11

+ Recent posts